When an expression will not fit on a single line, break it up according to these general principles:
Break after a comma.
Break after an operator.
Prefer higher-level breaks to lower-level breaks.
Align the new line with the beginning of the expression at the same level on the previous line
A method call with parameters is broken up at a parameter comma.
longMethodCall(expr1, expr2, expr3, expr4, expr5);
For arithmetic expressions find the higher level to break at. Even though the compiler won’t complain about either of the following, the second version is generally more readable and should be preferred.
Less readable avoid:
var = a * b / (c - g + f) + 4 * z;
Better:
var = a * b / (c - g + f) + 4 * z;
An indentation standard using spaces never was achieved. Some people like 2 spaces, some prefer 4. Better use tabs. Tab characters have some advantages. Everyone can set their own preferred indentation level. If you want to increase the indentation (or decrease), mark one block and increase the indent level with Tab and with Shift-Tab you decrease the indentation.
Here, we define the Tab as the standard indentation character.
Do not use spaces for indentation - use tabs!
Note: Even though it’s possible to set the wanted indention level for the tab character to a preferred value this should be used with caution It is recommended to keep the default setting of 4.
Blank lines improve readability. They set off blocks of code which are in themselves logically related.
Two blank lines should always be used between:
One blank line should always be used between:
There should be a single space after a comma or a semicolon, for example:
TestMethod(a, b, c); //OK TestMethod(a,b,c); //Avoid
or
TestMethod( a, b, c ); //Avoid
Single spaces surround operators (except unary operators like increment or logical not), example:
a = b; //OK a=b; //Avoid for(int i = 0; i < 10; ++i) //OK for(int i=0;i < 10;++i) //Avoid
Sometimes it is more readable to format a logical block of lines as a table:
string name = "My Name"; int value = 1; Time moment = DateTime.Now;
Note: Use spaces for the table like formatting and not tabs because the table formatting may look strange in special tab intent levels.