DEV Community

Cover image for Code Smell 34 - Too Many Attributes
Maxi Contieri
Maxi Contieri

Posted on • Updated on • Originally published at maximilianocontieri.com

Code Smell 34 - Too Many Attributes

A class defines objects with lots of attributes.

Problems

  • Low Cohesion

  • Coupling

  • Maintainability

  • Readability

Solutions

  1. Find methods related to attributes.
  2. Cluster these methods.
  3. Break the object related to those clusters.
  4. Find real objects related to this new objects and replace existing references.

Examples

  • DTOs

  • Denormalized table rows

Sample Code

Wrong

Right

Detection

Most linters warn when you declare too many attributes. Setting a good warning threshold should be easy.

Tags

  • primitive

Conclusion

Bloated objects know too much and are very difficult to change due to cohesion.

Developers change these objects a lot, so they bring merge conflicts and are a common problems source.

Relations

Credits

Photo by Andy Li on Unsplash


So much complexity in software comes from trying to make one thing do two things.

Ryan Singer

Top comments (7)

Collapse
 
gintama profile image
gin-tama

two sample codes are the same.

Collapse
 
gouravkhunger profile image
Gourav Khunger • Edited

Yeah, I was comparing them for 10 mins lol

Collapse
 
mcsee profile image
Maxi Contieri

sorry. there's a bug in dev.to

This is not the first time they report it on my code smell articles.
files ARE different and of course code is different.
They have commented it is a bug in Mobile App.

Will report it to dev.to team

I'm leaving the hyperlinks here as plan B

Wrong
gist.github.com/mcsee/b6c664aef324...

Right
gist.github.com/mcsee/c34dd227f16b...

Thread Thread
 
gouravkhunger profile image
Gourav Khunger

Ah, I see!
I was viewing it from the PWA so I guess I got to see the same.
Anyways, Well written!

Thread Thread
 
mcsee profile image
Maxi Contieri

thank you!!

Collapse
 
golovpavel profile image
Golov Pavel

I think that your solution solves only the readability problem. Is it so?

Collapse
 
mcsee profile image
Maxi Contieri

No. it also creates testable abstractions and cohesive new objects. will add to code to clarifiy