Java Reference
In-Depth Information
[Long 2012]
NUM01-J.Donotperformbitwiseandarithmeticoperationsonthesamedata
40. Properly encode relationships in constant definitions
Thedefinitionsofconstantexpressionsshouldberelated exactly whenthevaluestheyex-
press are also related.
Noncompliant Code Example
In this noncompliant code example, OUT_STR_LEN must always be exactly two greater
than IN_STR_LEN . These definitions fail to reflect this requirement:
Click here to view code image
public static final int IN_STR_LEN = 18;
public static final int OUT_STR_LEN = 20;
Compliant Solution
In this compliant solution, the relationship between the two values is represented in the
definitions:
Click here to view code image
public static final int IN_STR_LEN = 18;
public static final int OUT_STR_LEN = IN_STR_LEN + 2;
Noncompliant Code Example
In this noncompliant code example, there appears to be an underlying relationship
between the two constants where none exists:
Click here to view code image
public static final int VOTING_AGE = 18;
public static final int ALCOHOL_AGE = VOTING_AGE + 3;
A programmer performing routine maintenance may modify the definition for
VOTING_AGE , but fail to recognize the resulting change in the definition for ALCOHOL_AGE .
Search WWH ::




Custom Search