DEV Community

Cover image for Code Smell 174 - Class Name in Attributes
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

11

Code Smell 174 - Class Name in Attributes

Redundancy in names is a bad smell. Names should be contextual

TL;DR: Don't prefix your attributes with your class name

Problems

  • Not Contextual Names

Solutions

  1. Remove the class prefix from the attribute

Context

This is a naming smell, we should not read attributes in isolation and names are contextual.

Sample Code

Wrong

public class Employee {
   String empName = "John";
   int empId = 5;
   int empAge = 32;
}
Enter fullscreen mode Exit fullscreen mode

Right

public class Employee {
   String name;
   int id; // Ids are another smell
   int age; // Storing the age is yet another smell
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

When the full name is included in the prefix, our linters can warn us.

Tags

  • Naming

Conclusion

Careful naming is a very important task.

We need to name after the behavior, not type or data

Relations

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Phoenix Han on Unsplash


Copying skips understanding. Understanding is how you grow. You have to understand why something works or why something is how it is. When you copy it, you miss that. You just repurpose the last layer instead of understanding all the layers underneath.

Jason Fried


This article is part of the CodeSmell Series.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
wadecodez profile image
Wade Zimmerman

Any sort of prefix is generally a bad idea. Databases and CSS are notoriously bad about this.

IMO if you have to prefix something for it to work or make sense, then there is something else wrong.

Collapse
 
mcsee profile image
Maxi Contieri

indeed

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay