Tuesday, May 22, 2007

Laziness comes back to bite you.

Some time ago I wrote a class that was really just used as a record to carry some data around places. It looked a bit like this:

public class FilterInfo
    /// The name of the filter.
    /// Maximum length of 100 chars.
    public string Name;

    /// The description of the filter.
    /// Maximum length of 1000 chars.
    public string Description;

See how I saved a few keystrokes by declaring the members as fields rather than as properties? Silly me. Well, it turns out that NVelocity does not know how to access the values of fields in templates but it has no trouble at all with properties. Oops.

It took me 15 minutes to figure that out during which time I'm sure I could have declared those members as properties instead. Silly Jeff. Fortunately I think that was my only lapse in this project...

P.S. I do consider NVelocity's behavior here to be a little bit broken but I don't care enough to argue the point or to add support for public fields.


Anonymous said...

In C# 3.0 it will be easier to be lazy and still have properties! The syntax will look something like this:

public string Blah { get; set; }

This will automatically generate a private variable for you. The nice thing is that it will mean doing the right thing is no harder than being lazy!

Unknown said...

Hmm, it's called Code Snippets (i.e. prop).

However, if you're extremely lazy (like me), you can create ones like ps (for public string), pi (for public int) and so on & so forth.


Gunnlaugur Þór Briem said...

“Fortunately I think that was my only lapse in this project”

I trust that the blooper gods will be delivering punishment for that remark any time now. :)

Anonymous said...

So is there any use for public fields?