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

Remove the class prefix from the attribute

Context


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri

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;
}

Right

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

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.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2022-10-29T16:27:21+00:00) Code Smell 174 – Class Name in Attributes. Retrieved from https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/

MLA
" » Code Smell 174 – Class Name in Attributes." Maxi Contieri | Sciencx - Saturday October 29, 2022, https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/
HARVARD
Maxi Contieri | Sciencx Saturday October 29, 2022 » Code Smell 174 – Class Name in Attributes., viewed ,<https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 174 – Class Name in Attributes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/
CHICAGO
" » Code Smell 174 – Class Name in Attributes." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/
IEEE
" » Code Smell 174 – Class Name in Attributes." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/. [Accessed: ]
rf:citation
» Code Smell 174 – Class Name in Attributes | Maxi Contieri | Sciencx | https://www.scien.cx/2022/10/29/code-smell-174-class-name-in-attributes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.