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

6

u/PaulRivers10 Aug 11 '15 edited Aug 25 '15

Some of these are fine but 6 and 7 make me cringe.

It "solves" a non-existent problem I never run into. I've never run into a problem with someone extending my class when I didn't want them to, or frankly even reassigning my variable (except for the ones I actually want non-changeable but those are static final already).

I does make fixing bugs a gigantic pain in the ass, as well as adding new functionality, as well as just cluttering up trying to read the code.

Now the code starts to read like: It final "solves" a non-existant final problem I final never run final into.

It's messy, without solving anything that I've found to be any sort of regular problem. If someone is messing in my code that I don't want to be they just remove "final"'s they don't like anyways.

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.

1

u/anubus72 Aug 12 '15

but what if you or someone else wants to mock out methods on a class later for testing?

7

u/lukaseder Aug 12 '15

You don't mock classes. You mock interfaces. Design for this approach!

3

u/voronaam Aug 12 '15

How is it there, in 2002? Just before PowerMock was created, I mean.

If you ever going to have one implementation of the interface - you do not need an interface. That is design, not throwing an interface as a facade for every bean!

1

u/lukaseder Aug 12 '15

This is going to be a fun discussion - yet - I'm seeing myself out. Cheers ;)

2

u/voronaam Aug 12 '15

It could've been - I do not usually throw such bold statements around. Next time, then.

P.S. I enjoyed Scala references, Scalatra is my framework of choice more often than not. And it is not interface(trait)-heavy ;)

2

u/anubus72 Aug 12 '15

so you're saying that every class I ever create has to have an interface as well, just so that I can create a mock class for testing? Certainly in some situations that's the right approach but I don't see why you would need to stick with that approach 100% of the time

4

u/lukaseder Aug 12 '15

No. I'm saying that those classes that deserve mocking (services, repositories, daos, what not) better be interfaces anyway. The rest, no need to mock.