Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to forgo getters and setters for simple classes?

Tags:

I'm making a very simple class to represent positions in 3D space.

Currently, I'm just letting the user access and modify the individual X, Y and Z values directly. In other words, they're public member variables.

template <typename NumericType = double>
struct Position
{
    NumericType X, Y, Z;

    // Constructors, operators and stuff...
};

The reasoning behind this is that, because NumericType is a template parameter, I can't rely on there being a decent way to check values for sanity. (How do I know the user won't want a position to be represented with negative values?) Therefore, there's no point in adding getters or setters to complicate the interface, and direct access should be favored for its brevity.

Pos.X = Pos.Y + Pos.Z; // Versus...
Pos.SetX(Pos.GetY() + Pos.GetZ());

Is this an okay exception to good practice? Will a (hypothetical) future maintainer of my code hunt me down and punch me in the face?

like image 298
Maxpm Avatar asked Jun 29 '11 23:06

Maxpm


2 Answers

The idea behind using getters and setters is to be able to perform other behavior than just setting a value. This practice is recommended because there are a multitude of things you might want to retrofit into your class.

Common reasons to use a setter (there are probably more):

  • Validation: not all values allowed by the type of the variable are valid for the member: validation is required before assignment.
  • Invariants: dependent fields might need to be adjusted (e.g. re-sizing an array might require re-allocation, not just storing the new size).
  • Hooks: there is extra work to perform before/after assignment, such as triggering notifications (e.g. observers/listeners are registered on the value).
  • Representation: the field is not stored in the format "published" as getters and setters. The field might not even stored in the object itself; the value might be forwarded to some other internal member or stored in separate components.

If you think your code will never, ever use or require any of the above, then writing getters and setters by principle is definitely not good practice. It just results in code bloat.

Edit: contrarily to popular belief, using getters and setters is unlikely to help you in changing the internal representation of the class unless these changes are minor. The presence of setters for individual members, in particular, makes this change very difficult.

like image 80
André Caron Avatar answered Oct 04 '22 19:10

André Caron


Getters and setters are really only an important design choice if they get/set an abstract value that you may have implemented in any number of ways. But if your class is so straight-forward and the data members so fundamental that you need to expose them directly, then just make them public! You get a nice, cheap aggregate type without any frills and it's completely self-documenting.

If you really do want to make a data member private but still give full access to it, just make a single accessor function overloaded once as T & access() and once as const T & access() const.


Edit: In a recent project I simply used tuples for coordinates, with global accessor functions. Perhaps this could be useful:

template <typename T>
inline T cX(const std::tuple<T,T,T> & t) { return std::get<0>(t); }

typedef std::tuple<double, double, double> coords;
//template <typename T> using coords = std::tuple<T,T,T>; // if I had GCC 4.8

coords c{1.2, -3.4, 5.6};

// Now we can access cX(c), cY(c), cZ(c).
like image 40
Kerrek SB Avatar answered Oct 04 '22 19:10

Kerrek SB