Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a private auto property instead of a simple variable for a programming standard

In a discussion with a peer, it was brought up that we should consider using auto properties for all class level variables... including private ones.

So in addition to a public property like so:

public int MyProperty1 { get; set; }

Our private class-level variables would look like this:

private int MyProperty2 { get; set; }

Instead of:

private int _myProperty2;

I'm on the fence about why someone would want to do this but I can't decide if my reluctance to accept this is because of my own internal brainwashing with how I write my code to the same programming standards and naming conventions I've used for 10 years or because I've never seen this before (for a reason).

I realize it's extra code to type but to be honest, when using auto-properties, I don't think I've ever typed it out due to the 'prop' and 'propg' snippets so it'd be very simple to set up a new snippet to create a private auto property so the extra code doesn't bother me too much since I never have to type it.

Other than aesthetics which may just be my subconscious, are there any issues that could result from using fully private auto properties? Are there any good reasons to do this or not to do it? I've seen a lot of code in my day on stackoverflow, codeplex, codeproject, etc. and I've never seen anyone use this standard.... is there a reason why?

like image 966
Biggert Avatar asked May 25 '11 13:05

Biggert


2 Answers

Private auto-properties are completely pointless, in my opinion. What value does a private auto-property provide that a plain field doesn't?

(It's different when the auto-property is only partially private -- eg, a public/protected getter with a private setter -- or when you use a private non-automatic property to enable you to wrap additional code around the getter/setter.)

like image 64
LukeH Avatar answered Nov 15 '22 23:11

LukeH


This does not make too much sense.

I can think of a 'benefit':

  • you can later add logic to the getter and/or setter and be sure it is always passed

but frankly your classes should not become so big that this is useful.

"are there any issues" ?

Your properties won't work as arguments to ref or out parameters.

like image 43
Henk Holterman Avatar answered Nov 15 '22 22:11

Henk Holterman