You join independent information
TL;DR: Don't mix ortoghonal behavior
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.
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]);
}
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
This is a semantic smell, but you can find suspicious concatenation actions on peer reviews.
AI Assistants don't usually suggest this kind of premature optimization of bad rules.
Don't mix unrelated things since breaking things is always harder than having them separated.
Code Smell 20 - Premature Optimization
Code Smells are my opinion.
Photo by Tomas Sobek on Unsplash
Design is choosing how you will fail.
Ron Fein
Software Engineering Great Quotes
This article is part of the CodeSmell Series.