# Code Smell 243 -  Concatenated Properties

*You join independent information*

> TL;DR: Don't mix ortoghonal behavior

# Problems

- Maintainability

- Error Prone

- Performance Penalties

- [Premature optimization](https://maximilianocontieri.com/code-smell-20-premature-optimization)

- [The principle of least astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment) principle violation

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

- Duplication of Logic on breaking the attributes

- [Coupling](https://maximilianocontieri.com/coupling-the-one-and-only-software-design-problem)

# Solutions

1. 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

[Gist Url]: # (https://gist.github.com/mcsee/ed66e002ac7cf031d7256b7529a1624d)
```javascript
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

[Gist Url]: # (https://gist.github.com/mcsee/7a5cb375b631c683845d61095b0d9ded)
```javascript
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

%[https://maximilianocontieri.com/code-smell-20-premature-optimization]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Tomas Sobek](https://unsplash.com/@tomas_nz) on [Unsplash](https://unsplash.com/photos/photo-of-red-and-blue-zippers-nVqNmnAWz3A)
    
* * *

>Design is choosing how you will fail.

_Ron Fein_

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