AnswerBun.com

Name for unnecessary transcoding antipattern?

Say I have a library function that expects a JSON-encoded object and does some things with it:

function foo(bar) {
    const baz = JSON.parse(bar);
    doTheThing(baz);
    doTheOtherThing(baz.qux);
}

Now I encounter an object that is not already JSON-encoded but I want to do the same things with it. Since I don’t want to touch the library that includes foo(), I just do this:

const bar = JSON.stringify(baz);
foo(bar);

That this is bad should be obvious; we should have a version of foo() that takes as input the unencoded object to avoid the wasted (and perilous) roundtrip through JSON. However, I’ve seen variations on this antipattern repeated many times in my career. It has nothing to do with Javascript or JSON; it could be Unicode encodings or image file formats or whatever, but the essence of the problem is that the cost of changing the existing code is perceived to be so high that we end up writing code that shoehorns data into some format just so it can be translated back into a useable format later.

I have to think that someone has previously described this problem. My search-fu is probably not just not good enough to find that discussion. Does this antipattern have a name?

Software Engineering Asked by Bart Robinson on December 30, 2020

1 Answers

One Answer

This is simply called bad factoring.

If it is at all conceivable that input might be made available in various forms, then there is no reason why a function should perform both decoding and processing inline, and in fact a very obvious reason why it shouldn't: because it very likely will lead to unnecessary code. And unnecessary code is bad because it opens the opportunity for defects. (Code that doesn't exist can't have defects.)

Functions are called "functions" for a reason. If you want a method that decodes and processes, then just compose a trivial two-liner from the two methods that fulfill one function each.

Edit: Oh, right. If you want a buzzword to hit management over the head with, you can invoke the venerable Single Responsibility Principle!

Answered by Kilian Foth on December 30, 2020

Add your own answers!

Related Questions

Mutexes at application logic level

1  Asked on December 24, 2021

   

When are enums NOT a code smell?

7  Asked on December 21, 2021 by stromms

         

How the basic structure of a simple messaging app works?

1  Asked on December 17, 2021 by rajodiya-jeel

       

SimpleFactory vs Factory Method

2  Asked on December 10, 2021 by asier-naiz

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.