views:

33

answers:

2

I see a lot of legacy .Net 1.1-style code at work like in example below, which I would like to shrink with the help of an auto-property. This will help many classes shrink by 30-40%, which I think would be good.

public int MyIntThingy
{
    get
    {
        return _myIntThingy;
    }

    set
    {
        _myIntThingy = value;
    }
} private int _myIntThingy = -1;

This would become:

public int MyIntThingy
{
    get;
    set;
}

And the only question is - where do I set MyIntThingy = -1;?

If I wrote the class from the start, then I would have a better idea, but I did not. An obvious answer would be: put it in the constructor. Trouble is: there are many constructors in this class. Watching the initialization to -1 in the debugger, I see it happen (I believe) before the constructor gets called. It is almost as if I need to use a static constructor as described here: http://www.c-sharpcorner.com/uploadfile/cupadhyay/staticconstructors11092005061428am/staticconstructors.aspx except that my variables are not static. Java's static initializer comes to mind, but again - my variables are not static. http://www.glenmccl.com/tip_003.htm

I want to make stylistic but not functional changes to this class. As crappy as it is, it has been tested and working for a few years now. breaking the functionality would be bad. So ... I am looking for shorter, sweeter, cuter, and yet EQUIVALENT code. Let me know if you have questions.

+1  A: 

If you just need a stylistic, non-breaking change, consider changing the format a little:

public int MyIntThingy
{
    get { return _myIntThingy; }
    set { _myIntThingy = value; }
} 
private int _myIntThingy = -1;    

Isn't that prettier?

And consider using auto-properties for future code only. It sounds too risky to use them on existing code, unless there are no default values.

Robert Harvey
@Robert, we are also trying to make StyleCop happier - e.g. reduce the number of warnings it produces; it as well as our internal rules dictate that a brace must always be (cannot think of an exception) on its own line.
Hamish Grubijan
Then I think you're stuck with what you have. IMO it's not worth the effort, and not worth the risk.
Robert Harvey
+1  A: 

I'm afraid that you have no option.

If you want to use an auto-property with an initial value that differs from the type's default value then you'll need to set the initial value in the constructor(s).

LukeH
@LukeH, would you agree that using an auto-property on fresh code is nice because it forces you to initialize the value in the constructor, thus making the logic easier to follow? If a tool is of no help, I might analyze every constructor in this class to death and make it clean. I am considering putting all this stuff into an `init()` method and calling `init` from the first line of every constructor.
Hamish Grubijan
@Hamish That's what I do when I know I'm going to have more than one constructor. The only problem (and the reason for the "leave it as is" above) is that **any** change you make could break something. Why chance it when "breaking the functionality would be bad," as you said.
Michael Todd