Declarations

Contents

Number of Declarations per Line

One declaration per line is recommended since it encourages commenting. In other words:

int level; // indentation level
int size;  // size of table

Do not put more than one variable or variables of different types on the same line when declaring them. This example also demonstrates the drawbacks of non-obvious variable names. Be clear when naming variables.

int a, b; double c, d;

Initialization

Try to initialize local variables as soon as they are declared.

string name = myObject.Name;

or

int val = time.Hours;

Class and Interface Declarations

Example   7: Class and Interface declarations

class MySample : MyClass, IMyInterface
{
	private int myInt;

	public MySample(int myInt)
	{
		this.myInt = myInt ;
	}
	void Inc()
	{
		++myInt;
	}
	void EmptyMethod()
	{
	}
}

Methods

Try to keep methods as short as possible. It makes no sense to have rules for the number of lines a method should consist of, but if a method grows big, think of breaking it up in smaller pieces.

Every method should be preceded by a documentation comment explaining the purpose of the method. Every public method?

Properties

Every property should be preceded by a documentation comment explaining the purpose of the property. Public?

Always use a property to expose a public instance member. The private instance member should be named with camel casing.

 

Example   8: Property to expose a public instance member

private int temperature;
/// <summary>
/// Gets or sets the current temperature.
/// </summary>
public int Temperature
{
	get { return this.temperature; }
	set { this.temperature = value; }
}

If the property is simple as in the example above, the formatting can be kept equally simple. For more complex properties, conform to the general guidelines for indentation and formatting.

Example   9: A bit more complex property

/// <summary>
/// Gets the average temperature for this year.
/// </summary>
public int AverageTemperature
{
	get 
	{ 
		int summedTemperature = 0;
		TemperatureCollection temperatures = GetYearTemperatures();
		for(int i = 0; i < temperatures.Count; i++)
		{
			summedTemperature += temperatures[i].Value;
		}
		return summedTemperature / temperatures.Count; 
	}
}

2.6 Variables and Constants

Do not define all variables at the beginning of a block. Instead, define each variable just before it is used for the first time:

Example   10: All variables defined in one place

public static void Main()
{
	string line; // Avoid
	bool exit = false; // Avoid
	Thermometer thermometer = new Thermometer();

	Console.WriteLine(MessageWelcome);
	do 
	{
		...
		line = Console.ReadLine();
		...
	} while(!exit);
}

Example   11: Variables defined before first usage

public static void Main()
{
	Thermometer thermometer = new Thermometer();

	Console.WriteLine(MessageWelcome);

	bool exit = false; // OK

	do 
	{
		...
		string line = Console.ReadLine();// OK
		...
	} while(!exit);
}  

Do not define two variables on the same line:  Duplicate from above

int celsius = 0, farenheit = 0; // Avoid

Define const fields as private or protected if no other class has an interest in them.

Do not use magic numbers! A magic number is a numeric constant embedded in code, without a constant definition. Any number except -1, 0, and 1 is considered magic. Use constant fields instead.

Example   12: Constant fields instead of magic numbers

private const int AbsoluteZero = -273;
private const int MaxTemperature = 100;
...
//Using declared constants is correct
temperature = random.Next(AbsoluteZero, MaxTemperature);
//Use of magic numbers is not correct
temperature = random.Next(-273, 100);