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?

3

u/CubsThisYear Aug 12 '15

You should never be mocking non-abstract methods. If you need to mock something, then by definition you have two implementations of the same functionality so you should have an interface for that functionality.

If you start mocking non-abstract methods, that means you are modifying your objects just for the purpose of testing. This will ultimately make your tests either brittle or incomplete.

1

u/[deleted] Aug 12 '15

Unit tests are, by design, incomplete. They are trying to test a tiny portion of your code in isolation.

The goal of unit tests isn't to tell if your system as a whole works. It is to quickly isolate failures when they do happen.

0

u/CubsThisYear Aug 12 '15

They are incomplete by necessity, not by design. If you could test your entire system while still making it relatively easy to change, you certainly would, given infinite time.

But none of that justifies using mocks on concrete classes. If you need to mock an implemented method you've either violated SRP or you are pretending something isn't pluggable when it clearly needs to be (otherwise you wouldn't be plugging in the mock). Your code now looks like it operates on a single implementation but it really doesn't.

1

u/voronaam Aug 13 '15

They are indeed incomplete by design. Whole system test are called integration tests. Unit tests are supposed to test a small unit of work in isolation, just like it has been pointed out already.