DEV Community

loading...
Cover image for Code Smell 55 - Object Orgy

Code Smell 55 - Object Orgy

mcsee profile image Maxi Contieri Updated on ・1 min read

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.

Problems

  • Information Hiding Violation

  • Encapsulation Violation

  • Coupling

Solutions

  1. Couple to interfaces and behavior, never data.

Sample Code

Wrong

Right

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

Wikipedia

Refactoring Guru

C2

Credits

Picture by Nicolas Poussin


A data structure is just a stupid programming language.

Bill Gosper

Discussion (0)

pic
Editor guide