Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
private final int BUFSIZE = 512;

// ...

public void exampleFunction() {
  int nblocks = 1 + ((nbytes - 1) >> 9);  // BUFSIZE = 512 = 2^9
  // ...
}

The programmer has assumed that BUFSIZE is 512, and right-shifting 9 bits is the same (for positive numbers) as dividing by 512. However, if BUFSIZE changes to 1024 in the future, modifications will be difficult and error-prone.

...

This compliant solution uses the identifier assigned to the constant value in the expression.

Code Block
bgColor#ccccff

private final int BUFSIZE = 512;

// ...

public void exampleFunction(int nbytes) {
  int nblocks = 1 + (nbytes - 1) / BUFSIZE;
  // ...
}

Exceptions

DCL02-EX1: The use of symbolic constants should be restricted to cases where they improve the readability and maintainability of the code. When the intent of the literal is obvious, or where the literal is not likely to change, using symbolic constants can impair code readability. The following noncompliant code example obscures the meaning of the code by using too many symbolic constants.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8495834025ddc772-073c210f-4d474d0c-b4ac9e11-274a691c0588767d8700126b"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="91c4bc9b55600d7d-642d88a3-43a14b83-b580a5d4-240cccdd338cac5108bb7a6a"><ac:plain-text-body><![CDATA[

[[Core Java 2004

AA. Bibliography#Core Java 04]]

]]></ac:plain-text-body></ac:structured-macro>

...