Home > Code Analysis > Code Issues hints for expressions

Code Issues hints for expressions

September 28th, 2012

Here are the code issues for different expressions. If the fix is applied, following a hint it may optimize the code and/or improve its readability.

Null coalescing operation can be used

Cause:

The null coalescing operation verifies the value of a variable for ‘nullness’. If the value is not equal to ‘null’, a variable’s value is returned. If it is equal to ‘null’, a substitute value, provided as a second operand of the operation, is returned. The null coalescing operation simplifies the syntax of the ternary operation, for instance. Converting an expression to a null coalescing operation may be increase code readability.

Sample:

CodeRush Null Coalescing Operation Can Be Used Sample

How to fix:

  • Apply the Compress to Null Coalescing Operation refactoring:

CodeRush Null Coalescing Operation Can Be Used Fix

Redundant String.Format call

Cause:

The String.Format call allows composing a string data, that combines other strings and referenced variable values. If the String.Format call does not take referenced variable values, a simple string concatenation operation or even a single string value can be used instead of String.Format. This may optimize the code and improve its clarify.

Sample:

CodeRush Redundant String Format Call Sample

How to fix:

  • Apply the Remove Redundant Call refactoring:

CodeRush Remove Redundant Call Fix

Redundant ToString() call

Cause:

There are cases when the ToString() call is redundant. In these cases, the compiler automatically calls ToString(). If a call ToString() is not required, a hint is shown suggesting you remove it.

Sample:

CodeRush Redundant ToString Call Sample

How to fix:

  • Apply the Remove Redundant Call refactoring:

CodeRush Redundant ToString Call Fix

String.Compare can be used

Cause:

Strings can be compared using the equality operator ‘==’. However, this operator performs an exact, case-sensitive comparison of two strings.. For better support of string comparison, the String.Compare call can be used. This code issue of a hint type shows you that the equality operator can be changed into the String.Compare call.

Sample:

CodeRush String Compare Can Be Used Sample

How to fix:

CodeRush Use String Compare Fix

String.Format can be used

Cause:

String.Format can process typical strings and variable references. Sometimes, instead of concatenating these pieces with a string concatenation operator, it might be better to use the special String.Format call for this purpose. This may improve code readability and clarity.

Sample:

CodeRush String Format Can Be Used Sample

How to fix:

CodeRush Use String Format Fix

Ternary expression can be used

Cause:

The conditional operator allows you to define a boolean condition and two expressions as a result of a boolean operation. Using the simplified ternary expression can improve code readability, because it allows you to compress if-else statements to a single expression.

Sample:

CodeRush Ternary Expression Can Be Used Sample

How to fix:

  • Apply the Compress to Ternary Expression refactoring:

CodeRush Compress To Ternary Expression Fix

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

Similar Posts: