This is the third article from the new series- High-Quality Automated Tests. In the first publication, I showed you how to apply coding standards and styles in your .NET tests using EditorConfig. This is the second part of the list of the coding styles you need to use. If you haven’t read the first part, I suggest doing so.
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.
11. Style Expression Bodied Properties
CORRECT
// csharp_style_expression_bodied_properties = true:error
public int PlanetsCount => 42;
INCORRECT
// csharp_style_expression_bodied_properties = false:error
public int MoonsCount
{
get { return 13; }
}
12. Style Throw Expression
CORRECT
// csharp_style_throw_expression = true:error
FuelType = fuelType ?? throw new ArgumentNullException(nameof(FuelType));
INCORRECT
// csharp_style_throw_expression = false:error
if (fuelType == null)
{
throw new ArgumentNullException(nameof(fuelType));
}
this.FuelType = fuelType;
13. Style var Elsewhere
CORRECT
// csharp_style_var_elsewhere = true:error
int moonSize = CalculateMoonSize();
INCORRECT
// csharp_style_var_elsewhere = false:error
var secondMoonSize = CalculateMoonSize();
14. Style var for Built in Types
CORRECT
// csharp_style_var_for_built_in_types = true:error
var planetSize = 42;
INCORRECT
// csharp_style_var_for_built_in_types = false:error
int secondPlanetSize = 42;
15. Style var When Type Is Apparent
CORRECT
// csharp_style_var_when_type_is_apparent = true:error
var robots = new List<int>();
INCORRECT
// csharp_style_var_when_type_is_apparent = false:error
List<int> robots = new List<int>();
16. Sort System Directives First
CORRECT
// dotnet_sort_system_directives_first = true
using System;
using System.Collections.Generic;
INCORRECT
// dotnet_sort_system_directives_first = false
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
17. Style Coalesce Expression
CORRECT
// dotnet_style_coalesce_expression = true:error
int? sunSize = null;
var blackHole = sunSize ?? 10000;
INCORRECT
// dotnet_style_coalesce_expression = false:error
var secondBlackHole = sunSize != null ? sunSize : 10000;
18. Style Collection Initializer
CORRECT
// dotnet_style_collection_initializer = true:error
var shieldsPower = new List<int> { 42, 13 };
INCORRECT
// dotnet_style_collection_initializer = false:error
var shieldsPower = new List<int>();
shieldsPower.Add(42);
shieldsPower.Add(13);
19. Style null Propagation
CORRECT
// dotnet_style_null_propagation = true:error
var smallSpaceShip = spaceShipFactory?.Build();
INCORRECT
// dotnet_style_null_propagation = false:error
var smallSpaceShip = spaceShipFactory == null ? null : spaceShipFactory.Build();
20. Style Object Initializer
CORRECT
// dotnet_style_object_initializer = true:error
var bigSpaceShip = new SpaceShip
{
Name = "Meissa"
};
INCORRECT
// dotnet_style_object_initializer = false:error
var bigSpaceShip = new SpaceShip();
bigSpaceShip.Name = "Meissa";
Summary
I presented to you 20 of my favorites EditorConfig coding styles. In the next articles of the series, I will show you another tool for coding standards called StyleCop. I will again share my must-use rules in a couple of articles.
