DEV Community

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

Posted on • Edited 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

class ExcelSheet {
String filename;
String fileEncoding;
String documentOwner;
String documentReadPassword;
String documentWritePassword;
DateTime creationTime;
DateTime updateTime;
String revisionVersion;
String revisionOwner;
List previousVersions;
String documentLanguage;
List cells;
List cellNames;
List geometricShapes;
}
view raw polluted.dart hosted with ❤ by GitHub

Right

class ExcelSheet {
FileProperties fileProperties;
SecurityProperties securityProperties;
DocumentDatingProperties datingProperties;
RevisionProperties revisionProperties;
LanguageProperties languageProperties;
DocumentContent content;
}
// Object has less attributes
// They are not only grouped for testability
// New objects are more cohesive, more testable,
// less conflicts and more reusable
// FileProperties/SecurityProperties
// can be reused for other documents
// Rules and preconditions on fileProperties
// will be moved to this object
// so ExcelSheet constructor will be cleaner
view raw cohesive.dart hosted with ❤ by GitHub

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more