This is the second article from the new series- High-Quality Automated Tests. In the previous publication, I showed you how to apply coding standards and styles in your .NET tests using EditorConfig. In this one, I am going to share with you some of the coding styles that you need to use. Initially, my idea was to publish all of them in a single post, but since there are a lot, I decided to split them into two parts.
While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research while I was working on a similar feature for our solution.
1. Indent Case Content
CORRECT
bool isEarthRound = false;
// csharp_indent_block_contents = true
if (isEarthRound)
{
var answerOfUniverse = 42;
}
INCORRECT
// csharp_indent_block_contents = false
if (isEarthRound)
{
var answerOfUniverse = 42;
}
2. Indent Block Contents
CORRECT
var answerOfEverything = 42;
// csharp_indent_case_contents = true
switch (answerOfEverything)
{
case 42:
break;
case 13:
break;
}
INCORRECT
var answerOfEverything = 42;
// csharp_indent_case_contents = false
switch (answerOfEverything)
{
case 42:
break;
case 13:
break;
}
3. Prefer Braces
CORRECT
// csharp_prefer_braces = true:error
if (isEarthRound)
{
return;
}
INCORRECT
// csharp_prefer_braces = false:error
if (isEarthRound)
return;
4. Space After Keywords in Control Flow Statements
CORRECT
// csharp_space_after_keywords_in_control_flow_statements = true
if (isEarthRound)
{
}
while (isEarthRound)
{
}
INCORRECT
// csharp_space_after_keywords_in_control_flow_statements = false
if (isEarthRound)
{
}
while (isEarthRound)
{
}
5. Space before Colon in Inheritance Clause
CORRECT
// csharp_space_before_colon_in_inheritance_clause = true
public class SpaceShipOne : Rocket
INCORRECT
// csharp_space_before_colon_in_inheritance_clause = false
public class SpaceShipOne : Rocket
6. Style Conditional Delegate Call
CORRECT
// csharp_style_conditional_delegate_call = true:error
spaceshipOne?.Invoke("98");
INCORRECT
// csharp_style_conditional_delegate_call = false:error
if (spaceshipOne != null)
{
spaceshipOne("98");
}
7. Style Expression Bodied Accessors
CORRECT
// csharp_style_expression_bodied_accessors = true:error
private int? fuelType;
public int? FuelType
{
get => this.fuelType;
set => this.fuelType = value;
}
INCORRECT
// csharp_style_expression_bodied_accessors = false:error
private int fuelType1;
public int FuelType1
{
get { return this.fuelType1; }
set { this.fuelType1 = value; }
}
8. Style Expression Bodied Constructors
CORRECT
// csharp_style_expression_bodied_constructors = true:error
public SampleRulesCode() => FuelType = 100;
INCORRECT
// csharp_style_expression_bodied_constructors = false:error
public SampleRulesCode()
{
FuelType = 100;
}
9. Style Expression Bodied Indexers
CORRECT
// csharp_style_expression_bodied_indexers = false:error
public int this
{
get { return 42; }
}
INCORRECT
// csharp_style_expression_bodied_indexers = false:error
public int this
{
get { return 42; }
}
10. Style Expression Bodied Methods
CORRECT
// csharp_style_expression_bodied_methods = true:error
public int CalculateAnswerOfEverything() => 42;
INCORRECT
// csharp_style_expression_bodied_methods = false:error
public int CalculateAnswerOfEverything()
{
return 42;
}
Summary
I presented to you 10 of my favorites EditorConfig coding styles. Since I have at least ten more, I will create a second part.
