views:

41

answers:

1

In almost all of my classes, I have a mixture of properties and internal class variables. I have always chosen one or the other by the rule "property if you need it externally, class variable if not". But there are many other issues which make me rethink this often, e.g.:

  • at some point I want to use an internal variable from outside the class, so I have to refactor it into a property which makes me wonder why I don't just make all my internal variables properties in case I have to access them externally anyway, since most classes are internal classes anyway it aren't exposed on an API so it doesn't really matter if the internal variables are accessible from outside the class or not

  • but then since C# doesn't allow you to instantiate e.g. List<string> property in the definition, then these properties have to be initialized in every possible constructor, so these variables I would rather have internal variables just to keep things cleaner in that they are all initialized in one place

  • C# code reads more cleanly if constructor/method parameters are camel case and you assign them to pascal case properties instead of the ambiguity of seeing templateIdCode and having to look around to see if it is a local variable, method parameter or internal class variable, e.g. it is easier when you see TemplateIdCode = templateIdCode that this is a parameter being assigned to a class property. This would be an argument for always using only properties on internal classes.

e.g.:

public class TextFile
{
    private string templateIdCode;
    private string absoluteTemplatePathAndFileName;
    private string absoluteOutputDirectory;
    private List<string> listItems = new List<string>();

    public string Content { get; set; }
    public List<string> ReportItems { get; set; }

    public TextFile(string templateIdCode)
    {
        this.templateIdCode = templateIdCode;
        ReportItems = new List<string>();
        Initialize();
    }
    ...

When creating internal (non-API) classes, what are your strategies in deciding if you should create an internal class variable or a property?

+1  A: 

If I have a private variable that I find needs public access at a later point, I just create a property that uses it as it's private member, ex:

private List<string> listItems = new List<string>();

Public List<string> ListItems
{
     get{return listItems;}
     set{listItems = value;}
}

This allows you to create public access to the data, without having to refactor any code. It also allows you to initialize the data in the private member, and not have to do it in the constructor.
One more advantage is that any modifications to the data that you want to perform for anyone accessing the public property, can be done in the property's getter. Even though VS2008 introduced Automatic Properties as a feature, I still prefer the VS2005 style of properties.

derek
You can have autoproperties in VS2005 as long as you build with the correct version of the C# compiler
Rune FS