0

I'm writing a little library for myself which has a class that contains hexfields with their cube coordinates x,y and z. Now I usually mark my member variables with a leading m, e.g. int mPosition; or mX, mY, mZ, in this specific case. This would lead to the getter being named getmX().

This is pretty straightforward, but is somehow not as readable as getX(). Would you say it is acceptable to use mX in the class, but rename the publically visible getter to getX()? Or is there a reason to stay consistent? There will not be a publically available setter.

Draugr
  • 436
  • 4
  • 11

2 Answers2

9

This would lead to the getter being named getmX().

No, it wouldn't.

  • The entity is "called" x.
  • The member variable you used to represent that entity is mX.
  • The getter function you used to access that entity is getX().

I wouldn't go too far down the path of getters and setters, though; they're a bit of an anti-pattern when only thinly wrapping individual variables like this.

Besides, call it whatever you want.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

The fact that private variables have a leading m is an implementation detail, clients shouldn't rely on it.

Your getters are public and should never change even if you change your implementation. Therefore, it's not a question of being easy to use. You should write getX() for better encapsulation.

KABoissonneault
  • 2,359
  • 18
  • 17