Dev Genius

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

Follow publication

Code Smell 37 — Protected Attributes

Protected attributes are great for encapsulating and controlling access to our properties. They might be warning us for another smell.

Photo by Jonathan Farber on Unsplash

Problems

Solutions

  1. Favor composition
  2. Don’t subclassify attributes.
  3. Extract behavior to separate objects.
  4. Use traits (if available).

Wrong

Right

Detection

In languages supporting protected attributes we can avoid them by policy or have a warning of this smell.

Tags

  • Encapsulation

Conclusion

Protected attributes are yet another tool we should use carefully. Every decision is a smell, and we should be very careful with attributes and inheritance.

Relations

More Info

Traits on Wikipedia

Subclasses shouldn’t always share all characteristics of their parent class but will do so with inheritance. This can make a program’s design less flexible. It also introduces the possibility of calling methods on subclasses that don’t make sense or that cause errors because the methods don’t apply to the subclass.

Steve Klabnik

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