Skip to main content

Programming Style Rules

  • 10 minutes to read

You can define a set of programming style rules that CodeRush applies to your code when you refactor, expand templates, and run code cleanup.

Open the Editor | C# (Visual Basic) | Programming Style options page to configure your code preferences.

Style_Rules

You can configure the following code style aspects:

Type/Member Visibility Modifiers

Defines whether to add visibility modifiers (private, public, etc.) or remove them from places where they do not change code behavior.

private class MyClass // Explicit visibility modifier
{
    private int myField; // Explicit visibilty modifier 
    int MyProperty { get; set; } // Implicit visibilty modifier
}

Use the Apply visibility style code cleanup rule.

Order of Modifiers

Defines the order of a member’s modifiers.

static internal protected void HelloWorld() => Console.WriteLine("Hello World"); // Violates the default style
protected internal static void HelloWorld() => Console.WriteLine("Hello World"); // Follows the default style

This option is available for C# only because Visual Studio automatically sorts modifiers in Visual Basic.

Use the Sort modifiers code cleanup rule to change the existing code according to this style.

Namespace References Declaration Style

Defines the namespace references declaration style. The examples below show differences between options.

// Use fully qualified type names (without adding new namespace import/using references)
namespace CRRDemo {
    class Person : DemoInterfaces.IPerson {
        public string FullName { get; set; }
    }
    //...
}

// Place namespace import/using references at the top of the file
using DemoInterfaces;
namespace CRRDemo {
    class Person : IPerson {
        public string FullName { get; set; }
    }
    //...
}

// Place namespace import/using references inside the active namespace
namespace CRRDemo {
    using DemoInterfaces;
    class Person : IPerson {
        public string FullName { get; set; }
    }
    //...
}

Use Braces in Statements

Defines the style for insignificant braces around blocks with a single statement.

// Never use braces for single statements
if (statement1)
    DoThings();
if (statement2)
    return 42;

// Use braces only for statements which don't control program execution flow
if (statement1) {
    DoThings();
}
if (statement2)
    return 42;

// Always use braces for single statements
if (statement1) {
    DoThings();
}
if (statement2) {
    return 42;
}

Use the Apply ‘Braces in statements’ style code cleanup rule.

Optional Parenthesis

Defines where CodeRush should explicitly add or omit optional parenthesis.

[Foo()]  // Explicit parenthesis in attributes
[Bar]    // Omit parenthesis in attributes
public class C {
    public int Foo { get; set; }
    public static C[] Create() {
        return new[] {
            new C() {  // Explicit parenthesis in constructor calls
                Foo = (8 * 42) + 11  // Explicit parenthesis in arithmetic operators
            },
            new C {  // Omit parenthesis in constructor calls
                Foo = -3 * 42 + 11  // Omit parenthesis in arithmetic operators
            }
        };
    }
}

Use one of the following code cleanup rules to change the existing code according to this style:

  • Apply ‘Optional parenthesis’ style to attributes”
  • Apply ‘Optional parenthesis’ style to new object creation (for C#) or Apply ‘Optional parenthesis’ style to invocations (constructor, method or property) (for Visual Basic)
  • Apply the ‘Optional parenthesis’ style to explicitly specify the order of operations

Attribute Lists

Defines the style for member attributes when attributes are applied simultaneously.

// Prefer single attribute list for all attributes
[FirstAttribute,
SecondAttribute]
void DoThings() { /* ... */}

// Put each attribute in its own attribute list
[FirstAttribute]
[SecondAttribute]
void DoThings() { /* ,., */}

Use the Apply ‘Attribute lists’ style or Apply ‘Attribute lists’ style to assembly-level attributes code cleanup rule.

using System.Reflection;
// Prefer single attribute list for all attributes
[assembly: AssemblyTitle("Assembly Title"),
AssemblyDescription("Assembly Description"),
AssemblyConfiguration("Assembly Configuration")]

// Put each attribute in its own attribute list
[assembly: AssemblyTitle("Assembly Title")]
[assembly: AssemblyDescription("Assembly Description")]
[assembly: AssemblyConfiguration("Assembly Configuration")]

Visual Studio’s Options and Settings from the EditorConfig File

The following code style settings are disabled on the Editor | C# (Visual Basic) | Programming Style page of the CodeRush options dialog:

UserInfo

You can use the corresponding Visual Studio code style preferences and settings in the EditorConfig file to configure the code style in CodeRush:

  • ‘this.’ and ‘Me.’ preferences;
  • predefined type preferences;
  • ‘var’ preferences;
  • namespace declaration style for C# 10;
  • use expression body.

UserInfo

Use Expression Bodies

Defines whether to use expression bodies in methods, constructors, operators, properties, indexers, accessors, lambdas, and local functions.

To change these settings in Visual Studio 2017 or later:

choose Tools | Options | Text Editor | C# | Code Style | Expression preferences.

To change these settings in Visual Studio 2015:

open the Editor | C# | Programming Style CodeRush options page.

public class MyClass {
    public MyClass() {
        MyMethod();
    }

    //  Do not use expression body in properties
    public virtual string MyProperty {
        get { return string.Empty; }
    }

    // Use expression body on a single line in accessors  
    public virtual string MyProperty {
        get => string.Empty;
    }

    // Use expression body on a single line in properties 
    public virtual string MyProperty => string.Empty;

    // Do not use expression body in methods
    public virtual string MyMethod() {
        return string.Empty;
    }

    // Use expression body in methods when possible
    public virtual string MyMethod() => string.Empty;
    }
}    

