# Code Smell 182 - Over Generalization

> TL;DR: Don't make generalizations beyond real knowledge.

# Problems

- Overgeneralization

- [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) violation

# Solutions

1. Think before making structural generalizations

# Context

Refactoring is not just looking at structural code. 

We need to refactor behavior and check if it needs an abstraction.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/2b7734f1f6c0ab46a88a71b37464ceeb)
```rust
fn validate_size(value: i32) {
 	    validate_integer(value);
}
 	
fn validate_years(value: i32) {
 	    validate_integer(value);
}

fn validate_integer(value: i32) {
 	    validate_type(value, :integer);
 	    validate_min_integer(value, 0);
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/1eca09d9bb13cea966bcbe2eda95021c)
```rust
fn validate_size(value: i32) {
	    validate_type(value, Type::Integer);
	    validate_min_integer(value, 0);
}
	
fn validate_years(value: i32) {
	    validate_type(value, Type::Integer);
	    validate_min_integer(value, 0);
}
	
// Duplication is accidental, therefore we should not abstract it	
```

# Detection

[X] Manual

This is a semantic smell.

 # Tags

- Duplication

# Conclusion

Software development is a thinking activity. 

We have automated tools to help and assist us. We need to be in charge.

# Relations

%[https://maximilianocontieri.com/code-smell-46-repeated-code]

# More Info

- [DRY—The Evils of Duplication](https://en.wikipedia.org/wiki/The_Pragmatic_Programmer)

# 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 [Matthew Henry](https://unsplash.com/@matthewhenry) on [Unsplash](https://unsplash.com/s/photos/duplicate)  
  
* * *

> Duplication is far cheaper than the wrong abstraction.

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