Why you should care
– Confusing: the developer (who is often not the same person as the initial software author) doesn’t quickly understand what this value means in the context of the application (why 65? why not 64 or 66?). Here, 65 refers to the legal age for work retirement in the United Kingdom.
– Counter-productive: if the value is used by other source files (across components, microservices, etc.) and needs to be changed (e.g. the legal age for retirement will change next year in the UK and will be 67), you’re development team will have to modify all places where this value is used.
CAST Recommendations
From an software engineering perspective, it is recommended to manage this value globally, in a way that other files, components … can access it from a single place, as shown in the example below.
Suspicious Code Pattern:
public class Foo {
public void setPassword(String password) {
// don’t do this
if (password.length() > 7) {
throw new InvalidArgumentException(“password”);
}
}
}
Refactored Code:
public class Foo {
public static final int MAX_PASSWORD_SIZE = 7;
public void setPassword(String password) {
if (password.length() > MAX_PASSWORD_SIZE) {
throw new InvalidArgumentException(“password”);
}
}
}
How we detect
– Used as an initialization when declaring a variable
– Are one the following autorized numbers: 0.0, 1.0, 0., 1., or an integer from 0 to 9
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.