Home > Code Analysis > Code Issues hints for declaration and initialization

Code Issues hints for declaration and initialization

October 12th, 2012

Here are a few simple code issues of a hint type (suggestion) for declarations and initializations.

Can combine initialization with declaration

Cause:

The declaration and its initialization can be combined into a single statement instead of two separate statements. Combining an initialization and declaration may improve code readability.

Sample:

CodeRush Can Combine Initialization With Declaration Code Issue

How to fix:

  • Apply the Move Initialization to Declaration refactoring:

CodeRush Move Initialization To Declaration Fix Preview

Can inline temporary variable

Cause:

The expression assigned to the temporary variable that is used only once can be inlined. Removing a temporary variable may improve code readability.

Sample:

CodeRush Can Inline Temporary Variable Code Issue

How to fix:

  • Apply the Inline Temp refactoring:

CodeRush Inline Temp Fix Preview

Declaration can be a constant

Cause: If the initialized declaration does not change its value, it can be a constant. A constant differs from a variable because once declared, the value assigned to the constant cannot be modified. Having constant declarations creates a self-documenting and strong code by telling the developer that he/she is not allowed to change the value of the constant variable, which can results in fewer errors. Additionally, constants improve performance over regular variables in many cases, particularly when using integral types such as integer.

Sample:

CodeRush Declaration Can Be A Constant Code Issue

How to fix:

  • Apply the Convert to Constant refactoring:

CodeRush Convert To Constant Fix Preview

Implicit variable can be used

Cause:

This code issue notifies a developer that an implicitly typed local variable can be used. Such variables provide a new simplified declaration syntax that instructs the compiler to infer the type of a new variable according to its initial usage. Converting a variable to an implicitly typed local variable may improve code readability.

Sample:

CodeRush Implicit Variable Can Be Used Code Issue

How to fix:

CodeRush Make Implicit Fix Preview

Initializer can be used

Cause:

The instantiation of a variable and subsequent property assignments can be converted into a object initializer expression. Object initializers allow you to set the initial values of fields or properties of an object as part of the single instantiation statement, which is more compact and may improve code readability

Sample:

CodeRush Initializer Can Be Used Code Issue

How to fix:

  • Apply the Convert to Initializer refactoring:

CodeRush Convert To Initializer Fix Preview

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Oct/12/2012
ID: C198

Similar Posts: