The goto legacy in Java
AKA “Why use a function when you can make a program less readable?”
While refactoring a method, I’ve stumbled upon a syntax I’ve never seen before in Java code: the “break label” syntax.
To summarize, when I saw the label standing alone in its line, I immediately recognized a “goto”-like statement, however as this is java code I was not expecting that to be possible.
I know the break and continue statements to handle jumps in loops, and these can be handy at times, but in general I prefer to encapsulate the loop in a method and return instead of breaking — which does the same thing in the end — but it allows to separate the logic and refactor into smaller and more maintainable methods.
So when I discovered this possibility in the syntax, I had a bad feeling about it.
As I mentioned initially, my bias is that I think it makes the code less readable, because it adds a layer of complexity in the understanding of the code. Reading and understanding the (legacy or to-be legacy) code are in my opinion very important aspects to think about for programmers, because you usually don’t code for yourself, and you can face your own code months or years later and curse your past self because of that.
Next step for this code will of course be to refactor it. Maybe I’ll post something later on about it to share the fun.
I will end by summarizing my opinion with this: it’s not because a language allows you to do something that you should do it. Think before doing :-)
Got any similar experience? Happy to read from you!