Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 35 — State as Properties

Maximiliano Contieri
Dev Genius
Published in
2 min readNov 27, 2020
Photo by Tom Crew on Unsplash

Problems

  • Mutability
  • Attributes polluting
  • Setters

Solutions

  1. Model states as mathematical set inclusion.
  2. State is accidental, take it away from the object.

Examples

  • State diagrams

Sample Code

Wrong

Right

Detection

If we want to be extreme, we should consider every setter to be a potential state change. Linters can warn us. But we might end up getting too many false positives.

Exceptions

  • Over Design
  • Performance issues (if a serious benchmark supports it).

Tags

  • Mutation

Conclusion

This technique is very elegant but can lead to over design. For example changing a visual component its color should be a counterexample to this smell.

We should be aware and very caution like with any other smell.

They are hints and not rigid rules.

Relations

More info

First make the change easy (warning: this might be hard), then make the easy change.

Kent Beck

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response