Formatting

Whitespace and Symbols

Blank Lines

In the following list, the phrase “surrounding code” refers to a line consisting of more than just an opening or closing brace. That is, no new line is required when an element is at the beginning or end of a methods or other block-level element.
Always place an empty line in the following places:

  • Between the file header and the namespace declaration or the first using statement.
  • Between the last using statement and the namespace declaration.
  • Between types (classes, structs, interfaces, delegates or enums).
  • Between public, protected and internal members.
  • Between preconditions and ensuing code.
  • Between post-conditions and preceding code.
  • Between a call to a base method and ensuing code.
  • Between return statements and surrounding code.
  • Between block constructs (e.g. while loops or switch statements) and surrounding code.
  • Between documented enum values; undocumented values may be grouped together.
  • Between logical groups of code in a method; this notion is subjective and more a matter of style. You should use empty lines to improve readability, but should not overuse them.
  • Between the last line of code in a block and a comment for the next block of code.
  • Between statements that are broken up into multiple lines.
  • Between a #region tag and the first line of code in that region. See next section.
  • Between the last line of code in a region and the #endregion tag. See next section.

Do not place an empty line in the following places:

  • After another empty line.
  • Between retrieval code and handling for that code. Instead, they should be formatted together.

    1. IMetaReadableObject obj = context.Find<IMetaReadableObject>();
    2. if (obj == null)
    3. {
    4. context.Recorder.Log(Level.Fatal, String.Format("Error!"));
    5. return null;
    6. }
  • Between any line and a line that has only an opening or closing brace on it (i.e. there should be no leading or trailing newlines in a block).
  • Between undocumented fields (usually private); if there are many such fields, you may use empty lines to group them logically.

Line Breaks

  • Use line-breaking only when necessary, as outlined below.
  • No line should exceed 120 characters.
  • Use as few line-breaks as possible.
  • Line-breaking should occur at natural boundaries; the most common such boundary is between parameters in a method call or definition.
  • A line-break at a boundary that defines a new block should be indented one more level.
  • A line-break at any other boundary should be indented at the same level as the original line.
  • The separator (e.g. a comma) between elements formatted onto multiple lines goes on the same line after the element. The IDE is much more helpful when formatting that way.
  • The most natural line-breaking boundary is often before and after a list of elements. For example, the following method call has line-breaks at the beginning and end of the parameter list.
    1. people.DataSource = CurrentCompany.Employees.GetList(
    2. connection, metaClass, GetFilter(), null
    3. );
  • If one of the parameters is much longer, then you add line-breaking between the parameters; in that case, all parameters are formatted onto their own lines:
    1. people.DataSource = CurrentCompany.Employees.GetList(
    2. connection,
    3. metaClass,
    4. GetFilter("Global.Applications.Updater.PersonList.Search"),
    5. null
    6. );
  • Note in the examples above that the parameters are indented. If the assignment or method call was longer, they would no longer fit on the same line. In that case, you should use two levels of indenting.
    1. Application.Model.people.DataSource =
    2. Global.ApplicationEnvironment.CurrentCompany.Employees.GetList(
    3. connection,
    4. metaClass,
    5. GetFilter("Global.Applications.Updater.PersonList.Search"),
    6. null
    7. );
  • Even if there is a logical grouping for parameters, you should still apply line-breaking using the all-on-one-line or each-on-its-own-line rules stated above. For example, the following method specifying Cartesian coordinates feels natural, but is not well-supported by automatic formatting rules:
    1. Geometry.PlotInBox(
    2. "Global.Applications.MainWindow",
    3. topLeft.X, topLeft.Y,
    4. bottomRight.X, bottomRight.Y
    5. );
    As nice as it might look, do not use this line-breaking technique.

Indenting and Spacing

  • An indent is two spaces.
  • Use a single space after a comma (e.g. between function arguments).
  • There is no space after the leading parenthesis/bracket or before the closing parenthesis/bracket.
  • There is no space between a method name and the leading parenthesis, but there is a space before the leading parenthesis of a flow-control statement.
  • Use a single space to surround all infix operators; there is no space between a prefix operator (e.g. “-” or “!”) and its single parameter.
  • Do not use spacing to align type members on the same column (e.g. as with the explicitly assigned values for members of an enumerated type).

