# Code Smell 70 - Anemic Model Generators

*Anemic Models are the problem. Anemic Model Generators are the Meta Problem.*

> TL;DR: Do not create anemic objects. Much less with automatic tools.

# Problems

- Anemic Objects

- Coupling to Implementation

- Harder to Debug

# Solutions

1. Create your objects manually.

2. Focus on essential behavior instead of accidental data storage.

# Sample Code

## Wrong

[//]: # (https://gist.github.com/mcsee/f930ccb9f2a14798aea9c6b96977b391)

```php
<?

AnemicClassCreator::create(
    'Employee',
    [
        new AutoGeneratedField('id', '$validators->getIntegerValidator()'),
        new AutoGeneratedField('name', '$validators->getStringValidator()'),
        new AutoGeneratedField('currentlyWorking', '$validators->getBooleanValidator()')
    ]);

class Employee extends AutoGeneratedObjectEmployee {
}

//We have magic setters and getters
//getId() , setId(), getName()
//Validation is not implicit
//Class are loaded via an autoloader

$john = new Employee;
$john->setId(1);
$john->setName('John');
$john->setCurrentlyWorking(true);

$john->getName(); //returns 'John'
```


## Right

[//]: # (https://gist.github.com/mcsee/366d7d78b0952ac088125f75c2bc4035)

```php
<?

final class Employee {
    private $name;
    private $workingStatus;

    public function __construct(string $name, WorkingStatus $workingStatus) {
        //..
    }

    public function name(): string {
        return $this->name;
        //This is not a getter. It is Employee's responsibility to tell us her/his name
    }
}

//We have no magic setters or getters
//all methods are real and can be debugged
//Validations are implicit

$john = new Employee('John', new HiredWorkingStatus());

$john->name(); //returns 'John'
```


# Detection

Often, anemic models are generated with [meta-programming](https://maximilianocontieri.com/laziness-i-meta-programming). 

We need to track these magic [code generators](https://maximilianocontieri.com/lazyness-ii-code-wizards).

# Tags

- Anemic

# Conclusion

Code Wizards, Meta-programming, and anemic models are all code smells.

We need to avoid these dark techniques.

Having to write *explicitly* the code makes us reflect on every piece of data we encapsulate.

# Relations

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

# More info

%[https://maximilianocontieri.com/laziness-i-meta-programming]

%[https://maximilianocontieri.com/lazyness-ii-code-wizards]

# Credits

Photo by <a href="https://unsplash.com/@lennykuhne">Lenny Kuhne</a> on <a href="https://unsplash.com/s/photos/factory">Unsplash</a>
  
* * *

> The best smells are something that's easy to spot and most of time lead you to really interesting problems. Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should be in this class.

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