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

High-Quality Automated Tests - Top 9 StyleCop Coding Styles: Part 1

DZone's Guide to

High-Quality Automated Tests - Top 9 StyleCop Coding Styles: Part 1

In this series on automated testing techniques, you'll find the first of a long list of tips to make sure your tests are clean and efficient.

· DevOps Zone ·
Free Resource

With the influx of DevOps-related products and services on the market, today’s application delivery toolchain has become complex and fragmented. Watch Avoiding the DevOps Tax to learn best practices for integration and automation to realize a faster DevOps lifecycle.

This is the new article from the High-Quality Automated Tests series. In the last publication, I showed you how to apply coding standards and styles in you.NET tests using StyleCop. In this one, I am going to share with you some of the coding styles that you need to use. Initially, my idea was to publish all of them in a single post, but since there are a lot, I decided to split them into three parts.

1. SA1519 Braces Must Not Be Omitted From Multi-Line Child Statements

Correct

if (true)
{
    return this.value;
}


Incorrect

if (true)
    return this.value;


Although this is legal in C#, StyleCop requires the braces to be present when the statement spans multiple lines, to increase the readability and maintainability of the code.

Since this is like a religious war between different programmers. I did additional research. Below you can find most up-voted answers in the most popular programming forums and groups.

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

StackOverflow Comments Quality Code

2. SA1515 Single Line Comment Must Be Preceded by Blank Line

Correct

public bool Enabled
{
    get 
    {
        Console.WriteLine("Getting the enabled flag.");
        
        // Return the value of the 'enabled' field.
        return this.enabled;  
    }
}
public bool Enabled
{
    get 
    {
        Console.WriteLine("Getting the enabled flag.");
        ////return false;
        return this.enabled;  
    }
}


To improve the readability of the code, StyleCop requires blank lines in certain situations, and prohibits blank lines in other situations. This results in a consistent visual pattern across the code, which can improve recognition and readability of unfamiliar code.

Incorrect

public bool Enabled
{
    get 
    {
        Console.WriteLine("Getting the enabled flag.");
        // Return the value of the 'enabled' field.
        return this.enabled;  
    }
}


3. SA1513 Closing Brace Must Be Followed by Blank Line

Correct

if (AmIHungry)
{
    MakePancakes();
}
return pancakesList;


Incorrect

if (AmIHungry)
{
    MakePancakes();
}
return pancakesList;


4. SA1507 Code Must Not Contain Multiple Blank Lines in a Row

Correct

public bool ShouldBuyTheMoon
{
    get 
    { 
        Console.WriteLine("TODO: Should check the stocks after 1 month...");
        return this.shouldBuyTheMoon; 
    }
}


Incorrect

public bool ShouldBuyTheMoon
{
    get 
    { 
        Console.WriteLine("TODO: Should check the stocks after 1 month...");
        
        return this.shouldBuyTheMoon; 
    }
}


"To improve the readability of the code, StyleCop requires blank lines in certain situations, and prohibits blank lines in other situations. This results in a consistent visual pattern across the code, which can improve recognition and readability of unfamiliar code."

5. SA1502 Element Must Not Be On Single Line

Correct

public object DefuseTheRockets()
{
    return null; 
}


Incorrect

public object DefuseTheRockets() { return null; }


6. SA1500 Braces for Multi Line Statements Must Not Share Line

Correct

public object Teleport()
{
    lock (this) 
    {
        return this.allMyParticles;
    }
}


Incorrect

public object Teleport()
{
    lock (this) {
        return this.allMyParticles;
    }
}


7. SA1627 Documentation Text Must Not Be Empty

Incorrect

/// <summary>
/// </summary>
/// <remarks></remarks>
/// <param name="firstName">Your first name.</param>
/// <param name="lastName">Your last name.</param>
/// <returns>The application status.</returns>
public bool ApplyForJoiningTheDarkForce(string firstName, string lastName)
{
    ...
}


8. SA1005 Single Line Comments Must Begin With Single Space

Correct

private void VisitMarsSometimes()
{
    // TODO: 1. Visit grandma there.
    // TODO: 2. Buy some refrigerator magnets.
}


Incorrect

private void VisitMarsSometimes()
{
    // TODO: 1. Visit grandma there.
    //     TODO: 2. Buy some refrigerator magnets.
}


9. SA1204 Static Elements Must Appear Before Instance Elements

Correct

private static bool ShouldMakePancakesWeeklyForecast;
private bool shouldTodayMakePancakes;


Incorrect

private bool shouldTodayMakePancakes;
private static bool ShouldMakePancakesWeeklyForecast;


Summary

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

Automation and collaboration are critical to increasing operational efficiency, accelerating delivery, and innovating products. Watch Overcoming Barriers to DevOps Automation to gain insight on ways you can automate your processes with CI/CD and overcome common obstacles to developing a faster DevOps lifecycle.

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