Akce
Štábní kultura¶
C#¶
Ukázka třídy:
using System;
namespace SpaceTraffic
{
/// <summary>
/// What is this class and its purpose?
/// How it works in general?
/// </summary>
public class SampleClass
{
// Region for all instance variables.
#region Fields
private int someNumber = 0;
// don't use type in the name.
private List<int, string> items = new List<int, string>();
// don't use listOfItems or itemList.
private int _somethingComplicated = 0;
// use prefix _ for variables used for storing property value, that should not be used directly even in this class.
// group these properties in the end of this region
#endregion
#region Properties
/// <summary>
/// What is this?
/// </summary>
public string First { get; set; }
/// <summary>
/// What is this?
/// </summary>
public string SomeText { get; private set; }
/// <summary>
/// What is this?
/// </summary>
public bool IsSomething {get; set; }
// use Is prefix for boolean properties
/// <summary>
/// What is this?
/// </summary>
public string SomethingComplicated
{
get { return this._propertyValue; }
set
{
if(String.IsNullOrEmpty(value))
{
this._propertyValue = "default";
}
else
{
// use else branch for better readability
this._propertyValue = value;
}
}
}
#endregion
// If only one item is there, use Constructor instead of Constructors.
#region Constructor
/// <summary>
/// What is performed by the constuctor?
/// </summary>
/// <param name="items">What is this parameter used for?</param>
/// <param name="someNumber">...</param>
/// <param name="something">...</param>
public SampleClass(List<int, string> items, int someNumber, bool something = true)
{
// use same names for parameters as they are for variables and properties
// always use this. for instance variables and methods, even if it is not necessary.
this.items = items;
this.someNumber = someNumber;
}
// perefer use of default values over making multiple constructors with different parameters.
#endregion
/// <summary>
/// What is this method good for?
/// </summary>
public void ForCycleDemo()
{
int[] numbers = new int[100];
foreach( int number in numbers)
{
// do something...
}
// use foreach when iterating through array or collection
int[] otherNumbers = new int[100];
for(int i = 0; i < otherNumbers.Length; i++)
{
numbers[i] = otherNumnbers[i];
}
// use for when it is appropriate.
// reserve name i for iteration index or simple counter
int[,] matrix = new int[10,10];
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int column = 0; column < matrix.GetLength(1); column++)
{
matrix[row, column] = 0; // do something.
}
}
// never use i and j for something like this, we are programmers, not mathematicians.
}
/// <summary>
/// What will this method test?
/// </summary>
/// <param name="something">What is this</param>
/// <returns>When its true? false otherwise.</returns>
public bool IsThisTestTrue(int something)
{
}
public User GetUser(int id)
{ }
public User GetUser(string name)
{ }
// versus
public User GetUserByName(string name)
{ }
public User GetUserById(string id)
{ }
// Use overloading with caution, don't make magic methods with bunch of parameters.
public void TryCatchExample()
{
try
{
// something dangerous
}
catch (SomeParticularException ex)
{
// log this
throw;
// rethrow
}
catch (SomeParticularException ex)
{
//IN CODE:
// should never happen!
// does not matter.
// - if exception can occur in this code, but due to specific circumstances it does not matter or it will never happen,
// document it by commentary.
}
// never ever use catch(Exception ex) without good reason.
// only catch exceptions you know they can occur.
}
private void IfExample()
{
if (something)
null;//then do something short
if (something)
{
}
else
{
}
if (a)
{
if (b)
{
}
else
{
//IN CODE:
// not possible
// document by commentary impossible causes
}
}
else
{
}
if (a)
{
}
else if (b)
{
}
else
{
}
}
}
}
Aktualizováno uživatelem Martin Štěpánek před více než 13 roky(ů) · 4 revizí