Inspired by Cedric Beust’s thoughts on coding conventions, I present my contribution to the discipline of writing unmaintainable Java code. These hints and tips have been collected from a variety of Java projects, over many years.1 In the hands-on spirit of industry-standard, panic-driven development, this list concerns itself purely with low-level coding and cowboy-style pseudo-design techniques.
In no particular order:
null rather than a zero length array. This causes every for loop iterating over the array to need wrapping with if (array != null). A similar effect can be achieved when coding with collection classes.object.getX().getX(). For bonus unmaintenance points, ensure that getX() and getX() return different types.
if (something1()
&& something2()
&& something3()
&& something4()
&& something5()
&& something6()
&& something7()
&& something8()
&& something9()) {
...
long constants, use a lower-case ‘l’ in preference to upper-case ‘L’. Even with plain font such as Courier, 1l can cause all kinds of havoc. And with proportional san-serif fonts being supported by more and more IDEs, things can only get less clear and more unmaintainable.boolean values in a condition. The classic example is ”if (number.isEven() == true) {…”, a trifle which is easily ignored by a maintenance programmer. However, given the right opportunity and a small amount of thoughtlessness, this simple device can finesse one line of code into five:
if (number.isEven() == true) {
return true;
} else {
return false;
}
PS: Most of these are not fatal—they are simple to correct, and would be caught in even a cursory code review. Even so, I am thankful that I haven’t come across a single project that has put all of these into practice.
1 It’s just a bunch of notes really, but there would be too much irony in any more formal presentation.
2 I couldn’t be bothered to work out which two were not being used, so I left them all there while I added a new one.
My son Connor is five. I asked him about his week at school.
Connor’s favourite thing for the whole week was ‘Toy Time’. Every toy time he played with the Duplo; he says he “never takes a break from it.” Today Connor made a boat. The friends who helped him were Jarrod and Thomas.
The boat was not very big, but it was very long. It went across the whole room. Connor tells me they used it as a boat, but it actually was a bridge.