Feature Envy, Class Cohesion and Driver Modules


Mech Retriever Dev Blog

This post could also have been called “why lots of little control variables at the top of your Driver class is a design smell,” but that would’ve been too long.

In Object-Oriented programming, Class Cohesion is the idea that most of your class’s methods should probably use most of its variables.  It’s not a strong rule, but it’s a useful hint–if subsets of its variables are only used by subsets of methods, you may actually have what should be multiple classes tucked into one.

Feature Envy, on the other hand, is the idea that if a method is mostly using data from another class, then that method should probably live on that class, and not whatever class it lives on.

Given this, if you have a lot of little control variables at the top of your class–flags and whatnot–then you should probably break it up, which is what I’ve been doing.  The trick is figuring out the seam.  Sometimes a logical process uses some variables from one class and some from another.

At times like that, sometimes the best solution is to pass a function from the class with less variables used to the class with more variables used.  This enables classes to remain in charge of their own state but still provide the functionality you need.


Like, Share, Subscribe, or Tweet us!

Check us out on Youtube or Deviantart!

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