Home > Code Analysis > Code Issues hints for types and members

Code Issues hints for types and members

September 28th, 2012

Here are the suggestions (hints) code issues which might improve the readability, clarity and performance of your source code.

Initializer can be used

Cause:

Object initializers allow multiple property values to be set within a single object instance creation operation. They are used in conjunction with existing object constructors. Object initializers provide a simplified syntax for specifying initial values for one or more properties of an object. Using an object initializer may improve code readability.

Sample:

CodeRush Initializer Can Be Used Sample

How to fix:

  • Apply the Convert to Initializer refactoring:

CodeRush Initializer Can Be Used Fix

Partial class has only single part

Cause:

A class, structure or interface can be marked as partial to allow its definition to be split between several files. If a type is marked as partial and has only a single part, there is no need to mark it partial. Removing the partial keyword may increase code clarity.

Sample:

CodeRush Partial Class Has Only A Single Part Sample

How to fix:

  • Remove the ‘partial’ keyword:

CodeRush Partial Class Has Only A Single Part Fix

 

Property can be auto-implemented

Cause:

If a property uses a simple backing store field and have the corresponding accessors to return the value of the field, and set the value to the field, this property can be simplified to an automatically implemented property. This usual syntax for declaring a read/write property includes a large number of lines of code for a relatively simple task. Converting such a property into an auto-implemented property may increase code readability a lot.

Sample:

CodeRush Property Can Be Auto Implemented Sample

How to fix:

  • Apply the Convert to Auto-implemented Property refactoring:

CodeRush Property Can Be Auto Implemented Fix

 

Redundant delegate creation

Cause:

In some cases, specifying the delegate type explicitly is redundant. For example, it is redundant when you subscribe to an event, because the compiler will infer the type of the delegate automatically from the event handler creation code.

Sample:

CodeRush Redundant Delegate Creation Sample

How to fix:

  • Apply the Remove Redundant Delegate Creation refactoring:

CodeRush Redundant Delegate Creation Fix

 

Redundant sealed modifier

Cause:

If a member marked as ‘sealed’ is located inside the class which is also marked sealed, then there’s no need to mark a member as sealed, because the use of the sealed keyword on a class prevents the class from inheriting functionality for all its members.

Sample:

CodeRush Redundant Sealed Modifier Sample

How to fix:

  • Remove the ‘sealed’ keyword:

CodeRush Redundant Sealed Modifier Fix

 

Type can be moved to separate file

Cause:

It is recommended to have a single class per source file. If a file contains several neighborhood classes, and a particular class can be moved to its own file (e.g., a type name differs from the file name), a hint is shown indicating that the class can be moved.

Sample:

CodeRush Type Can Be Moved To Separate File Sample

How to fix:

  • Apply the Move Type to File refactoring to each class:

CodeRush Move Type To File Fix

 

Type name does not correspond to file name

Cause:

It is also recommended to have a class name that corresponds to the file name, where the class resides. If a class name does not correspond to the file name, the hint is shown suggesting you to rename a file or a class to a single name.

Sample:

CodeRush Type Name Does Not Correspond To File Name Sample

How to fix:

  • Apply the Rename File to Match Type refactoring:

CodeRush Rename File To Match Type Fix

 

  • Apply the Rename Type to Match File refactoring:

CodeRush Rename Type To Match File Fix

 

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Sep/28/2012
ID: C192

Similar Posts:

  1. No comments yet. Be the first and leave a comment!