What’s the worst sort of code?
Nope, not Perl either.
Ok, so Perl is quite irritating.
The Worst Sort of Code…
It’s code we don’t need… followed by code that almost works, and then code that doesn’t work.
Code that almost works is very annoying, because you won’t realise it doesn’t work at first.
Code that almost works and we don’t need… there’s the perfect storm.
High Minded Best Intentions
We all want to do the best job possible. The creative process of development is full of what-ifs, and wouldn’t-it-be-greats.
Sometimes the developer wants to go the extra mile. If you can do that cheaply and there’s a known up-coming requirement, then that’s lovely. Brilliant.
Indeed, sometimes it’s actually easier to solve the general case for all use cases, rather than solve the subset in front of us.
However, we have to be self-critical about the time and risk we place on a solution that’s few steps beyond the immediate requirements. This is sometimes because the all-singing-general-purpose solution can be much more complex, imply some tricky edge cases and… wait for it… not quite work.
This can be a case where the attempt to provide two solutions for the price of one can produce no fully working solutions to either problem.
Let’s imagine we receive input text in the format
"A B C" where there is a space between every character and output text in the format
"ABC" where there’s not. With the spacing the string
"The cat" would become
"T h e c a t".
We can easily make an algorithm to convert between these formats. No big deal. We can also employ the space adder or the space remover, depending on which format we expect to be dealing with.
Let’s say someone tries to make the converting algorithm detect if we have the interlaced spaced and automatically do the conversion to output format if necessary. That might be nice. We don’t have to have two entry points and the caller doesn’t need to know which format is which. Except… what of the string
"A " – which format is that in?
By attempting to handle all possible inputs with general purpose method, we can make an unprocessable edge case possible.
The above is an extreme example of a bit of attempted sophistication backfiring. The general case is that it’s often wasteful to solve problems we don’t have, even more so if the solution we end up with cannot be made to work as well as a simpler solution to the problem we DO have.
Opinions expressed by Java Code Geeks contributors are their own.