DEV Community

Cover image for Code Smell 103 - Double Encapsulation
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2

Code Smell 103 - Double Encapsulation

Calling our own accessor methods might seem a good encapsulation idea. But it is not.

TL;DR: Don't use setters and getters, even for private use

Problems

  • Setters

  • Getters

  • Exposing private attributes

Solutions

  1. Remove setters

  2. Remove getters

  3. Protect your attributes

Context

Using double encapsulation was a standard procedure in the 90s.

We wanted to hide implementation details even for private use.

This was hiding another smell when too many functions relies on data structure and accidental implementation.

For example, we can change an object internal representation and rely on its external protocol.

Cost/benefit is not worth it.

Sample Code

Wrong

contract MessageContract {
    string message = "Let's trade";

    function getMessage() public constant returns(string) {
        return message;
    }

    function setMessage(string newMessage) public {
        message = newMessage;
    }

    function sendMessage() public constant {
        this.send(this.getMessage());
        //We can access property but make a self call instead
    }
}
Enter fullscreen mode Exit fullscreen mode

Right

contract MessageContract {
    string message = "Let's trade";

    function sendMessage() public constant {
        this.send(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semiautomatic

We can infer getters and setters and check if they are invoked from the same object.

Tags

  • Encapsulation

Conclusion

Double encapsulation was a trendy idea to protect accidental implementation, but it exposed more than protected.

Relations

More Info

Credits

Photo by Ray Hennessy on Unsplash


Encapsulate the concept that varies.

Erich Gamma


This article is part of the CodeSmell Series.

Enterprise-level in-app dashboards. Startup-level speed.

Enterprise-level in-app dashboards. Startup-level speed.

Ship pixel-perfect dashboards that feel native to your app with Embeddable. It's fast, flexible, and built for devs.

Get early access

Top comments (0)

Developer-first embedded dashboards

Developer-first embedded dashboards

Embed in minutes, load in milliseconds, extend infinitely. Import any chart, connect to any database, embed anywhere. Scale elegantly, monitor effortlessly, CI/CD & version control.

Get early access

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay