I've been working quite a lot in Java. Although this is part of the university course, we actually have some QAs we need to satisfy, and so the quality of the code matters. Most modern languages are Turing Complete, so it is rarely about what you can do, but rather how you can do it. Every now and again (more often than not actually), I came across painfully annoying Java constructs which make the code that much harder to read and write. Like today, I placed my return statement in finally
block (completely by accident). My code had a similar shape*:
1 2 3 4 5 6 7 8 9 10 |
int value = 0; try { value += 1; return value; } catch(Exception e) { } finally { return value; } //the return value was meant to be here (not in the finally) |
Lo and behold, I was getting 0 all the time. At first, I didn't really notice the fact that this is where I placed in the finally block. Even when I discovered it was few moments before I made the connection (inference that the value was 0 all the time). Just for fun, I did the same thing in C#. Instead of ambiguous code, you get a compiler error as a change of flow is not allowed in the finally
.
So yeah... The Java way.