The definitions of two constant expressions should be related if when and only if when the values they express are expressing are also related.
Noncompliant Code Example
In this noncompliant code example, OUT_STR_LEN
must always be exactly two greater than IN_STR_LEN
. However, this This is not obvious from the definitions.
Code Block | ||
---|---|---|
| ||
public static final int IN_STR_LEN = 18; public static final int OUT_STR_LEN = 20; |
Compliant Solution
Instead, the The relationship between the two values should be represented in the definitions.
...
Noncompliant Code Example
In this noncompliant code example, there appears to be an underlying relationship between the two constants, but when in fact there is none.
Code Block | ||
---|---|---|
| ||
public static final int ADULT_AGE = 18; public static final int ALCOHOL_AGE = ADULT_AGE + 3; |
A programmer performing routine maintenance may modify the definition for ADULT_AGE
but fail to recognize the resulting change in the definition for ALCOHOL_AGE
.
Compliant Solution
Instead, the The definitions should reflect the lack of a relationship between the two constants.
...
Failure to properly encode relationships in constant declarations can may lead to unexpected values and produce code that is difficult to maintainmay complicate maintenance.
Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL03-J | low | unlikely | high | P1 | L3 |
...