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

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

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


Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.

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 }}