Naming Conventions
- 5 minutes to read
You can define naming standards for each identifier type in C#, Visual Basic, and JavaScript. CodeRush applies the naming style to your code when you run Refactorings, Code Providers, and Code Templates.
The code parts that do not follow naming conventions can be treated as code issues (Analyzer ID CRR1000). For example, you can create a naming convention which specifies that classes should contain the “Custom” prefix and the first word is in upper case.
Configure Naming Conventions
Access Configuration Settings
Open the Editor | <Language> | Naming Conventions options page.
The naming convention contains the main rule and additional rule settings for each type of identifier. Code generation features refer to the main rule, whereas the static code analysis uses the main rule and additional rules.
You can configure naming conventions for any type of identifier. For example, to change naming conventions for private fields, perform the following actions:
Configure Main Rule
Select Instance Private Fields and click Edit in the “Main Rule” section to change the main rule for this convention type.
The Naming Rule dialog appears.
- In the Naming Rule dialog, set the Prefix field to “_” and Capitalizing to PascalCase.
Click OK to apply the changes and close the Naming Rule dialog.
The Main Rule section of the Naming Conventions options page shows the modified settings for Instance Private Fields.
Note
To define the snake_case naming rule, select “all lower” in the Capitalizing field and set Separator Options to “With Underscores”.
Enable “Use in Code Analysis” Setting
Select Instance Private Fields in the identifier list and enable the “Use in Code Analysis” option to allow CodeRush to detect violations of this naming rule and show them in the Code Issues window.
CodeRush also allows you to add the CRR1000 diagnostic to the Visual Studio’s Error List window. Refer to the Background Analysis topic for more information.
Configure Additional Rules (Optional)
CodeRush allows you to add, modify, and remove additional rules. The static code analysis uses additional rules. Perform the following actions to configure an additional rule:
Click Edit in the “Additional Rules” section for Instance Private Fields to change the additional rule.
In the invoked Naming Rule dialog, type “Name” in the Suffix field and set Capitalizing to camelCase.
Click OK to save these settings and close this dialog.
CodeRush updates the additional rule on the Naming Conventions options page.
Click OK to close this options page.
Apply Main Rule
Consider the following C# code:
namespace App
{
public class MyClass
{
public MyClass(int firstParameter)
{
}
}
}
Run the Declare Field with Initializer code provider to generate a field of the int type.
CodeRush declares the “_FirstParameter” field that matches the main rule.
Apply Additional Rules
CodeRush applies additional rules if the Use In Code Analysis option is enabled for the convention type on the Naming Conventions options page.
CodeRush checks code. Code parts that do not follow the main rule and additional rules are treated as code issues (ID: CRR1000). For example, the “_firstParameter” field violates the additional rule in the following code:
namespace App
{
public class MyClass
{
readonly int _firstParameter;
//...
}
}
Open the Code Issues Window to check this issue.
You can also see this issue in Visual Studio’s Error List. Refer to the Background Analysis topic for more information.
To fix this issue, rename the “_firstParameter” field to match the naming convention. For example, change the field name to “_fieldParameterName”.
Generic Types’ File Naming Rule
The Naming Conventions feature also includes the “Generic Types’ File Naming rule” option. This option allows you to specify the naming convention for file names of generic types. CodeRush applies this naming convention when you run code generation features.
Example
The following example shows how to use the “Generic types’ file naming rule” option.
Consider the following code:
namespace App
{
public class PagedQuery<TOut> : PagedQueryBase, IQuery<TOut>
{
//...
}
class MyClass
{
//...
}
}
Perform the following actions:
Open the Naming Conventions options page and set the Generic types’ file naming rule option to “Type name with parameters count” as shown in the screenshot above.
Click OK to apply the setting and close the options page.
Run Move Type to File refactoring.
CodeRush moves the generic class to a new file and the name of this file contains the type name and parameter count.
Visual Studio’s Options and Settings from the EditorConfig File
You can use Visual Studio’s naming styles options and corresponding settings in the EditorConfig file to configure naming conventions in CodeRush.
Note
CodeRush supports settings from the EditorConfig file for Visual Studio 2019 16.5 and later.
Visual Studio’s naming style options override CodeRush settings specified on the Naming Conventions options page.
For example, the following Visual Studio’s naming rule for types specifies the pascal case naming style and the “_” prefix.
Open Visual Studio options and navigate to the “Text Editor | C# | Code Style | Naming” options page to access to these settings.
The following screencast shows the Name Anonymous Type refactoring that applies the specified Visual Studio naming rule:
You can use the settings in the EditorConfig file to override Visual Studio’s naming convention settings and CodeRush settings. For example, the following EditorConfig file defines a naming convention for classes and structures. This convention specifies that all public classes and structures should be capitalized and contain the Name suffix.
CodeRush applies the overridden naming settings when you run a refactoring, template, and code provider. The screencast below shows the Name Anonymous Type refactoring: