High-Quality Automated Tests — Top 10 ReSharper Coding Styles
High-Quality Automated Tests — Top 10 ReSharper Coding Styles
These code examples will help you write better automated tests for .NET applications using the coding conventions of the ReSharper style.
Join the DZone community and get the full member experience.
Join For FreeLearn how integrating security into DevOps to deliver "DevSecOps" requires changing mindsets, processes and technology.
This is the next article from the High-Quality Automated Tests series. In the last publications, I showed you how you could apply coding styles through two popular technologies- EditorConfig and StyleCop. This one will be dedicated to another favorite tool among .NET community- ReSharper. It should be noted that it is not free, however since many people use it any way I think it worth mentioning how it can help you in the battle with the inconsistent ugly code.
File Layout
One neat feature of ReSharper is that it can check whether the order of your properties, fields, methods, events, etc. is correct. If you place your methods above your constructors, a warning will be displayed.
Naming Style
Another feature that you can utilize is the naming rules. If some of your class members don't follow them a warning will be displayed.
Prefer Wrap After "(" in Declaration
Correct
void LandMarsRover(
int x,
int y,
int z,
double fuelNeeded)
{
}
Incorrect
void LandMarsRover(int x,
int y,
int z,
double fuelNeeded)
{
}
Prefer Wrap After "(" in Invocation
Correct
LandMarsRover(
int x,
int y,
int z,
double fuelNeeded);
Incorrect
LandMarsRover(int x,
int y,
int z,
double fuelNeeded);
Spaces Before Parentheses "if"
Correct
if (isRoverLandedSuccessfully)
{
Celebrate();
}
else
{
SendAnotherRover();
}
Incorrect
if(isRoverLandedSuccessfully)
{
Celebrate();
}
else
{
PlaySadSong();
}
The same applies for while, catch, switch, for, foreach, using, lock, and fixed.
Spaces Around Addictive Operators
Correct
totalFuel = engineFuel + upperRocket - upperBathroomFuel;
Incorrect
totalFuel = engineFuel+upperRocket-upperBathroomFuel;
The same applies to assignments, logical operators, equality, etc.
Spaces After Comma Method Invocation
Correct
LaunchSatellite(_xSky, _ySky, _fuel);
Incorrect
LaunchSatellite(_xSky,_ySky,_fuel);
Spaces Around Lambda Arrow
Correct
Action calculateMoonBaseFuel = rover => roverFuel * 4.5;
Incorrect
Action calculateMoonBaseFuel = rover=>roverFuel * 4.5;
Align First Call Arguments With "("
Correct
LandMarsRover(
int x,
int y,
int z,
double fuelNeeded);
Incorrect
LandMarsRover(
int x,
int y,
int z,
double fuelNeeded);
Use "String.Empty" Instead of "" Literal
Correct
private string secretJupiterBase = string.Empty;
Incorrect
private string secretJupiterBase = "";
Learn how enterprises are using tools to automate security in their DevOps toolchain with these DevSecOps Reference Architectures.
Published at DZone with permission of Anton Angelov , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}