Fri, Aug 24 2007 8:53 AM Marc Jacobi

Why do we not design a class like this?

I started learning WPF some time ago and the first thing that struck me was the sheer number of properties available on these classes. It turns out that those properties are mainly there to make support for Xaml (declaritive programming) easier/better. But even before WPF, WinForms too had its share of properties on its classes.

So I wondered why would we want to have so many Font-properties on a class in WPF; at least in WinForms its on a Font object and the Form/Control only has a Font property (the answer has probably to do with Xaml -again).

To be more generic: why do we not (in general) design our class properties to be grouped in functional related groups and make these groups class properties themselves?

So instead of (public fields used for clarity):

public class MyClass
{
    public int MinWidth;
    public int MinHeight;
    public int MaxWidth;
    public int MaxHeight;

    // ...more properties
}

Why don't we make code like this:

public class MyClass
{
  public Dimension Dimension = new Dimension();

  public struct Dimension
  {
    public int MinWidth;
    public int MinHeight;
    public int MaxWidth;
    public int MaxHeight;
  }

  // ...more property groups
}

For classes with a lot of properties this would make things a little more navigatable for the user of that class (i would think). When there are several pages of intellisense worth of properties (in the first example) you would be able to reduce that to only a handfull of groups. Note that I propose a property group as a struct: its merely a container for properties and has no functionality of its own.

One of the downsides of this approach could be that accessing the main class from a property implementation in a property group requires passing that reference to all property groups. On the otherhand: most property implementations are pretty simple anyway...

Thoughts?

# re: Why do we not design a class like this?

Friday, August 24, 2007 11:26 AM by michaud

What would be the benifits/drawbacks when inheriting from such a class?

# re: Why do we not design a class like this?

Friday, August 24, 2007 11:40 AM by Marc Jacobi

Good point! If you have virtual properties this would probably not be a good idea. If the properties are just plain vanilla properties there would be no problem (I can think of at this moment).

# re: Why do we not design a class like this?

Thursday, August 30, 2007 8:08 AM by dotgrid

It is a very good point. and might put some sense into building large properties stuffed classes.

But taking it too much to the extreme would make my code page size a lot more wider.

# re: Why do we not design a class like this?

Thursday, August 30, 2007 9:47 AM by Marc Jacobi

Taking thing to the extreme is never a good thing (unless its money? ;-).

I would think that you would only need one level in most cases (as my example). Multiple levels of property containers reeks of bad design and might justify actual classes instead of just property structs.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 3 and 5 and type the answer here: