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

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

DZone's Guide to

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

This series continues to provide examples of rules for writing good automated tests for .NET with the StyleCop coding standards.

· DevOps Zone ·
Free Resource

Easily enforce open source policies in real time and reduce MTTRs from six weeks to six seconds with the Sonatype Nexus Platform. See for yourself - Free Vulnerability Scanner. 

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

1. SA1203 Constants Must Appear Before Fields

Correct

private const string warningMessage = "Beware Evil Cookie Robots behind the Door!!";
private int fuelQuanlity;

Incorrect

private int fuelQuanlity;
private const string warningMessage = "Beware Evil Cookie Robots behind the Door!!";


2. SA1131 Use Readable Conditions

Correct

public void ShouldShootTheMissile(string missileType)
{
    if (missileType == null)
    {
        throw new ArgumentNullException(nameof(missileType));
    }
}

Incorrect

public void ShouldShootTheMissile(string missileType)
{
    if (null == missileType)
    {
        throw new ArgumentNullException(nameof(missileType));
    }
}


3. SA1130 Use Lambda Syntax

Correct

Action calculateTargetX = () => { x = 0; };
Action calculateTargetY = () => { y = 0; };
Func<int, int, int> calculateDefense = (m, n) => m + n;

Incorrect

Action calculateTargetX = delegate { x = 0; };
Action calculateTargetY = delegate() { y = 0; };
Func<int, int, int> calculateDefence = delegate(int m, int n) { return m + n; };


4. SA1028 Code Must Not Contain Trailing Whitespaces

A violation of this rule occurs when a line of code ends with any form of whitespace other than the end of line character(s). Extra whitespace can show up in editors with visible whitespace, or cause unnecessary code churn when developers format the document such that trailing whitespace is removed later. Some diff tools even highlight trailing whitespace to encourage developers to trim it.

The C# language contains several constructs which span multiple lines of codes. The following table describes the behavior of this rule with respect to each of these constructs:

Image title

Image title


5. SA1128 Constructor Initializer Must Be on Own Line

Correct

public class RobotGardner
{
    public RobotGardner() 
        : this(30)
    {
    }
     public RobotGardner() 
         : base(50)
    {
    }
}

Incorrect

public class RobotGardner
{
    public RobotGardner() : this(30)
    {
    }
     public RobotGardner() : base(50)
    {
    }
}


6. SA1127 Generic Type Constraints Must Be on Own Line

Correct

private void CleanMoonDust<TRockType, TTool>()
    where TRockType : class
    where TTool : class, new()
{
}

Incorrect

private void CleanMoonDust<TRockType, TTool>() where TRockType : class where TTool : class, new()
{
}


7. SA1125 Use Shorthand for Nullable Types

Correct

private DateTime? launchStart;

Incorrect

private Nullable<DateTime> launchStart;


8. SA1124 Do Not Use Regions

Correct

private DateTime? landingTime;

Incorrect

#region Variables
private DateTime? landingTime;
#endregion

"In many editors, including Visual Studio, the region will appear collapsed by default, hiding the code within the region. It is generally a bad practice to hide code, as this can lead to bad decisions as the code is maintained over time."

Image title


9. SA1120 Comments Must Contain Text

Correct

private DateTime? archiveOldMoonBase; // maybe we should go there and clean the kitchen

Incorrect

private DateTime? archiveOldMoonBase; //


Summary

I presented to you 9 of my favorites StyleCop coding styles. Since I have at least 15 more, you will find them in the next article from the series.

Automate open source governance at scale across the entire software supply chain with the Nexus Platform. Learn more.

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