Deep Simplicity - Rule 7

In this entry I am going to talk about rule #7: Don't Use Any Classes with More Than Two Instance Variables. This for me is one of the hardest rules to try to follow. The author of Deep Simplicity states that he is about to release a one hundred thousand line application that strictly follows these rules, so I guess I just suck.

The reason that we can break any object down to have one or two instance variables is because when you have more than two of them, you can usually group two into one object. Adhering to this rule makes for logical code that has a very defined responsibility. This after all is the whole point to these rules; making it so that everything is so simple it requires little to no thought to work on the project. Let's look at some code:

class User
{
    protected $firstName;
    protected $lastName;
    protected $address;
    protected $phoneNumber;

    public function __toString()
    {
        return $this->firstName . ' ' . $this->lastName;
    }
}

So we have all seen the classic User object, you might be wondering how we can get this object to have one or two instance variables. You can do it by either working your way down by splitting an object into related halves or working your way up by taking any to instance variables and making an object out of them. So let's try it:

class User
{
    protected $name;
    protected $contactInformation;

    public function __construct()
    {
        $this->name = new Name()
        $this->contactInformation = new ContactInformation();
    }

    public function __toString()
    {
        return $this->name->asFullName();
    }
}

class Name
{
    protected $first;
    protected $last;

    public function asFullName()
    {
        return $this->first . ' ' . $this->last;
    }
}

class ContactInformation
{
    protected $address;
    protected $phoneNumber;
}

So there we have it. The User object was broken down into finer grained objects that can then house the logic specific to those properties. It's pretty sweet when it works out that way, but in the interest of full disclosure I'll admit that I have no idea how to break up the original User class I was going to use for this example:

class User
{
    protected $firstName;
    protected $lastName;
    protected $address;
    protected $city;
    protected $state;
    protected $zip;
    protected $phoneNumber;
}

To me, that is more like the User objects I see all of the time. I can still break it up into Name and ContactInformation. ContactInformation can be broken down into Address and PhoneNumbers, but I can't think of a way to break up this object:

class Address
{
    protected $address;
    protected $city;
    protected $state;
    protected $zip;
}

To me, every property in there belongs to an address. Further, I can't think of a way to group any two of these properties into a new object or a way to split this object in half.

Next time is rule #8: Use First Class Collections.

blogroll

social