One way to simplify code – “IsAppropriateFor”

cabin2

I personally like to avoid unnecessary complexity. I love solving problems with simple, easy to read, code. Dr. Chris Dansie at the University of Utah’s department of Information Science likes to say that, “…when it comes to real-world software application design and development: ‘Simplicity breeds security, stability and scalability”. Thus, I present to you one pattern I use that especially increases maintainability. When writing object classes, developers most often separate the logic that determines whether a class is appropriate or not into a separate class. Here, I will use the example of buildings and assigning groups to those buildings. This post is inspired by the fact that I stayed at a wonderful lodge in Colorado this last spring. Thus, if you have a class called Lodge and a class called Cabin, one might see a client class called GroupAssigner with the following pseudo code:

Class GroupAssigner

if Group.groupSize > 12

Instantiate Lodge

else

Instantiate Cabin

If there is a growing number of properties that determine what type of building to assign, then the if/else statements can grow overly complex and messy. It will be difficult to maintain this code whenever the logic needs to be updated. I propose adding a static IsAppropriateFor method to each building class which includes all the scenarios that make that building appropriate. Because the logic actually describes the building, it is more suitable for the logic to live in the class that it describes.

Class Lodge

public static IsAppropriateFor(Group)

if Group.groupSize > 12

return true

 

Class Cabin

public static IsAppropriateFor(Group)

if Group.groupSize <= 12

return true

Then, the GroupAssigner class can simply have the following (which happens to be easier to read and thus is more maintainable):

Class GroupAssigner

if Lodge.IsAppropriateFor(Group)

Instantiate Lodge

if Cabin.IsAppropriateFor(Group)

Instantiate Cabin

The writer of new building classes is the one who knows best when such buildings are appropriate whereas the writer of the GroupAsssigner class may be long gone. Maintaining the logic becomes easier because a developer looking to make updates can start by examining each building’s appropriateness. Also, notice that IsAppropriateFor is a static method. This allows the logic to live in its respective class and it allows the logic to be called upon without first instantiating the class. Imagine the cost savings obtained in the case where the appropriateness logic is simple but instantiation is expensive! This strategy is especially helpful when instantiating a whole series of classes. I love using IsAppropriateFor when the classes represent optional steps in a long process. Limitations: This strategy is best suited for logic where the appropriateness of one class does not affect the appropriateness of another class. However, in the case where one class’s appropriateness affects another class’s appropriateness, a developer can use a combination of IsAppropriateFor and additional (now simpler) logic in the client class. End with a fun example:

Class CozyCabinInTheUintas

public static IsAppropriateFor(Group)

if Group.IsMyFamily

return true

Class HugeLodgeInParkCity

public static IsAppropriateFor(Group)

if Group.IsAllThoseReadingThisPost

return true

 

Class DareDevilEventCenter

public static IsAppropriateFor(Group)

if Group.IsCoupleWantingToRideAFlyingBedOver12Trucks

return true

 

Class GroupAssigner

Public ScheduleAndReserveEvents

if Cabin.IsAppropriateFor(Group)

Instantiate Cabin(Date of Summer Vacation)

if Lodge.IsAppropriateFor(Group)

Instantiate Lodge(Date of Coding Convention)

if DareDevilEventCenter.IsAppropriateFor(Group)

Instantiate DareDevilEventCenter(Date of Coworker’s Surprise Marriage Proposal)

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>