r/programming • • 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/
28 Upvotes

35 comments sorted by

View all comments

5

u/robby_w_g Aug 11 '15 edited Feb 15 '26

Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.

1

u/lookmeat Aug 12 '15

Think of it more in the lines of YAGNI. But what if I need to overload this method? What if I need to change the value of the input variable? The answer to all those questions is: you probably won't need it, and when you do you can change the signature.

Now if a user writes poor code that breaks your library that is his problem. But what if the user wrote solid good code that breaks your library because your library is poorly written? By making a method final you can work on the assumption that it won't be replaced, so you don't have to worry about different considerations. Then you can make the few areas of your object that can be overridden non-final and then be paranoid around those few methods (allowing your library to work even with poor code, that makes it better than most libraries out there).

With the making parameters and variables final it's meant to work much like functional programming does. It assures us what is being altered and what isn't. Again you could just check on the code and verify it doesn't happen, but why do it when the compiler can do it for you and all for a thing (changing the value of a variable) that you don't really need?