On Thursday, 17 November 2022 at 04:39:35 UTC, thebluepandabear wrote:
I am creating a TUI library and I have a class with the following constant fields:

```
class Label : Renderable {
    const string text;
    const TextAlignment textAlignment;
    const Color color;

this(Dimensions dimensions, string text, TextAlignment textAlignment, Color color) {
        this.dimensions = dimensions;
        this(text, textAlignment, color);
    }

this(string text, TextAlignment textAlignment, Color color) {
        this.text = text;
        this.textAlignment = textAlignment;
        this.color = color;
    }

    override Cell[] render() const {
        Cell[] cells;

        for (int x = 0; x < 0 + text.length; ++x) {
            cells ~= Cell(Coordinates(x, 0), text[x], color);
        }

        return cells;
    }
}
```

I am debating whether or not I should add getter methods to these properties. On one hand, it will inflate the codebase by a lot, on the other hand -- in other languages like Java it is a good practice:

```
class Label : Renderable {
    private const string text;
    private const TextAlignment textAlignment;
    private const Color color;

this(Dimensions dimensions, string text, TextAlignment textAlignment, Color color) {
        this.dimensions = dimensions;
        this(text, textAlignment, color);
    }

this(string text, TextAlignment textAlignment, Color color) {
        this.text = text;
        this.textAlignment = textAlignment;
        this.color = color;
    }

    string getText() const {
        return text;
    }

    TextAlignment getTextAlignment() const {
        return textAlignment;
    }

    Color getColor() const {
        return color;
    }

    override Cell[] render() const {
        Cell[] cells;

        for (int x = 0; x < 0 + text.length; ++x) {
            cells ~= Cell(Coordinates(x, 0), text[x], color);
        }

        return cells;
    }
}
```

It's not a lot of code that has been added but if you have a class with say 10 different fields, adding getter methods would definitely increase the code size by a lot, so what are you guys thoughts on this?

The Java code you presented requires getter, since your member variables are private (which is the default in java anyway). In D, public is the default.

First decided whether you really do want them public (because those members now form a part of the interface that you present to the user of that class, which immediately constrains you in terms of changes).

Decide whether they are in fact amenable to direct public access (i.e. do you need to test for null, or have some other rule that you must meet before providing a value back to the caller?

Better to just make them private, and then provide getters. Then if you do need to implement a check for null or whatever, then you can do that with changing the interface.

Controlled access to data (i.e. data protection) should always be the default, unless you are absolutely sure you don't need (and will never need) that controlled access.

Reply via email to