cover-img

Code Smell 27 - Associative Arrays

Use arrays for rapid prototyping, use object for serious business.

21 November, 2022

7

7

1

[Key, values], magic, fast, malleable and error prone.
TL;DR: Use arrays for rapid prototyping, use object for serious business.

Problems

Coupling

Information Hiding

Code Duplication

Fail Fast

Integrity

Solutions

1.

Reify objects

2.

Create cohesive small objects

3.

Don't leave them anaemic, find their cohesive relations.

Sample Code

Wrong

Anaemic

Validated

Right

Degrees deserves reification

Many people suffer from primitive obsession and believe this is over design. Designing software is about making decisions and comparing trade-offs. The performance argument is not valid nowadays since modern virtual machines can efficiently deal with small short-lived objects.

Detection

We cannot forbid Associative Arrays since they are very good as a first approach.
They will be fine for exporting data, serialization, persistence and other accidental implementation issues.
We should avoid them on our systems.

Tags

Primitive

Conclusion

When creating objects, we must not think of them as data. This is a common misconception.
We should stay loyal to our Bijection and discover real world objects.
Most associative arrays have cohesion and represent real world entities, and we must treat them as first class objects.

Relations

Credits

There’s nothing more permanent than a temporary hack.

Kyle Simpson
This article is part of the CodeSmell Series.

7

7

1

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.