Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting rule to have blank line between class member declarations

Micrsoft provides bunch of coding settings for EditorConfig .NET coding convention settings for EditorConfig

But cannot find the way to create a rule, which will suggest developer to add empty line between class members declaration.

// "Bad" style
public class Order
{
    private readonly IRepository _repository;
    private readonly IPriceCalculator _priceCalculator;
    public Order(IRepository repository, IPriceCalculator priceCalculator)
    {
        _repostitory = repository;
        _priceCalculator = priceCalculator;
    }
    public CopyFrom(Order originalOrder)
    {
        // Create new order
    }
    public Cancel(Customer customer)
    {
        // Cancel order
    }
}

// Good style
public class Order
{
    private readonly IRepository _repository;

    private readonly IPriceCalculator _priceCalculator;

    public Order(IRepository repository, IPriceCalculator priceCalculator)
    {
        _repostitory = repository;
        _priceCalculator = priceCalculator;
    }

    public CopyFrom(Order originalOrder)
    {
        // Create new order
    }

    public Cancel(Customer customer)
    {
        // Cancel order
    }
}
like image 865
Basin Avatar asked Feb 17 '19 02:02

Basin


1 Answers

The Visual Studio's .NET coding convention settings for EditorConfig don't provide a code style setting for that, neither does the EditorConfig itself.

So using EditorConfig only, there is no way to create such a rule.

You might consider using StyleCop for this.

StyleCop was originally developed by Microsoft. The tool implements the code style guidelines as Microsoft defined them for the C# language. Now the development continues as open source.

StyleCop is available as a code analyzer (e.g. via this NuGet package), so you can even configure it for your projects and solutions independently. Of course, you can store the configuration in your repositories to synchronize the settings across all developers. The configuration is stored in a .ruleset file, the file format is natively supported by Visual Studio.

You could also use StyleCop as a Visual Studio Extension (StyleCop Classic), but I would not recommend doing that.

For your use case, the rule SA1513 is of interest.

Cause
A closing brace within a C# element, statement, or expression is not followed by a blank line.

In general, there are lots of rules in StyleCop that improve the code readability and structure. You are free to choose those rules that suit your needs.

like image 169
dymanoid Avatar answered Oct 16 '22 08:10

dymanoid