Braces

  • Curly braces should—with a few exceptions outlined below—go on their own line.
  • A line with only an opening brace should never be followed by an empty line.
  • A line with only a closing brace should never be preceded by an empty line.

Properties

  • Simple getters and setters should go on the same line as all brackets.
  • Abstract properties should have get, set and all braces on the same line.
  • Complex getters and setters should have each bracket on its own line.
  • Place Auto-Property Initializers on the same line.
    1. public int Maximum { get; } = 45;

Methods

  • Completely empty methods should have brackets placed on separate lines:
    1. SomeClass(string name)
    2. : base(name)
    3. {
    4. }

Parentheses

  • Use parentheses only to improve clarity.
  • Do not use parentheses for simple expressions. The following expression is clear enough without extra parentheses.
    1. if (context != null && context.Count > 0)
    2. {
    3. }

Language Elements

Methods

Definitions

  • Stay consistent with line-breaking in related methods within a class; if one is broken up onto multiple lines, then all related methods should be broken up onto multiple lines.
  • The closing brace of a method definition goes on the same line as the last parameter (unlike method calls). This avoids having a line with a solitary closing parenthesis followed by a line with a solitary opening brace.
    1. public static void SetupLookupDefinition(
    2. RepositoryItemLookUpEdit lookupOptions,
    3. IMetaClass metaClass)
    4. {
    5. // Implementation...
    6. }
  • Generic method constraints should always be on their own line, with a single indent.
    1. string GetNames<T>(IMetaCollection<T> elements, string separator, NameOption options
    2. where T : IMetaBase;
  • The indent for a generic-method constraint stays the same, even with wrapped parameters.
    1. public static void SetupLookupFromData<T>(
    2. RepositoryItemLookUpEdit lookupOptions,
    3. IDataList<T> dataList)
    4. where T : IMetaReadable
    5. {
    6. SetupLookupFromData<T>(lookupOptions, dataList, dataList.MetaClass);
    7. }

Calls

  • The closing parenthesis of a method call goes on its own line to “close” the block (see example below).
    1. result.Messages.Log(
    2. Level.Error,
    3. String.Format(
    4. "Class [{0}] has the same name as class [{1}].",
    5. dbCls.Identifier,
    6. classMap[cls.MetaId]
    7. )
    8. );
  • If the result of calling a method is assigned to a variable, the call may be on the same line as the assignment if it fits.
    1. people.DataSource = CurrentCompany.Employees.GetList(
    2. connection,
    3. ViewAspectTools.GetViewableWrapper(cls),
    4. GetFilter().Where(String.Format(“PersonId = {0}”, personId)),
    5. null
    6. );
  • If the call does not fit easily—or if the method call is “too far away” from the ensuing parameters—you should move the call to its own line and indent it:
    1. WindowTools.GetActiveWindow().GetActivePanel().GetActiveList().DataSource =
    2. CurrentCompany.Organization.MainOffice.Employees.GetList(
    3. connection,
    4. ViewAspectTools.GetViewableWrapper(cls),
    5. GetFilter().Where(String.Format(“PersonId = {0}”, personId)),
    6. null
    7. );

Chaining

  • Chained method calls can be formatted onto multiple lines; if one chained method-call is formatted onto its own line, then they must all be on separate lines.
    1. string contents = header
    2. .Replace("{Year}", DateTime.Now.Year.ToString())
    3. .Replace("{User}", "ENCODO")
    4. .Replace("{DateTime}", DateTime.Now.ToString());
  • If a line of a chained method-call opens a new logical context, then ensuing lines should be indented to indicate this. For example, the following example joins tables together, with the last three statements applied to the last joined table. The indenting helps make this clear.
    1. query
    2. .Join(Settings.Relations.Company)
    3. .Join(Company.Relations.Office)
    4. .Join(Office.Relations.Employees)
    5. .WhereEquals(Employee.Fields.Id, employee.Id)
    6. .OrderBy(Employee.Fields.LastName, SortDirection.Ascending)
    7. .OrderBy(Employee.Fields.FirstName, SortDirection.Ascending);

Constructors

  • Base constructors should be on a separate line, indented one level.
    1. public class B : A
    2. {
    3. B(string name)
    4. : base(name)
    5. {
    6. }
    7. }

Initializers

  • Don’t add a trailing comma for the last element.

Object Initializers

Longer initialization blocks should go on their own line; the rest can be formatted in the following ways (depending on line-length and preference):

  • Shorter initialization blocks (one or two properties) can be specified on the same line:
    1. var personOne = new Person { LastName = "Miller", FirstName = "John" };
  • The new-clause is on the same line as the declaration:

    1. var sizeAspect = new ViewPropertySizeAspect
    2. {
    3. VerticalSizeMode = SizeMode.Absolute,
    4. VerticalUnits = height
    5. };
    6. prop.Aspects.Add(sizeAspect);
  • The new-clause is on its own line:

    1. var sizeAspect =
    2. new ViewPropertySizeAspect
    3. {
    4. VerticalSizeMode = SizeMode.Absolute,
    5. VerticalUnits = height
    6. };
    7. prop.Aspects.Add(sizeAspect);
  • The initializer is nested within the method-call on the same line as the declaration:
    1. prop.Aspects.Add(new ViewPropertySizeAspect { VerticalUnits = height });
  • The initializer is nested within the method-call on its own line:
    1. prop.Aspects.Add(
    2. new ViewPropertySizeAspect
    3. {
    4. VerticalSizeMode = SizeMode.Absolute,
    5. VerticalUnits = height
    6. });
  • Putting the new operator on the same line is also fine, but then you shouldn’t indent the braces.
    1. prop.Aspects.Add(new ViewPropertySizeAspect
    2. {
    3. VerticalSizeMode = SizeMode.Absolute,
    4. VerticalUnits = height
    5. });

If the initializer goes spans multiple lines, then the new-clause must also go on its own line.

Array Initializers

The type is usually optional (unless you’re initializing an empty array), so you should leave it empty.

Lambdas

  • Do not use anonymous delegates; use lambda notation instead.
  • Do not use parentheses around a single parameter in a lambda expression.
  • All rules for standard method calls also apply to method calls with lambdas.
  • Longer lambdas should be written with an indent, as follows:

    1. var persistentProperties =
    2. model.ReferencedProperties.FindAll(
    3. prop =>
    4. {
    5. // More code...
    6. return prop.Persistent;
    7. }
    8. );
  • Short lambdas benefit can just be inlined:
    1. public string[] Keys
    2. {
    3. get
    4. {
    5. return ToStrings(i => i.Name);
    6. }
    7. }
    Also OK, since the get body is short:
    1. public string[] Keys
    2. {
    3. get { return ToStrings(i => i.Name); }
    4. }
    Even better, use expression-bodied members:
    1. public string[] Keys => ToStrings(i => i.Name);
  • Short lambdas are just parameters; treat them as you would any other parameters:
    1. _context = new DataContext(
    2. Settings.Default.ConfigFileName,
    3. DatabaseType.PostgreSql,
    4. () => ModelGenerator.CreateModel()
    5. );
    In the example above each parameter is on its own line, as required.
  • Keep parameters short in constructor bases.
    1. public Application()
    2. : base(DatabaseType.PostgreSql, () => ModelGenerator.CreateModel())
    3. {
    4. }
  • All rules for standard method calls also apply to method calls with lambda expressions.
  • Very short lambda expressions may be written as a simple parameter on the same line as the method call:
    1. ReportError(msg => MessageBox.Show(msg));
  • Longer lambda expressions should go on their own line, with the closing parenthesis of the method call closing the block on another line. Any calls attached to the result—like ToList() or Count()—should go on the same line as the closing parenthesis.
    1. people.DataSource = CurrentCompany.Employees.Where(
    2. item => item.LessonTimeId == null
    3. ).ToList();
  • Longer lambda expressions should not be both wrapped and used in a foreach-statement; instead, use two statements as shown below. Use short parameter names in lambdas since they are used very close to their declaration (by definition).

    1. var appointmentsForDates = data.Appointments.FindAll(
    2. a => a.StartTime >= startDate && a.EndTime <= endDate
    3. );
    4. foreach (var appointment in appointmentsForDates)
    5. {
    6. // Do something with each appointment
    7. }

Multi-Line Text

  • Longer string-formatting statements with newlines should be formatted using the verbatim strings (@"") and should avoid using concatenation:
    1. result.SqlText = String.Format(
    2. @"FROM person
    3. LEFT JOIN
    4. employee
    5. ON person.employee_id = employee.id
    6. LEFT JOIN
    7. company
    8. ON person.company_id = company.id
    9. LEFT JOIN
    10. program
    11. ON company.program_id = program.id
    12. LEFT JOIN
    13. settings
    14. ON settings.program_id = program.id
    15. WHERE
    16. program.id = {0} AND person.hire_date <= '{2}';
    17. ",
    18. settings.ProgramId,
    19. state,
    20. offset.ToString("yyyy-MM-dd")
    21. );
  • If the indenting in the string argument above is important, you may break indenting rules and place the text all the way to the left of the source in order to avoid picking up extra, unwanted spaces. However, you should consider externalizing such text to resources or text files.
  • The trailing double-quote in the example above is not required, but is permitted; in this case, the code needs to include a newline at the end of the SQL statement.

return Statements

  • If a return statement is not the only statement in a method, it should be separated from other code by a single newline.
  • Always use multi-line formatting for return statements so they’re easy to see.
    1. if (Count != other.Count)
    2. {
    3. return false;
    4. }
  • Do not use else with return statements.

    1. if (a == 1)
    2. {
    3. return true;
    4. }
    5. else // Not necessary
    6. {
    7. return false;
    8. }

    Instead, you should write the return as shown below.

    1. if (a == 1)
    2. {
    3. return true;
    4. }
    5. return false;

    In this case, return the condition instead.

    1. return a == 1;

switch Statements

The following rules apply for all switch statements, including pattern-matching.

  • Contents under switch statements should be indented.
  • Braces for a case-label are not indented; this maintains a nice alignment with the brackets from the switch-statement.
  • Use braces for longer code blocks under case-labels; leave a blank line above the break-statement to improve clarity.

    1. switch (flavor)
    2. {
    3. case Flavor.Up:
    4. case Flavor.Down:
    5. {
    6. if (someConditionHolds)
    7. {
    8. // Do some work
    9. }
    10. // Do some more work
    11. break;
    12. }
    13. default:
    14. break;
    15. }
  • Use braces to enforce tighter scoping for local variables used for only one case-label.
    1. switch (flavor)
    2. {
    3. case Flavor.Up:
    4. case Flavor.Down:
    5. {
    6. int quarkIndex = 0; // valid within scope of case statement
    7. break;
    8. }
    9. case Flavor.Charm:
    10. case Flavor.Strange:
    11. int isTopOrBottom = false; // valid within scope of switch statement
    12. break;
    13. default:
    14. break;
    15. }
  • If brackets are used for a case-label, the break-statement should go inside those brackets so that the bracket provides some white-space from the next case-label.
    1. switch (flavor)
    2. {
    3. case Flavor.Up:
    4. case Flavor.Down:
    5. {
    6. int quarkIndex = 0;
    7. break;
    8. }
    9. case Flavor.Charm:
    10. case Flavor.Strange:
    11. {
    12. int isTopOrBottom = false;
    13. break;
    14. }
    15. default:
    16. {
    17. handled = false;
    18. break;
    19. }
    20. }

Ternary and Coalescing Operators

  • Do not use line-breaking to format statements containing ternary and coalescing operators; instead, convert to an if/else statement.
  • Do not use parentheses around the condition of a ternary expression. If the condition is not immediately recognizable, extract it to a local variable.
    1. return _value != null ? _value.ToString() : "NULL";
  • Prefix operators (e.g. !) and method calls should not have parentheses around them.
    1. return !HasValue ? Value.ToString() : "EMPTY";

Comments

  • Place comments above referenced code.
  • Indent comments at the same level as referenced code.

Regions

  • Do not use regions.
  • A historical usage of #region tags is to delineate code regions to be ignored by ReSharper in generated files. Instead, tell ReSharper to ignore files with the pattern of the generated filenames (e.g. *.Class.cs).