The Complexity of Types

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

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

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.

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: Logo

You are commenting using your 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