Code Smell 182 - Over Generalization
Maxi Contieri
Posted on November 27, 2022
We are refactoring fans. Sometimes we need to stop and think.
TL;DR: Don't make generalizations beyond real knowledge.
Problems
Overgeneralization
Bijection violation
Solutions
- 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
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
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
More Info
Disclaimer
Code Smells are just my opinion.
Credits
Photo by Matthew Henry on Unsplash
Duplication is far cheaper than the wrong abstraction.
Sandi Metz
Software Engineering Great Quotes
Maxi Contieri ใป Dec 28 '20
#codenewbie
#programming
#quotes
#software
This article is part of the CodeSmell Series.
๐ ๐ช ๐
๐ฉ
Maxi Contieri
Posted on November 27, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.