These code style settings are used by Declaration Providers, the Declare Menu, Templates, and other features that generate new members.

You can also use the Apply expression body styles code cleanup rule.

Namespace Declaration Style for C# 10

Applies the Visual Studio namespace declaration preferences and corresponding settings from the .editorconfig file in code cleanup. This preference defines file-scoped or block-scoped namespace body styles for C# 10.

using System;

namespace MyNamespace // Block-scoped namespace body style
{
  public class MyClass
  {
  }
}

using System;

namespace MyNamespace; // File-scoped namespace body style

public class MyClass
{
}

To change this setting in Visual Studio: choose Tools | Options | Text Editor | C# | Code Style | General | Code block preferences: namespace declarations.

To modify the existing code according to this style:

  1. Enable the “Apply namespace declaration style” code cleanup rule on the Editor | C# | Code Cleanup CodeRush options page;
  2. Run code cleanup.

Local Declaration Style

Defines whether to use explicitly-typed or implicitly-typed variable declarations.

var T = new Temperature(); // Prefer 'var'
Temperature T = new Temperature(); // Prefer explicit type

Use the Apply variable declaration style code cleanup rule to change the existing code according to this style.

Built-in Type Names

Defines whether to use CLR types and/or their keyword aliases.

int ID; // Prefer predefined type
Int32 ID; // Prefer framework type

Use the Apply built-in type style code cleanup rule.

Use ‘this’/‘Me’ Qualifier

Defines whether to add or remove the ‘this.’/‘Me.’ Qualifier in places where it does not change code behavior.

// Use 'this' qualifier for properties
public class Temperature {
    public double Celsius {
        get { return (this.Fahrenheit - 32) * (5.0 / 9); }
        set { this.Fahrenheit = (value * 9.0 / 5) + 32; }
    }
    public double Fahrenheit { get; set; }
}

// Do not use 'this' qualifier for properties
public class Temperature {
    public double Celsius {
        get { return (Fahrenheit - 32) * (5.0 / 9); }
        set { Fahrenheit = (value * 9.0 / 5) + 32; }
    }
    public double Fahrenheit { get; set; }
}

Use the Apply ‘this’/‘Me’ qualifier style code cleanup rule.

You can configure the “Do not replace the existing copyright header during code cleanup” option.

Header Option

This option allows you to save the existing copyright header after CodeRush applies the Add copyright header code cleanup rule.

Consider the following code:

    //-----------------------------------------------------------------------
    // <copyright file="D:\Projects\CodeRushDemo\CodeRushDemo\Program.cs" company="My Company">
    //     Author: User
    //     Copyright (c). All rights reserved.
    // </copyright>
    //-----------------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    // ...

To see the “Do not replace the existing copyright header during code cleanup” option in action, perform the following steps:

  • Open the CodeRush IDE | User Info options page and specify the following settings:

UserInfo

  • Enable the “Do not replace the existing copyright header during code cleanup” option.

  • Run the Add copyright header code cleanup rule.

CodeRush does not change the existing copyright header.

Refer to the following example for more information: How to Add a License to Code Files.

Generate New Blazor Usings in the _Imports.razor File

This option adds new Blazor ‘using’ statements to the _Imports.razor file after CodeRush applies the Extract Razor Component code provider. Disable this option to add Blazor ‘using’ statements to the original .razor file.

Usings

The following image shows the “@using BlazorApp.Components” line added to the NavMenu.razor file:

Usings

See Also