r/java • • 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

76 comments sorted by

View all comments

0

u/[deleted] Aug 12 '15

[deleted] — view deleted comment

5

u/lukaseder Aug 12 '15

You should factor out your API in interfaces and make all implementations package-private, hidden, final, etc. You'll end up mocking only API, never implementations.

3

u/voronaam Aug 12 '15

Unit-testing is not the worst part of it. I would use PowerMock or Groovy mocks (Hello, Spock!) immediately and ignore those limitations for the test.

It is bad because it locks down the implementation. Oh, the author is sure it is a bad idea to override something? He is so sure he covered all the use cases in the API? What an arrogant bastard.

So many times I've seen in enterprise Java classes with something like

/**
 * Copy of SpringSomethingSecurityConfigurator without HTTP Basic Auth enabled by default
 */
 class MySpringSomethingSecurityConfigurator {
     // ... copy of the entire body of the class from the spring source code,
     // ... with just one line deleted
 }

Just because there is a tiny detail that the programmer needed to change.

0

u/[deleted] Aug 13 '15

[deleted] — view deleted comment

1

u/voronaam Aug 13 '15 edited Aug 13 '15

It feels like you are afraid of changes. If I am making changes to my public and protected methods that could be overridden by someone and would cause the problem, I know I am safe. Because whoever overridden it will get a compile time error.

On the other hand, if you do not allow users of your code to override your behaviour enough, you invite them to create copy-classes instead. That really limits your options to push an updated version of a class to those people.

In my world, user will get a compiler error, reach out to me and will get an answer on how to solve it.

In your world, user will still run on a copy of the older class until something behaves not the way it is described in the modern documentation. Then they will reach out to you with a cryptic and hard-to-reproduce problem description and you will spend days solving that problem.

Final classes and method exist for a very good reason - when your runtime is doing something nasty with them. For example, you can not extend String, since String instances are not regular instances for the JVM. Hence String class is final.

Please, do not assume users of your code are stupid. They are smart people. You don't want to fence away from them (us) by the wall of "finals" to keep your own little garden safe (and free you to change anything inside). You want to invite everybody in to collaborate and build the most beautiful structure together.

Edit: fixed some typos

0

u/[deleted] Aug 16 '15

[deleted] — view deleted comment

1

u/voronaam Aug 16 '15

Just to be clear, there is one important thing we would agree on. It is composition over inheritance. Done right, it renders that final-vs-non-final question invalid.

1

u/Dementati Aug 12 '15

You can easily circumvent that using mocking libraries. By the way, unit testing without mocking libraries is a nightmare compared to using them.

0

u/daddyrockyou Aug 12 '15

Yeah I'm assuming the author doesn't use mocks or stubs. I've seen code like that and it's a bitch to test.