Code Smell 27 - Associative Arrays
Maxi Contieri
Posted on November 18, 2020
[Key, values], magic, fast, malleable and error prune.
TL;DR: Use arrays for rapid prototyping, use object for serious business.
Problems
Coupling
Information Hiding
Code Duplication
Fail Fast
Integrity
Solutions
Reify objects
Create cohesive small objects
Don't leave them anaemic, find their cohesive relations.
Sample Code
Wrong
<?
$coordinate = array('latitude'=>1000, 'longitude'=>2000);
//Its OK. they are just arrays. a Bunch of raw data
Anaemic
<?
final class GeographicCoordinate {
function __construct($latitude, $longitude) {
$this->longitude = $longitude;
$this->latitude = $latitude;
}
}
$coordinate = new GeographicCoordinate(1000, 2000);
//should throw an error since these values don't exist on Earth
Validated
<?
final class GeographicCoordinate {
function __construct($latitude, $langitude) {
if (!$this->isValidLatitude($latitude)) {
throw new InvalidLatitudeException($latitude);
//...
$this->longitude = $longitude;
$this->latitude = $latitude;
}
}
}
$coordinate = new GeographicCoordinate(1000, 2000);
//trows an error since these values don't exist on Earth
Right
Degrees deserves reification
<?
final class Latitude {
function __construct($degrees) {
if (!$degrees->between(-90, 90)) {
throw new InvalidLatitudeException($latitude);
}
//...
}
}
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.
<?
final class GeographicCoordinate {
function distanceTo(GeographicCoordinate $coordinate) {
}
function pointInPoligon(Polygon $polygon) {
//....
}
}
//Now we are in geometry world (and not in array world anymore). we can safely do many exciting things.
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
Code Smell 01 - Anemic Models
Maxi Contieri ・ Oct 20 '20
Credits
Photo by Melissa Askew on Unsplash
There’s nothing more permanent than a temporary hack.
Kyle Simpson
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
Last update: 2021/09/24
Posted on November 18, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.