DEV Community

Cover image for Code Smell 243 - Concatenated Properties
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2

Code Smell 243 - Concatenated Properties

You join independent information

TL;DR: Don't mix ortoghonal behavior

Problems

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

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]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Right

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Disclaimer

Code Smells are my opinion.

Credits

Photo by Tomas Sobek on Unsplash


Design is choosing how you will fail.

Ron Fein


This article is part of the CodeSmell Series.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay