in #16923 I already disagreed with Utility classes should not have public constructors (squid:S1118), because it makes code unnecessarily verbose, but I didn’t make myself clear: Classes with only “static” methods should not be instantiated (java:S2440) is the better rule, and that’s the only one that should exist. Which is what I request from the SonarQube developers.
I simply don’t consider it best practice to protect ourselves so hard from issues that simply never happen if we use common sense: if a class looks like a utility function, and behaves like a utility function then it is a utility function. This is duck typing, and works well in certain languages like Python. Unlike Kotlin, Java never had a good solution for this problem, but that doesn’t mean we should increase verbosity just because there is a very slight chance that someone may do the wrong thing. Instead, the wrong thing should be detected and reported as an issue, that’s what java:S2440 is about. I explained the details in the linked forum topic.
Also, there is a potential side effect of the rule:
You say, we can use Lombok to solve the problem too, well, it’s not necessarily a good library to use. It does come with downsides and risks, see for example:
- https://stackoverflow.com/questions/4589184/what-are-the-risks-with-project-lombok
- Reddit - The heart of the internet
- Should you use Lombok? Or, is it bad for you? | SivaLabs
- https://medium.com/@vgonzalo/dont-use-lombok-672418daa819
So instead of being overly defensive in our code, we should focus on readability first and what actually yields productivity.
=> Please remove squid:S1118.