Home > Code Analysis > Code Issues – Can initialize conditionally

Code Issues – Can initialize conditionally

January 21st, 2011

Cause:

This code issue may improve the efficiency of the code. It is shown if the initialization can be moved to an ‘else’ block of the following conditional statement. If the condition of the conditional statement is met, than the first initialization is unnecessary. This issue is shown as a suggestion (hint).

Sample:

CodeRush Code Issues - Can initialize conditionally

The method returns the current date and time on the computer. The ‘isUTC’ parameter specifies whether this method should return date and time, expressed as the local time (if false) or as the Coordinated Universal Time (if true). If we pass ‘true’ to this method the resulting var is initialized twice, which is inefficient.

How to fix:

Apply the “Initialize Conditionallyrefactoring:

CodeRush Can Initialize Conditionally Fix Preview

Notice that the resulting var is no longer reinitialized (if we pass ‘true’ to this method). In more complicated cases with time-consuming initialization this may improve the overall code performance.

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/12/2012
ID: C047

Similar Posts: