Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you are using getters and setters, how should you name the private member variables?

As kind of a follow up to this question about prefixes, I agree with most people on the thread that prefixes are bad. But what about if you are using getters and setters? Then you need to differeniate the publicly accessible getter name from the privately stored variable. I normally just use an underscore, but is there a better way?

like image 630
Iain Avatar asked Dec 03 '22 09:12

Iain


1 Answers

This is a completely subjective question. There is no "better" way.

One way is:

private int _x;
public get x():int { return _x; }
public set x(int val):void { _x = val; }

Another is:

private int x;
public get X():int { return x; }
public set X(int val):void { x = val; }

Neither is the right answer. Each has style advantages and disadvantages. Pick the one you like best and apply it consistently.

like image 122
David Arno Avatar answered Dec 08 '22 00:12

David Arno