In my early days I spent most of my time fixing bugs on a huge enterprise application, so by now I learned from experience that a lot of bugs could have been easily prevented. This is why I prefer a Functional Programming style, I love how FP handles state. As a software consultant I get to switch companies and teams quite regularly and most projects I have been working on use java 7 or 8. This almost always leads to a few dicussions regarding programming style. So today I would like to talk about good FP principles, and how Java makes them hard (and why languages like Kotlin are awesome).

Final keyword

Most of my variables (95%+) are usually immutable, and I would like my compiler to check this for me. In Kotlin we have val and var to declare variables, val being immutable and val being mutable. To make a variable non-mutable in Java, we need to use the final keyword before all variables, including parameters to get the behaviour I desire.

Nullability and contracts

Null is very dangerous in Java. In my opinion you should avoid passing it to a function as much as possible. Kotlin supports this really well since all arguments are non-null by default. If we declare a function like this:

fun someFunction(myValue: String) {}

myValue will be considered non-nullable, and thus Kotlin will give you a compilation error if you try to pass null. If we want to change myValue to be nullable, we have to do more work (add a ?):

fun someFunction(myValue: String?){}

So it is actually less work do do things properly in Kotlin, now lets take a look at what you can do in Java. In Java everything is nullable by default, so in practice a lot of programmers don’t think about nullability a lot. A good example is when a bug is fixed by doing a if(myValue == null) check. To add some nullability checking I love using JSR-305 @Nonnull and @Nullable annotations. They provide documentation for my functions, and a lot of tooling (eg: Intellij, sonar) can check for it.

public void foo(@Nonnull String arg1, @Nullable String arg2){}

The downside is that I have to put annotations in front of all my function arguments to get some of what Kotlin offers me out of the box. In short: it takes more effort to do things properly in Java.

Immutability

To make an immutable object in kotlin, you only need the data keyword

data class Pet(val name: String, val age: Int)

And you get the getters, equals, hashcode and copy methods for free. To change a property of an Immutable object, its a good practice to create a new, identical object, with the same values, except the one you want to change. Kotlin can do this with the copy method.

val myData = Pet("Max", 6)
val myNewData = myData.copy(age = 7)

To do this in Java, the only way is to call the whole constructor again.

Pet myData = Pet("Max", 6);
Pet myNewData = new Pet(myData.getName(), 7);

And this can get quite irritating with large constructors.

Conclusion

It takes quite a bit more effort to do good programming in Java. I see this as a reason to use a language like Kotlin, not as an argument not to bother with good programming in Java. So if you are unable to use Kotlin, please do use these principles and tricks in your Java code. For more information about why Kotlin is awesome, please do read my colleague’s blog: My move to kotlin.

shadow-left