The Complexity of Types

Question
What should you do when you have a type that exposes many methods and/or properties?

Answer
Consider splitting the type into one main class and one or more dependent classes.

Comments
Why should you split a type with many properties or methods with similar names? Because the code in smaller types can be reused more easily.

Example
Let’s say you have a Contact class with many similar properties such as HomeAddress, HomeCity, HomeZip, and HomeState. Such a type can be better modeled with two different types, Contact and Location, where Contact exposes one property of type Location. The benefit of having a distinct Location type becomes evident if you later decide to implement other similar properties, such as Work.

public class Contact
{
    public Location Home
    {
        ...
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s