Skip to main content

Command Palette

Search for a command to run...

Code Smell 55 - Object Orgy

Updated
2 min read
Code Smell 55 - Object Orgy
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

If you see your objects as data holders you will violate their encapsulation, but you shouldn't, as in real life, you should always ask for consent.

TL;DR: Don't mess with other object's data.

Problems

  • Information Hiding Violation

  • Encapsulation Violation

  • Coupling

Solutions

  1. Couple to interfaces and behavior, never data.

Sample Code

Wrong

<?

final class Point {
    public $x;
    public $y;
}

final class DistanceCalculator {
    function distanceBetween(Point $origin, Point $destination) {
        return sqrt((($destination->x - $origin->x) ^ 2) + (($destination->y - $origin->y) ^ 2));
    }
}
<?

final class Point {
    private $rho;
    private $theta;

    public function x() {
        return $this->rho * cos($this->theta);
    }

    public function y() {
        return $this->rho * sin($this->theta);
    }
}

final class DistanceCalculator {
    function distanceBetween(Point $origin, Point $destination) {

        return sqrt((($destination->x() - $origin->x() ^ 2) + (($destination->y() - $origin->y()) ^ 2)));
    }

}

Detection

You can set your linters to warn you for public attributes, setters and getters usage and discourage them.

Tags

  • Coupling

Conclusion

If your classes are polluted with setters, getters and public methods you will certainly have ways to couple to their accidental implementation.

Also Known as

  • Inappropriate intimacy

Relations

More info

Credits

Picture by Nicolas Poussin


A data structure is just a stupid programming language.

Bill Gosper



This article is part of the CodeSmell Series.

Last update: 2021/06/14

B

Amazing Content bro! I love code smell content, I read them all, I'm not a hardcore (biology background self-taught programmer) but I had no trouble following up, I wish I can write like you! I'm officially a fan 😇

B

Also, I have not made my first blog yet, but if do one day, I would like to have your blessing to imitate your style :)

S

I am wondering until where you are going to go... Already 55 code smells. Really good series I recommend to any software engineer working with OOP. Quick question, do you use all of them? Some can be detected by linters some not how do you deal with those? Some are also obvious with the experience so maybe the final list is not that big.

1
M

Thank you

I try to follow uncle bob's boy scout rule

If I see some of these smells I try to refactor them.

i directly avoid many of them . But I work in a huge codebase with a lot of people so the code has lots of layers

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.