# Code Smell 174 - Class Name in Attributes

> 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

```java
public class Employee {
   String empName = "John";
   int empId = 5;
   int empAge = 32;
}
```

## Right

```java
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

%[https://maximilianocontieri.com/code-smell-188-redundant-parameter-names] 

%[https://maximilianocontieri.com/code-smell-141-iengine-avehicle-implcar] 

%[https://maximilianocontieri.com/code-smell-96-my-objects] 

# More Info

%[https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab] 

* [Linux Hint](https://linuxhint.com/java-class-attributes/)
    

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Phoenix Han](https://unsplash.com/@phienix_han) on [Unsplash](https://unsplash.com/s/photos/mushroom)

---

> 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*

%[https://maximilianocontieri.com/software-engineering-great-quotes] 

---

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
