# Code Smell 190 - Unnecessary Properties

      
> TL;DR: Don't focus on accidental properties. You won't need many of them.

# Problems

- Anemic Models

- Properties bloating

- YAGNI violation

# Solutions

- Create attributes only to support your methods (behavior).

# Context

Whenever they want to model a person or an employee, junior programmers or students add an attribute *'id'* or *'name'* without thinking if they are really going to need them.

We need to add attributes 'on-demand' when there's enough evidence. Objects are not 'data holders'.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/f3a9f762f2781017247f5acf6cf281a1)
```ruby
class PersonInQueue
  attr_accessor :name, :job

  def initialize(name, job)
    @name = name
    @job = job
  end
end
``` 

## Right

[Gist Url]: # (https://gist.github.com/mcsee/53d1777f204e64f5746a9a148ada934a)
```ruby
class PersonInQueue

  def moveForwardOnePosition
    # implement protocol
  end
end
``` 

# Detection

[X] Semi-Automatic

We can detect unused attributes. 

But in many cases, we need an excellent designer to validate the actual need.

# Tags

- Anemic

# Conclusion

Start designing your objects from the protocol. 

Add attributes only when needed.

# Relations

%[https://maximilianocontieri.com/code-smell-144-fungible-objects]

%[https://maximilianocontieri.com/code-smell-01-anemic-models]

%[https://maximilianocontieri.com/code-smell-40-dtos]

%[https://maximilianocontieri.com/code-smell-109-automatic-properties]

# Credits

Photo by [Melanie Pongratz](https://unsplash.com/@melanie_sophie) on [Unsplash](https://unsplash.com/photos/SsBI9pweAeA)
 
---

> Object thinking focuses our attention on the problem space rather than the solution space.

_David West_

%[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]
