Friday, May 7, 2010

java is verbose

a mountain of code is the worst thing that can befall a person, a team, a company - Steve Yegge
So, is Java verbose? I hadn't considered it before reading Steve Yegge's post. And I didn't really get it, initially. But I've been looking at my code differently since then. Recently I ran across another example.

I wanted to write a switch-case statement for about 10 different String possibilities. Last time I checked, you couldn't do this, but I hunted around to be sure. It turns out that you will be able to do this in Java 7. But I couldn't do it now.

One alternative is to use numerous if-then-else lines to sort through the possibilities. I much prefer switch-case; I find it cleaner and easier to read.

If I wanted to stick with switch-case for this example, I could go with one of several workarounds, all of which produce excessive code which ideally should be hidden from view. Probably the most "proper" way of doing this would be to create an enum listing all the constants. Talk about extra code just to make a decision based on a String value!

In the end I went with if-then-else. Ho-hum.