I was made for coding you baby!

Introduction

In this article I am going to talk about complexity and overengineering. Many software developers, myself included, tend to think they are super cool just because they can handle a specific problem with the newest and fanciest framework, library or API out there. “But what does this have to do with kissing?”, you might ask. Well, KISS stands for Keep it simple, stupid! and gives you good advice about how you should write your code: Simple!

The Problem with this problem

The first time I started using Javas Streams API I felt incredibly smart and super up to date. I used it on every single List in my code just for the sake of doing so. Even Arrays got turned into a List, just to be cool.

[pastacode lang=”java” manual=”String%20firstItem%20%3D%20Arrays.asList(array).stream().findFirst().get()%3B” message=”” highlight=”” provider=”manual”/]
instead of

[pastacode lang=”java” manual=”String%20firstItem%20%3D%20array%5B0%5D%3B” message=”” highlight=”” provider=”manual”/]

But the downside was a more written code. And even worse, way less understandable code. Ain’t I sooooo cool!

Recatorying baby

A real world example I wrote looks as follows:

[pastacode lang=”java” manual=”%20%20%20%20Map%3CString%2C%20Person%3E%20personMap%20%3D%20personList.stream().filter(p%20-%3E%20(p.getAge()%20%3C%2050))%0A%20%20%20%20%20%20%20%20%20%20%20%20.collect(Collectors.toMap(Person%3A%3AgetName%2C%20Function.identity()))%3B” message=”” highlight=”” provider=”manual”/]

This seems pretty short and everyone used to the Stream API can read it perfectly fine. But you have to admit that for someone not knowing about streams, the lamda operator or method references, this single line can be pretty intimidating. On the other hand everyone who has read the first 253 pages of understands:

[pastacode lang=”java” manual=”Map%3CString%2C%20Person%3E%20personMap%20%3D%20new%20HashMap%3CString%2C%20Person%3E()%3B%0A%20%20%20%20for%20(Person%20person%20%3A%20personList)%20%7B%0A%20%20%20%20%20%20%20%20if(person.getAge()%20%3C%2050)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20personMap.put(person.getName()%2C%20person)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D” message=”” highlight=”” provider=”manual”/]

Which does exactly the same.

What have we learned?

Nowadays I know that doing things just for the sake of doing it can be incredibly dumb. I am not saying that you should stay away from new functionalities. But keep in mind that the framework you are using should suite the problem and not be the cause them. Just make sure to write your code so that you will be able to understand it at a single glimpse. Simple, isn’t it.

Categories: Blog Posts