Magic Literals
NOTE: This page / space is still a work in progress.
Magic numbers:
Any numbers apart from 0, -1, 1 should be in a (clearly named) constant local or global constant (constants are marked with the final keyword).
Magic Numbers: Good Style
public class Circle {
public static final double NUM_COLS = 10;
public static double printStars() {
for (int i = 0; i < NUM_ROWS; i++) {
System.out.print("*");
}
}
}Magic Numbers: Bad Style
public class Circle {
public static double printStars() {
for (int i = 0; i < 10; i++) {
System.out.print("*");
}
}
}