Back to Devexpress

Code Cleanup Rules

coderushforroslyn-401352-code-style-assistance-code-cleanup-code-cleanup-rules.md

latest13.5 KB
Original Source

Code Cleanup Rules

  • Jan 18, 2022
  • 4 minutes to read

Code Style

TitleDescriptionC#VB[1]
Collapse property accessorsConverts all property accessors into a single-line form.
Expand property accessorsConverts all property accessors into a multi-line form.
Format document[2]Runs the Document Formatting (C#) feature.
Make properties auto-implementedConverts properties with fully described accessors to auto-implemented properties.
Use ‘nameof’Converts string literals containing a variable or class member name to the nameof expression with the corresponding variable/member as an argument.

Layout

TitleDescriptionC#VB[1]
Add copyright headerAdds a copyright comment at the file’s beginning. (configuration)
Move types to separate filesMoves all types or selected types from one file to new separate files.
Organize membersSorts member declarations in the class and removes the redundant line breaks.
Sort namespace referencesSorts namespace references alphabetically or by length.
Sync Namespace with Folder StructureRenames namespaces within a project, so they match the project’s folder structure.

Programming Style

TitleDescriptionC#VB[1]
Apply the ‘Attribute lists’ style[2]Combines attributes into a list or breaks attribute lists apart (according to the corresponding style rule).
Apply the ‘Attribute lists’ style to assembly-level attributes[2]Combines assembly-level attributes into a list or breaks attribute lists apart (according to the corresponding style rule).
Apply the ‘Braces in statements’ styleAdds or removes optional block delimiters to/from all single-statement blocks (according to the corresponding style rule).
Apply the built-in type style[2]Converts explicitly-typed variable declarations to implicit or vice versa (according to the corresponding style rule). The related Code Action is Make Implicit/Explicit.
Apply the expression body stylesConverts single-statement properties and methods into expression-bodied structures.
Apply namespace declaration styleApplies the namespace declaration preferences specified in the Visual Studio style option or the .editorconfig file. This setting defines the file-scoped or block-scoped namespace body styles for C# 10.
Apply the ‘Optional parenthesis’ style to attributes[2]Adds or removes optional parenthesis to/from the selected attributes (according to the corresponding style rule).
Apply the ‘Optional parenthesis’ style to the new object creation (for C#). Apply the ‘Optional parenthesis’ style to invocations (a constructor, method or property) (for Visual Basic).[2]Adds or removes optional parenthesis to/from the new object creation (for C#) or invocations (for Visual Basic) according to the corresponding style rule.
Apply the ‘Optional parenthesis’ style to explicitly specify the order of operations[2]Adds or removes optional parenthesis to explicitly specify the order of operations (according to the corresponding style rule).
Apply the ‘this’/‘Me’ qualifier style[2]Adds ‘this’ / ‘Me’ qualifier where required or removes it where possible (according to the corresponding style rule).
Apply the variable declaration styleConverts CLR types to their keyword aliases or vice versa (according to the corresponding style rule).
Apply the visibility style[2]Adds type/member visibility modifiers where required or removes them where possible (according to the corresponding style rule).
Remove redundant braces in blocks without parent statement[2]Deletes optional block delimiters in “floating” code blocks.
Sort modifiers[2]Sorts type/member visibility modifiers according to the corresponding style rule. This

Redundant Code Removal

TitleDescriptionC#VB[1]
Remove all comments, excluding XML comments[2]Cleans the code from the non-XML comments.
Remove all regions[2]Cleans the code from the #region preprocessor directives.
Remove the redundant ‘base’/ ‘MyBase’ qualifier[2]Deletes the ‘base’/ ‘MyBase’ qualifier in all member references where it can be omitted.
Remove the redundant type cast[2]Deletes type casts in the places where they can be omitted.
Remove redundant type qualifiers[2]Deletes type qualifiers in the places where they can be omitted and adds the required using / Imports statements.

Strings

TitleDescriptionC#VB[1]
Convert to String InterpolationConverts all formatted strings into the string interpolations.
Convert to string.Empty[2]Converts all empty string literals (“”) into the string.Empty system constant.

Unused Code Removal

TitleDescriptionC#VB[1]
Remove unused membersDeletes unused members.
Remove unused namespace referencesDeletes the unused namespace references.
Remove unused parametersDeletes unused method parameters.
Remove unused private typesDeletes unused private nested types.
Remove unused typesDeletes “internal”, “private” and “private protected” nested types, and “internal” regular types.
Remove unused variablesDeletes unused variables.
Replace unused variables with discardReplaces unused variables with discard.

Footnotes

  1. Visual Basic

  2. This Code Action is available separately.

See Also

Code Cleanup