If you use getters and setters, what should be called private member variables?

As a continuation of this question about prefixes , I agree with most people in the thread that prefixes are bad. But what if you use getters and setters? Then you need to distinguish the public recipient name from the private stored variable. I usually use an underscore, but is there a better way?

+4
source share
6 answers

This is a completely subjective question. There is no β€œbetter” way.

One of the methods:

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; } 

There is also no right answer. Each has advantages and disadvantages of style. Choose the one you like best and apply it sequentially.

+6
source

I like the field prefix with underline, as others have mentioned.

 private int _x; 

I think this goes beyond direct personal preference (as David Arnault said in this thread). I think there are some real objective reasons for this:

  • This means that you do not need to write "this.x = x" for assignments (especially in setters and constructors).
  • It distinguishes your fields from your local variables / arguments. This is important to do: fields are more difficult to process than local ones, because their scale is wider / longer. Adding an extra character is a bit of a warning sign for coders.
  • In some IDEs, underlining will force autocomplete to sort the fields at the top of the offer list. This makes it easy to see all the fields for a class in a single block. This, in turn, may be useful; on large classes, you will not be able to see the fields (usually defined at the top of the class) on the same screen as the code you are working on. Sorting them to the beginning gives a convenient link.

(These conventions are for Java, but similar ones exist for other languages)

These things seem small, but their prevalence definitely makes my life easier when I code.

+6
source

In java there is this.foo in python there is self.foo, and other languages ​​have similar things, so I do not see the need for something to be named in a special way, when I can already use the language construct. In the same context, good IDEs and editors understand member variables and highlight them in a special way, so you can see them without using special names.

+3
source

In a case-sensitive language, I just use:

 private int myValue; public int MyValue { get { return myValue; } } 

Otherwise, I would use an underscore

 Private _myValue As Integer Public ReadOnly Property MyValue As Integer Get Return _myValue End Get End Property 
+1
source

There are as many different ways to do this as programmers do, but some of the most popular methods include (for the Foo property):

  • mFoo
  • m_foo
  • _foo
  • Foo
+1
source

I like to write "this.x = x". This is very clear to me. Also, when using Eclipse, you can automatically generate your getters / setters this way.

+1
source

Source: https://habr.com/ru/post/1277460/


All Articles