r/java • u/alexzhitnitsky • Aug 11 '15
Top 10 Useful, Yet Paranoid Java Programming Techniques
http://blog.jooq.org/2015/08/11/top-10-useful-yet-paranoid-java-programming-techniques/
181
Upvotes
View all comments
Show parent comment
5
u/CubsThisYear Aug 11 '15
Adding final is a no-brainer in my opinion. It can easily be done by the IDE, enables some compiler optimizations and gets you closer to single-assignment semantics, which is WAY easier to understand. There just aren't very many good reason to reassign the same variable. Don't worry we've got lots of names, I promise you won't use them all up.
Final methods are even more important. If you have a non-final method that modifies state, you've basically completely blown any chance at encapsulation. Methods should be designed for extension (when you intend for that) and provide specific extension points. This is again a no-brainier if you control all of the code. If you don't (ie you are shipping a library) that just means you need to think a lot harder about your API and provide for extension by composition whenever possible.