DEV Community

Joe Eames for Thinkster

Posted on

Code Smell: Array Data Clumps

This is a specific case of the more general "Primitive Obsession" code smell. That code smell is very generic and includes many different variations. This is one of the variations that I think is worth discussing, as it teaches us a subtlety of the proper use of data structures.

This code smell is about Arrays and centers around the kinds of data we have in them. Let's look at a couple of example arrays used properly.

image

Now let's compare them to one that fits our code smell:

image

Can you see the difference? We can also find this code smell by watching how an array is accessed:

image

What's wrong with this use of an array? It centers on how to properly structure our data. An array is a collection of items. Used properly, it's a collection of the same type (not data type) of item. But we aren't constrained to follow this.
Even when those items are all the same data type, they can ultimately be different kinds of data. That's what we have in our misbehaving array. Four strings, but each one is not the same "thing" as the other. When we have an array that has different "things" in it, then we tend to place things and access things in an array by specific index, and rarely as a complete collection.

Why is that a problem? It hides intentionality, and reduces expressiveness and therefore readability. When we want our street address, we should ask for a property named something like address1, and not the item at index 1. We're using an inappropriate data type, and over time this costs us in cognitive load, flexibility, and ultimately costs time and therefore money.

Why does this situation arise? Usually, it's because we either have an existing API that wants to accept or return an array, or we may find a clever way to loop through the elements in an array and use them which is quicker now than coding it as an object/class. Regardless of how we get into this situation, as all code smells are, it will usually cost us in the long run.

So replace that array with a more proper data type.

image

There's one variation of this that's worth mentioning specifically. This one:

image

This is no better. Sure we can loop through the array by 2's and the even index is the state, and the odd index is the capital, but this is still the same problem. This should be an array of objects, each with a state and capital property.

Proper use of data types (and here I don't just mean strings and numbers) is one of the foundations of any application. Misusing those data types for convenience today will usually cost us greatly in maintenance and rigidity. So keep those data types tight.

Signup for my newsletter here.

Happy Coding!

Visit Us: thinkster.io | Facebook: @gothinkster | Twitter: @gothinkster

Top comments (0)