It may be problematic to enforce your heuristics. This may be the reason that eclipse does not implement them.
Setting all classes that are not extended in your code to final
- how can the IDE know if they are intended to be extended elsewhere? Make them final
only if you really intend to prohibit extension.
The same goes for abstract
- there is no way for the IDE to know if you are writing a framework, or just a domain model .jar
, where there never will be any initialization. Making the entities abstract
automatically may shoot you in the foot later.
This is rarely the case for variables, so this heuristic eclipse can do.
EDIT I think I have to elaborate a bit on my reasoning.
Adding the final
modifier is not problematic for variables because they are mostly localized - they live within the scope of the code under your control. There are rarely modyfiable variables that are visible to 3rd party code and can be modified wihtout calling a method.
If the final
modifier on a variable is excessive, it's impact will be limited to your code, and you will probably notice it fast. So the worst case is that you will have to open the file and remove the modifier.
Things are different for classes - many of them are public and thus accessible to 3rd party code. If you declare all classes that are not instantiated in your code as abstract
, you will effectively prohibit everybody else from instantiating it - and this time you will not even know. So the worst case here is that somebody else will not be able use your code. That's why I would not automate it - most of the time you would want 3rd party code to be able to instantiate the classes that you have defined.
There is however a heuristic about when to make a class final
- when a class has only static methods. Some tools like PMD and Checkstyle know that, and will tell you about it. This option would actually fit well into eclipse, but is unfortunately not implemented.
As to making classees final
per default - andersoj and many others adhere to the principle "design for inheritance or prohibit it" - i think it was introduced by Joshua Bloch in his "Effective Java" book. Others argue that it would violate the open-closed principle to "seal" all classes by default. I tend to the latter.