-
-
Notifications
You must be signed in to change notification settings - Fork 26
Description
The analyzer is right about that the calculation is done in the mentioned method but it is checked whether it was calculated before, therefore removing the risk of calculating again.
I am not experienced in Java and I am certainly new to java-analyzer. I want to open an issue because I think the analyzer can be improved.
I submitted an iteration with the following code:
public class Hamming {
private final String leftStrand;
private final String rightStrand;
private Integer distance;
public Hamming(String leftStrand, String rightStrand) {
if (leftStrand.length() != rightStrand.length()) {
throw new IllegalArgumentException("strands must be of equal length");
}
this.leftStrand = leftStrand;
this.rightStrand = rightStrand;
}
public int getHammingDistance() {
if (distance != null) {
return distance.intValue();
}
int hammingDistance = 0;
for (int i = 0; i < leftStrand.length(); i++) {
if (leftStrand.charAt(i) != rightStrand.charAt(i)) {
hammingDistance++;
}
}
this.distance = Integer.valueOf(hammingDistance);
return getHammingDistance();
}
}I got the followint feedback from the analyzer:
Consider calculating the Hamming distance inside the constructor and storing the result in a member variable which can be returned inside getHammingDistance(). This solution seems to be calculating the Hamming distance inside the getHammingDistance() method. This means that the calculation might be done again if you did not use lazy initialization.