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.

9
  • 3
    Recommended reading: Naming considered harmful: 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
    Commented Jan 15, 2018 at 13:12
  • 1
    Call it The Daily WTF, if you like.
    – Doc Brown
    Commented Jan 15, 2018 at 13:27
  • 4
    ... or simply Overengineering, if you prefer that.
    – Doc Brown
    Commented Jan 15, 2018 at 13:33
  • 1
    Have the author of the code read How would you know if you've written readable and easily maintainable code? Commented Jan 15, 2018 at 14:44
  • 1
    Good grief! This example should go into a museum of horrors, alongside pickled body parts.
    – Steve
    Commented Jan 15, 2018 at 22:51

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.