Versions Compared

Key

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

Avoid the use of numerical values in code (or "magic numbers" ) in code when possible. Reasons for this include, appropriately Appropriately named symbolic constants make code more readable rather than checks against a specific number. For portability reasons also if If a specific number needs to be changed reassigning a symbolic value is much easier than replacing a specific number in the code since because each case has to be checked specifically.

Non Compliant Code

...

Example

Code Block
bgColor#ffcccc
if (age>age >= 18)
 {
        takevote(personID);
}
/*Various Processing code*/... 
if (age<age <= 18)
 {
	  checkSchoolEnrollment(personID);
}

...

Code Block
bgColor#ccccff
if (age>age >= ADULT_AGE)
 {
        takevote(personID);
}
/*Various Processing code*/... 
if (age<age <= ADULT_AGE)
 {	
  checkSchoolEnrollment(personID);
}   

In the compliant code it is easy to check if the user is an adult and process accordingly. If the definition of adult changes during iterations of the codebase it is much simpler to replace the value for ADULT_AGE then search for instance of 18 and see if they're appropriate for change.

...