1

Here is a piece of cool code to add the two integers, a and b:

NameService nameService = NameService.getSingletonInstance();
OperationService operationService = nameService.resolve(OperationService.class);
ValueFactory factory = OperationServiceFactory.newInstance();
AbstractValue va = factory.newIntegerValue(a);
AbstractValue vb = factory.newIntegerValue(b);
Operator operator = operationService.resolve(AdditonService.class);
AbstractValue vc = operator.performOperation(a, b);
int c = nameService.resolve(IntegerDecorator.class).getValue(vc);

Unfortunately I cannot paste the actual production code that does a little bit more (while not much) but the problem should be obvious: there is much more code than minimally required and despite of "design patterns applied" it is very difficult to read or refactor it. It can be written much simpler and shorter but the author of the code claims you being unprofessional for saying so. Where is the problem with the code? It really could not be that there is no any.

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
h22
  • 905
  • 1
  • 5
  • 15
  • 3
    Recommended reading: [Naming considered harmful](https://the-whiteboard.github.io/2017/11/21/naming-considered-harmful.html): *STOP IT. Avoid naming things that don’t need to have names. This was bad code. We gave it a name and joked about it. But now, I’ve got a name for it. It is not a good thing. This code already had a name before we christened it – it is Bad Code. __Use names for things that you want to remember__.* – amon Jan 15 '18 at 13:12
  • 1
    Call it [The Daily WTF](https://thedailywtf.com/), if you like. – Doc Brown Jan 15 '18 at 13:27
  • 4
    ... or simply [Overengineering](https://en.wikipedia.org/wiki/Overengineering), if you prefer that. – Doc Brown Jan 15 '18 at 13:33
  • 1
    Have the author of the code read [How would you know if you've written readable and easily maintainable code?](https://softwareengineering.stackexchange.com/q/141005/64132) – Dan Pichelman Jan 15 '18 at 14:44
  • `getSingletonInstance`. There’s your first anti-pattern right there. It’s called the singleton anti-pattern. – David Arno Jan 15 '18 at 17:58
  • 1
    Good grief! This example should go into a museum of horrors, alongside pickled body parts. – Steve Jan 15 '18 at 22:51
  • This is a [Coding Horror](https://blog.codinghorror.com/) if I ever saw one. – Mark Benningfield Jan 15 '18 at 22:53
  • Trying to coin a name for it, I'll pick "embroidering". – Flater Feb 09 '18 at 16:05
  • @amon: You'll want to remember bad things too, so you can stay away from them. I wouldn't fly a plane into the Bermuda triangle. If I had never heard of it before, I wouldn't be too fussed about flying into it (and presumably regret it later). As long as the name is attributed to the _entire range of possibilities for the same mistake_, and not just a particular subset of it; naming it seems fine (e.g. the northern half of the Bermuda triangle doesn't need a unique name to warn pilots flying into it). "Bad code" is an overly terse summary, and not enough to identify the problem ahead of time. – Flater Feb 09 '18 at 16:08

1 Answers1

7

You could call this Cargo cult programming where the programmer uses patterns without fully understanding why. Before you go accusing someone of this, make sure you understand the reasons behind these patterns yourself.

This isn't the actual production code, you said so yourself. Therefore you have to seriously consider the possibility that this might be even necessary under the circumstances. Even the code you wrote might actually be feasible in the right circumstances, such as in a code parser where operators and operands are all potentially dynamic.

Perhaps the author of this code merely meant to be as flexible as possible. It is questionable whether or not the added flexibility is worth the lack of readability.

Neil
  • 22,670
  • 45
  • 76