|
| 1 | +:!sectids: |
| 2 | + |
| 3 | +Variable can be made constant. |
| 4 | + |
| 5 | +== Why is this an issue ? |
| 6 | + |
| 7 | +Unlike variables, constant values are known at compile time and are injected as is in the code, requiring no runtime processing and therefore reducing the environmental footprint. |
| 8 | +Although good compilers will const eligible variables by themselves, it is still good practice to declare them constant, as it makes the code intent clearer. |
| 9 | + |
| 10 | +=== When can it be ignored ? |
| 11 | + |
| 12 | +This rule should not be ignored. |
| 13 | + |
| 14 | +== Non compliant examples |
| 15 | + |
| 16 | +[source, java] |
| 17 | +---- |
| 18 | +public class MyClass { |
| 19 | +
|
| 20 | + private final Logger logger = Logger.getLogger(""); |
| 21 | +
|
| 22 | + private Object myNonFinalAndNotReassignedObject = new Object(); // Noncompliant, object initialized but neither used nor reassigned |
| 23 | + private String varDefinedInClassNotReassigned = "0"; // Noncompliant, object initialized and used but not reassigned |
| 24 | + private String varDefinedInClassNotUsed = "0"; // Noncompliant, simple type object initialized but neither used nor reassigned |
| 25 | +
|
| 26 | + void localVariableNotReassigned() { |
| 27 | + String y4 = "10"; // Noncompliant, local variable initialized but not reassigned |
| 28 | +
|
| 29 | + logger.info(y4); |
| 30 | + logger.info(varDefinedInClassNotReassigned); |
| 31 | + } |
| 32 | +
|
| 33 | +} |
| 34 | +---- |
| 35 | + |
| 36 | +== Compliant examples |
| 37 | + |
| 38 | +[source, java] |
| 39 | +---- |
| 40 | +public class MyClass { |
| 41 | +
|
| 42 | + private final Logger logger = Logger.getLogger(""); // Compliant, object initialized and used (never reassigned but ok because "final" keyword used) |
| 43 | +
|
| 44 | + private Object myNonFinalAndReassignedObject = new Object(); // Compliant, reassigned in method |
| 45 | + private final Object myFinalAndNotReassignedObject = new Object(); // Compliant, object initialized and never reassigned but 'final' keyword used |
| 46 | +
|
| 47 | + private static final String CONSTANT = "toto"; // Compliant, because 'static' and 'final' keywords |
| 48 | + private String varDefinedInClassReassigned = "0"; // Compliant, reassigned in method |
| 49 | + private String varDefinedInConstructorReassigned = "1"; // Compliant, reassigned in constructor |
| 50 | +
|
| 51 | + public MyClass() { |
| 52 | + varDefinedInConstructorReassigned = "3"; |
| 53 | + logger.info(varDefinedInConstructorReassigned); |
| 54 | + } |
| 55 | +
|
| 56 | + void localVariableReassigned() { |
| 57 | + String y1 = "10"; // Compliant, reassigned above |
| 58 | + final String PI = "3.14159"; // Compliant, not reassigned but 'final' keyword used |
| 59 | +
|
| 60 | + y1 = "titi"; |
| 61 | +
|
| 62 | + logger.info(y1); |
| 63 | + logger.info(PI); |
| 64 | + } |
| 65 | +
|
| 66 | + void localVariableIncrement() { |
| 67 | + String y2 = "10"; // Compliant, reassigned above |
| 68 | + y2 += "titi"; |
| 69 | + logger.info(y2); |
| 70 | + } |
| 71 | +
|
| 72 | + void localIntVariableIncrement() { |
| 73 | + int y3 = 10; // Compliant, reassigned above |
| 74 | + ++y3; |
| 75 | + logger.info(y3+""); |
| 76 | + } |
| 77 | +
|
| 78 | + void classVariableReassigned() { |
| 79 | + varDefinedInClassReassigned = "1"; |
| 80 | +
|
| 81 | + logger.info(varDefinedInClassReassigned); |
| 82 | + logger.info(varDefinedInClassNotReassigned); |
| 83 | + logger.info(CONSTANT); |
| 84 | + } |
| 85 | +
|
| 86 | + void classVariableReassignedBis() { |
| 87 | + varDefinedInClassReassigned = "2"; // method to avoid sonarqube error asking for moving class variable "varDefinedInClassReassigned" to local variable method |
| 88 | + myNonFinalAndReassignedObject = new Object(); |
| 89 | +
|
| 90 | + logger.info(varDefinedInClassReassigned); |
| 91 | + logger.info(myNonFinalAndReassignedObject); |
| 92 | + logger.info(myFinalAndNotReassignedObject); |
| 93 | + } |
| 94 | +
|
| 95 | +} |
| 96 | +---- |
0 commit comments