# Code Smell 136 -  Classes With just One Subclass

*Being generic and foreseeing the future is good (again).*

> TL;DR: Don't over-generalize

# Problems

- Speculative Design

- Complexity

- Over-Engineering

# Solutions

1. Remove the abstract class until you get more examples

# Context

In the past, programmers told us to design for change. 

Nowadays, We keep following the scientific method. 

Whenever we find a duplication we remove it. 

Not before. 

Not with interfaces, not with classes.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/ce3cbe785c4b1d542a2b0660acabbc8f)
```python
class Boss(object):
    def __init__(self, name):
        self.name = name 
        
class GoodBoss(Boss):
    def __init__(self, name):
        super().__init__(name)
        
# This is actually a very classification example
# Bosses should be immutable but can change their mood
# with constructive feedback
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/441d6bf98d916336dbfa2670d7109640)
```python
class Boss(object):
    def __init__(self, name):
        self.name = name  
        
# Bosses are concrete and can change mood
```

# Detection

[X] Automatic 

This is very easy for our linters since they can trace this error at compile time.

# Exceptions

Some frameworks create an abstract class as a placeholder to build our models over them.

Subclassifing should be never our first option. 

A more elegant solution would be to declare [an interface](https://maximilianocontieri.com/code-smell-135-interfaces-with-just-one-realization) since it is less coupled.

# Tags

- Over Design

# Relations

%[https://maximilianocontieri.com/code-smell-114-empty-class]

%[https://maximilianocontieri.com/code-smell-11-subclassification-for-code-reuse]

%[https://maximilianocontieri.com/code-smell-43-concrete-classes-subclassified]

%[https://maximilianocontieri.com/code-smell-92-isolated-subclasses-names]

%[https://maximilianocontieri.com/code-smell-135-interfaces-with-just-one-realization]

# Conclusion

We need to wait for abstractions and not be creative and speculative.

# Credits

Photo by [Benjamin Davies](https://unsplash.com/photos/9b5dvrjb05g#:~:text=Photo%20by%20Benjamin%20Davies%20on%20Unsplash) on Unsplash

* * *

> Writing a class without its contract would be similar to producing an engineering component (electrical circuit, VLSI (Very Large Scale Integration) chip, bridge, engine...) without a spec. No professional engineer would even consider the idea.

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