Skip to main content
A newer version of this page is available. .

Programming Style Rules

  • 6 minutes to read

With CodeRush, you can define a set of programming style rules that automatically apply to your code when you are refactoring, expanding templates and performing code cleanup.

You can configure your code preferences in the Editor | <Language> | Programming Style options page.

Style_Rules

Below is a list of configurable code style aspects, each with an example of different code styles:

  • Local declaration style

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

    var T = new Temperature(); // Implicit typing
    Temperature T = new Temperature(); // Explicit typing
    

    You can change the existing code according to this style using the Apply variable declaration style code cleanup rule.

  • 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.

    int privateField; // Implicit visibility modifier
    private int privateField; // Explicit visibility modifier
    

    You can change the existing code according to this style using the Apply visibility style code cleanup rule.

  • Order of modifiers

    Defines the required order of 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
    

    You can change the existing code according to this style using the Sort modifiers code cleanup rule.

  • Built-in type names

    Defines whether to use the CLR types or their keyword aliases.

    int ID; // C# keyword
    Int32 ID; // CLR type
    

    You can change the existing code according to this style using the Apply built-in type style code cleanup rule.

  • 'this'/'Me' qualifier usage

    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; }
    }
    
    // Not to 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; }
    }
    

    You can change the existing code according to this style using the Apply 'this' qualifier style code cleanup rule.

  • Namespace references declaration style

    Defines the namespace references declaration style. Refer to the examples below for 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;
    }
    

    You can change the existing code according to this style using the Apply 'Braces in statements' style code cleanup rule.

  • Optional parenthesis

    Defines where the optional parenthesis should be explicitly added or omitted.

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

    You can change the existing code according to this style using the Apply 'Optional parenthesis' style code cleanup rule.

  • Attribute list

    Defines the style for member attributes when several attributes are applied at once.

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

    You can change the existing code according to this style using the Apply 'Attribute lists' style or Apply 'Attribute lists' style to assembly-level attributes code cleanup rule.

NOTE

This feature is available as a part of Code Cleanup.

See Also