Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

High-Quality Automated Tests — Top 10 ReSharper Coding Styles

DZone's Guide to

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.

· DevOps Zone ·
Free Resource

DevOps involves integrating development, testing, deployment and release cycles into a collaborative process. Learn more about the 4 steps to an effective DevSecOps infrastructure.

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.

ReSharper File Layout

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.

ReSharper Naming Style

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.

ReSharper Additive Operators


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 = "";


Read the 4-part DevOps testing eBook to learn how to detect problems earlier in your DevOps testing processes.

Topics:
devops ,automated testing ,tutorial ,resharper

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}