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

High-Quality Automated Tests - Top StyleCop Coding Styles: Part 3

DZone's Guide to

High-Quality Automated Tests - Top StyleCop Coding Styles: Part 3

This series shows by example the StyleCop rules on how to write better automated tests for your software.

· DevOps Zone ·
Free Resource

The need for DevOps innovation has never been greater. Get the results from over 100 business value assessments in this whitepaper, Digital Darwinism: Driving Digital Transformation, to see the positive impact of DevOps first hand.

This is the next publication from the High-Quality Automated Tests series. In Part 1, I showed you how to apply coding standards and styles in you .NET tests using StyleCop. This will be the third and last part of the mini-series dedicated to StyleCop coding styles. If you haven't, please check the and second part.

1. SA1119 Statements Must Not Use Unnecessary Parentheses

Correct

int xTarget = 5 + b;
string yTarget = this.GetTargetCoordinates().ToString();
return xTarget.FuelNeeded;

Incorrect

int xTarget = (5 + b);
string yTarget = (this.GetTargetCoordinates()).ToString();
return (xTarget.FuelNeeded);

"It is possible in C# to insert parenthesis around virtually any type of expression, statement, or clause, and in many situations use of parenthesis can greatly improve the readability of the code. However, excessive use of parenthesis can have the opposite effect, making it more difficult to read and maintain the code."

2. SA1117 Parameters Must Be on the Same Line or Separate Lines

Correct

public string LandMoonPioneer(int x, int y, double fuelToSave)
{
}
public string LandMoonPioneer(
    int x, int y, double fuelToSave)
{
}
public string LandMoonPioneer(
    int x,
    int y, 
    double fuelToSave)
{
}

Incorrect

public string LandMoonPioneer(int x, int y, 
     double fuelToSave)
{
}


3. SA1113 Comma Must Be on the Same Line as the Previous Parameter

Correct

public bool LandMoonPioneer(int x, int y, double fuelToSave)
{
    bool isLandingSuccessfull = TryToLand(
    x,
    y);
}

Incorrect

public bool LandMoonPioneer(int x, int y, double fuelToSave)
{
    bool isLandingSuccessfull = TryToLand(
    x,
    y
    );
}


4. SA1111 Closing Parenthesis Must Be on the Line of the Last Parameter

Correct

int xTarget = 5 + b;
string yTarget = GetTargetCoordinates().ToString();

Incorrect

int xTarget = 5 + b; string yTarget = GetTargetCoordinates().ToString();


5. SA1107 Code Must Not Contain Multiple Statements on One Line

Correct

string newMoonName = CreateNewMoonName("Jupiter", "Europa New");

Incorrect

string newMoonName = base.CreateNewMoonName("Jupiter", "Europa New");

"A violation of this rule occurs whenever the code contains a call to a member from the base class prefixed with base. And there no local implementation of the member."

6. SA1100 Do Not Prefix Calls With Base Unless Local Implementation Exists

Correct

public class JupiterMoonFactory<TPlanetType> : MoonFactory
{
    public JupiterMoonFactory(int size) : base(size)
    {
    }
}

Incorrect

public class JupiterMoonFactory<TPlanetType>: MoonFactory
{
    public JupiterMoonFactory(int size) :base(size)
    {
    }
}


7. SA1024 Colons Must Be Spaced Correctly

Correct

private Location secretMilitarySpaceBase = GetLocation("Space Zebra 2");

Incorrect

Location secretMilitarySpaceBase = GetLocation("Space Zebra 2");

This removes the need for the reader to make assumptions about the code, improving the readability of the code.

8. SA1400 Access Modifier Must Be Declared

Correct

public class JupiterMoonFactory<TPlanetType> : MoonFactory
{
    public JupiterMoonFactory(int size) : base(size)
    {
    }
}

Incorrect

public class JupiterMoonFactory<TPlanetType> : MoonFactory
{
    public JupiterMoonFactory(int Size) : base(Size)
    {
    }
}


9. SA1313 Parameter Names Must Begin With a Lowercase Letter

Correct

bool isPetrolOnMoon = LocateResources(ResourcesType.Petrol);
if (isTherePetrolOnMoon)
{
    // go there...
}

Incorrect

bool IsPetrolOnMoon = LocateResources(ResourcesType.Petrol);
if (IsPetrolOnMoon)
{
    // go there...
}


10. SX1309 Field Names Must Begin With an Underscore

Correct

private Location _secretMilitarySpaceBase = new Location(107, 45);

Incorrect

private Location secretMilitarySpaceBase = new Location(107, 45);

We will use _camelCase for internal and private fields and use readonly where possible. Prefix instances with _, static fields with s_, and thread static fields with t_. When use on static fields, readonly should come after static (i.e. static readonly not readonly static).

11. SA1304 Non-Private Readonly Fields Must Begin With an Uppercase Letter

Correct

private readonly Location SecretMilitarySpaceBase = new Location(107, 45);

Incorrect

private readonly Location secretMilitarySpaceBase = new Location(107, 45);


12. SA1303 Const Field Names Must Begin With an Uppercase Letter

Correct

public const string SecretSaturnBaseName = "Titan Chocolate Corns 2.1";

Incorrect

public const string secretSaturnBaseName = "Titan Chocolate Corns 2.1";
public const string SECRET_SATURN_BASE_NAME = "Titan Chocolate Corns 2.1";

We use PascalCasing to name all our constant local variables and fields. The only exception is for interop code where the constant value should exactly match the name and value of the code you are calling via interop.

Summary

In the last three articles, I presented to you more than 30 StyleCop coding styles. In the next publications from the series, I will share with you my list of must-use ReSharper coding styles.

Interested in Kubernetes but unsure where to start? Check out this whitepaper, A Roundup of Managed Kubernetes Platforms from Codeship by Cloudbees, for an overview and comparison of Kubernetes platforms. 

Topics:
devops ,automated testing ,test automation

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}