Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
TDD Design Starter Kit – Responsibilities, Cohesion, and Coupling
Recently, I started a new position with a company transitioning to TDD. One of the things I'm trying to accomplish in my new job is to help my colleagues become effective in TDD without most of the growing pains I experienced. So in an effort to help my new organization, I'm using this blog to communicate all the things I wish someone had shown me before I started writing code with Test Driven Development. So, in a couple of parts, here is all the off-the-cuff advice I can think to give to a TDD newbie to avoid seeing NUnit red bars in their sleep and getting the “Shame Card” permanently affixed to their monitor (for repeatedly breaking the build).
Separation of Concerns

Separation of Concerns is one of the most important concepts in programming, and the single most effective strategy for writing classes and methods that are easy to test. Simply put, divide your system into modules or layers, each with a distinct area of responsibility. We’ve known that layering is a good idea for a long time, but nothing exposes an intermingled ball of mud than trying to write automated tests. After you do TDD for awhile, you will probably notice that you slice classes thinner than before.

At the heart of every programming paradigm and design technique is a “Divide and Conquer” strategy to break a complicated whole into easily digestible pieces. Using OO programming, we are mostly concerned with defining the different responsibilities of the code, and assigning the responsibilities to the logical classes and methods.

There are a couple of qualities (more info here) to guide the division of labor in your class structure:
Cohesion – A measure of whether a class has a well defined, meaningful responsibility. High cohesion is desirable. If a class contains unrelated functions or responsibilities, it is not cohesive.
Coupling – More or less, how entangled or dependent a class is with other classes. A loosely coupled design implies that classes or subsystems can largely be modified independently of one another. The Pragmatic Programmers call this quality “orthogonality.”

TDD is all about rapidly making small pieces of code work, then molding the pieces into the aggregate. Cohesion is important to us, because we want to focus on making a single concern work at a time. Coupling is important because we want to make a concern work without interference from other concerns polluting our tests. Data access, business logic, user interface display, messaging, and logging are examples of separate concerns or responsibilities that should be built and unit tested separately. Divide and conquer.

Here’s a sample of code that flunks both cohesion and coupling tests.


public void PublishBigThings()
{
// Go get some configurion
int threshold = int.Parse(ConfigurationSettings.AppSettings["threshold"]);
string connectionString = ConfigurationSettings.AppSettings["connectionString"];

string sql = select * from things where ready_to_publish = true and size > ";
sql += threshold;

SqlConnection connection = new SqlConnection(connectionString);

connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
string name = reader["Name"].ToString();
string destination = reader["destination"].ToString();

if (this.IsDestinationApproved(destination))
{
Publish(name, destination);
}
}
}


The method is not cohesive because it is responsible for doing everything – pulling flags out of configuration, fetching data from a database, and making business logic decisions. The method is tightly coupled because it cannot function correctly without the proper configuration in place and a Sql Server database (other database engines need not apply).

“One Class, One Responsibility” is a good rule of thumb to follow in class construction. Don’t hesitate to create more classes. It can feel like more complexity, but it can also isolate complexities. See Fear of Adding Classes. It’s also much easier to make mistakes in big methods than in smaller methods.

More to come. Someday.

Posted Wed, Jul 20 2005 4:59 PM by Jeremy D. Miller
Filed under:

[Advertisement]

Comments

Jeremy D. Miller -- The Shade Tree Developer wrote TDD Design Starter Kit
on Thu, Jul 21 2005 1:05 PM
Here's a handful of articles on designing with or for TDD I had originally posted on my...
Jeremy D. Miller -- The Shade Tree Developer wrote My Strawman Can Beat Up Your Strawman
on Tue, Oct 18 2005 3:47 PM
There's a good discussion going on right now on the Yahoo XP board about Jim Shore's post on design. ...
Jeremy D. Miller -- The Shade Tree Developer wrote Test Driven Development with ASP.Net and the Model View Presenter Pattern
on Wed, Feb 1 2006 11:17 PM
A friend of mine was asking me a while back about ways to apply the Model View Presenter (the “Humble...
Jeremy D. Miller -- The Shade Tree Developer wrote Test Driven Development with ASP.Net and the Model View Presenter Pattern
on Thu, Feb 2 2006 7:11 PM
Author: <a href="blogs/jeremy.miller/">Jeremy D. Miller</a><br />A friend of mine was asking me a while back about ways to apply the Model View Presenter (the “Humble Dialog Box”) pattern to ASP.Net development to promote easier unit testing of the user interface layer. Two weeks and a major case of writer’s block later, I finally finished the post. I wrote a blog post last spring describing the usage of MVP (“The Humble Dialog Box”) with WinForms clients, but web development in general and ASP.Net in specific comes with a different set of challenges.
on Thu, Jun 1 2006 7:40 AM
Just a quick follow up to my post on "Test Small Before Testing Big."
It's relatively easy to sell the...
Jeremy D. Miller -- The Shade Tree Developer wrote Best of the Shade Tree Developer (with actual links!)
on Mon, Aug 7 2006 4:51 PM
Between being extremely short handed at work, tech' reviewing a new book, a
possible book proposal...
on Mon, Jan 8 2007 11:59 PM

My employer, Finetix , added my blog to their main feed and my most recent post at the time just happened

LOLO wrote re: TDD Design Starter Kit – Responsibilities, Cohesion, and Coupling
on Mon, Apr 9 2007 5:45 AM

BASICS

Jeremy D. Miller -- The Shade Tree Developer wrote Some Concepts to Know First
on Sun, Jun 29 2008 4:40 PM

  JEREMY’s NOTE:  I’m trying to rewrite the StructureMap documentation today, and I’m going

Community Blogs wrote Before you use an IoC tool, some concepts to know first
on Sun, Jun 29 2008 5:03 PM

JEREMY’s NOTE: I’m trying to rewrite the StructureMap documentation today, and I’m going to blog most

Add a Comment

(required)  
(optional)
(required)  
Remember Me?