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.

3

u/valenterry Aug 11 '15

You can also return null values randomly and throw errors randomly from your library. How is it your fault if the callers program breaks? He could have caught the nulls and the errors.

But if you want to write software that does not break so easy, you should better make everything final and thus not use inheritance at all. Also never return null. It helps everyone who uses your code.

Though, Java is a language that can make it quite hard to do those things, because e.g. there is no help for delegating all method calls to an internal object. So not using inheritance means you have to implement all the methods of the interface of the object you want to delegate to yourself. That's kinda annoying, so it might be an excuse still to use inheritance. However it's not an execuse for using Java at all!

3

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/pakoito Aug 12 '15

Also never return null.

I try to abide by this, but what do you do for cases where it's difficult to find an alternative, i.e. convert string to enum by equals.

Optional is Java8, we're working on Java6 still.

1

u/valenterry Aug 13 '15

As I said, if your language puts a spoke in your wheel, change that first. In Java 6 you can still create your own Option type (there are also already defined types you can use (e.g. in Google Guava) but they are of course not as comfortable without lambdas. =/

0

u/[deleted] Aug 11 '15

[deleted]

6

u/lookmeat Aug 12 '15

You know, one day a programmer will come over to your code, not knowing or caring to know what it was trying to do, will assume s/he knows more than you and then will screw everything up. And that programmer will be you.

2

u/isHavvy Aug 12 '15

The programmer has to stop for a moment and think about why it is final. They have to reason that no code will break with the invariant removed, or if it does, they have to fix it.

In code review, it gives the reviewer the impetus to check that too.

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?

1

u/vitalyd Aug 12 '15

If you agree that virtual extension points are a type of API and APIs need forethought, then you'll likely see why preventing unintended/"undesigned" extensibility is a good default to pick.

0

u/Eirenarch Aug 11 '15

I do follow the all methods are final because in C# that's the default. I guess Anders (Hallowed be His name!) agrees that all methods should be final :)

4

u/sh0rug0ru___ Aug 11 '15 edited Aug 11 '15

<pendantry>

All non-static methods are virtual in Java. Regardless of the pointer you have to the object (whether it is the concrete type or a super type), the same method will be called. Always.

On the other hand in C#, instance methods are by default non-virtual and cannot be overridden. But, that does not mean they cannot be overloaded. Thus, a pointer of the concrete type and a pointer of a super type can actually invoke different methods!

The equivalent to Java's final in C# is actually sealed. Which is not the default!

</pedantry>

1

u/Eirenarch Aug 11 '15

What? I am pretty sure final on a method means non-virtual in Java. final on a class means what sealed means in C#

5

u/sh0rug0ru___ Aug 11 '15 edited Aug 11 '15

If you look at the bytecode generated on a method call to a final method, you will see invokevirtual. All instance methods in Java are virtual, even final methods (except for constructors and private methods - invokespecial, and interfaces, oddly enough thorugh invokeinterface. Oh yes, and now invokedynamic). But this would have to be the case. What if you inherited from a non-final method and made your override final? This is exactly what sealed does on a virtual method or property in C#.

However, that is only half the story. The JVM can "devirtualize" methods, even non-final methods, based on class-hierarchy analysis. Paper if you're interested.

Thus, final or not final doesn't make a difference in Java at runtime. The JVM will figure out whether or not the method should be virtual or not. I would say final is more for enforcing constraints at compile-time.

1

u/vitalyd Aug 12 '15

Final allows JIT to skip registering class load dependencies since it knows the target will never change.