You join independent information
TL;DR: Don't mix ortoghonal behavior
Problems
Maintainability
Error Prone
Performance Penalties
The principle of least astonishment principle violation
Bijection Violation
Duplication of Logic on breaking the attributes
Solutions
- Break Orthogonal behavior and properties
Context
Parsing data is always a problem, where joining elements is much easier than breaking them.
If you use a separator to break the attributes, you need to make sure the separator does not belong to the domain, and you should escape it.
If you map your data to relational databases, search queries will be more difficult and less performant for concatenated attributes
Sample Code
Wrong
class Point {
constructor(coordString) {
this.coordString = coordString;
}
x() {
const coords = this.coordString.split(',');
if (coords.length !== 2) {
throw new Error('Invalid coordinate string format');
}
return parseFloat(coords[0]);
}
y() {
const coords = this.coordString.split(',');
if (coords.length !== 2) {
throw new Error('Invalid coordinate string format');
}
return parseFloat(coords[1]);
}
}
Right
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
Detection
[X] Semi-Automatic
This is a semantic smell, but you can find suspicious concatenation actions on peer reviews.
Tags
- Coupling
Level
[X] Beginner
AI Assistants
AI Assistants don't usually suggest this kind of premature optimization of bad rules
Conclusion
Don't mix unrelated things since breaking things is always harder than having them separated.
Relations
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--8ut8CKTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F366059%252F69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 20 - Premature Optimization
Maxi Contieri ・ Nov 8 '20
Disclaimer
Code Smells are my opinion.
Credits
Photo by Tomas Sobek on Unsplash
Design is choosing how you will fail.
Ron Fein
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--8ut8CKTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F366059%252F69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (0)