Coding Style Guide
Coding Style Guide
2 File Organization
2.1 C Sourcefiles
Keep your classes/files short, don't exceed 2000 LOC, divide your code up, make structures clearer. Put
every class in a separate file and name the file like the class name (with .cs as extension of course). This
convention makes things much easier.
longMethodCall(expr1, expr2,
expr3, expr4, expr5);
PREFER:
var = a * b / (c - g + f) +
4 * z;
The first is preferred, since the break occurs outside the paranthesized expression (higher level rule). Note
that you indent with tabs to the indentation level and then with spaces to the breaking position in our
example this would be:
> var = a * b / (c - g + f) +
> ......4 * z;
Where '>' are tab chars and '.' are spaces. (the spaces after the tab char are the indent with of the tab). A good
coding practice is to make the tab and space chars visible in the editor which is used.
/* Line 1
* Line 2
* Line 3
*/
As this will set off the block visually from code for the (human) reader. Alternatively you might use this
oldfashioned C style for single line comments, even though it is not recommended. In case you use this
style, a line break should follow the comment, as it is hard to see code preceeded by comments in the same
line:
Block comments may be useful in rare cases, refer to the TechNote 'The fine Art of Commenting' for an
example. Generally block comments are useful for comment out large sections of code.
/// <summary>
/// This class...
/// </summary>
All lines must be preceded by three slashes to be accepted as XML comment lines.
Tags fall into two categories:
Documentation items
Formatting/Referencing
The first category contains tags like <summary>, <param> or <exception>. These represent items
that represent the elements of a program's API which must be documented for the program to be useful to
5 Declarations
5.1 Number of Declarations per Line
One declaration per line is recommended since it encourages commenting1. In other words,
Do not put more than one variable or variables of different types on the same line when declaring them.
Example:
The above example also demonstrates the drawbacks of non-obvious variable names. Be clear when naming
variables.
5.2 Initialization
Try to initialize local variables as soon as they are declared. For example:
No space between a method name and the parenthesis "(" starting its parameter list.
The opening brace "{" appears in the next line after the declaration statement.
The closing brace "}" starts a line by itself indented to match its corresponding opening
brace.
For example :
void Inc()
{
++myInt;
}
void EmptyMethod()
{
}
}
6 Statements
6.1 Simple Statements
Each line should contain only one statement.
if (condition) {
DoSomething();
...
}
if (condition) {
DoSomething();
...
} else {
DoSomethingOther();
...
}
if (condition) {
DoSomething();
...
} else if (condition) {
DoSomethingOther();
...
} else {
DoSomethingOtherAgain();
...
}
Note: Generally use brackets even if there is only one statement in the loop.
while (condition) {
...
}
while (condition) ;
do {
...
} while (condition);
switch (condition) {
case A:
...
break;
case B:
...
break;
default:
...
break;
}
or
try {
...
} catch (Exception e) {
...
}
or
try {
...
} catch (Exception e) {
...
} finally {
...
}
7 White Space
7.1 Blank Lines
Blank lines improve readability. They set off blocks of code which are in themselves logically related.
Two blank lines should always be used between:
Methods
Properties
Local variables in a method and its first statement
Logical sections inside a method to improve readability
Note that blank lines must be indented as they would contain a statement this makes insertion in these lines
much easier.
Single spaces surround operators (except unary operators like increment or logical not), example:
Use spaces for the table like formatting and not tabs because the table formatting may look strange in
special tab intent levels.
System.Windows.Forms.Button cancelButton;
System.Windows.Forms.TextBox nameTextBox;
void DoSomething()
{
if (test == Test.TestMe) {
//...stuff gets done
} else {
//...other stuff gets done
}
}
}
}
Note: Indexer variables generally should be called i, j, k etc. But in cases like this, it
may make sense to reconsider this rule. In general, when the same counters or indexers are
reused, give them meaningful names.