Question

The coding standard at work wants even simple accessors to be written like a method.

i.e.

string x 
{
  get 
    { 
      return _x;
    }
}

While to me it makes sense to write it as

string x { get { return _x; } }

Because once you have 10 accessors, you'll be scrolling through pages of code wondering how it all fits together, as opposed to nicely fitted.

Which one makes more sense?

Was it helpful?

Solution

To me, it makes sense to write it like this:

string x 
{
    get { return _x; }
    set { _x = value; }
}

But where I worked 2 years ago, I was asked to write it like this:

string x {
    get { return _x; }
    set { _x = value; }
}

And I simply got used to it and it was natural to me in 3 months.

To me, this is like declaring 2 different things on one line, but mostly, I didn't used to it, so I don't prefer it:

public string X { get { return _x; } }

at the same time, autoproperties work fine, to me:

public string X { get; set; }

It is all up to your group. You have to decide and go forward with chosen standard. There is no good or bad. Microsoft may recommend something but this is mostly so that all developers can rather easily understand the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top