Wednesday, March 30, 2005

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.

46 Comments:

Anonymous Jeffrey Palermo said...

Great points. I like the "one class, one responsibility".

8:05 PM, April 01, 2005  
Anonymous low cost mlm said...

Congradulations on a very well written, interesting and surperb blog!

Have you heard about this shocking news?

1000's of Real People in 180+ Countries are Earning
Quiet Fortunes From Home Using the Most Ingenious
Automated Marketing System Ever Invented.

Are you curious to find out what this is all about?

Why not? You Have Nothing to Lose.

You can sign up for this system absolutely free of
charge and check it all out for yourself.

To start off we have a short video presentation we
would like you to watch. Just turn up your speakers
and click on the link below.

Have fun and open your mind!

It is all right here: prospects and trains It successfully covers prospects and trains related stuff.

Come and check it out now, Scott :-)

3:14 AM, November 19, 2005  
Anonymous low cost mlm said...

Congradulations on a very well written, interesting and surperb blog!

Have you heard about this shocking news?

1000's of Real People in 180+ Countries are Earning
Quiet Fortunes From Home Using the Most Ingenious
Automated Marketing System Ever Invented.

Are you curious to find out what this is all about?

Why not? You Have Nothing to Lose.

You can sign up for this system absolutely free of
charge and check it all out for yourself.

To start off we have a short video presentation we
would like you to watch. Just turn up your speakers
and click on the link below.

Have fun and open your mind!

It is all right here: multi-level marketing It successfully covers multi-level marketing related stuff.

Come and check it out now, Scott :-)

7:58 AM, November 19, 2005  
Anonymous high profit mlm said...

Hey, you have a great blog here! You really are very talented and deserve an honest compliment, congradulations! I'm definitely going to bookmark you!

I have a sponsors partners in Multi-Level Marketing site/blog. It pretty much covers sponsors partners in Multi-Level Marketing related stuff.

Come and check it out if you get time, Scott :-)

3:36 PM, November 19, 2005  
Anonymous high profit mlm said...

Congradulations on a very well written, interesting and surperb blog!

Have you heard about this shocking news?

1000's of Real People in 180+ Countries are Earning
Quiet Fortunes From Home Using the Most Ingenious
Automated Marketing System Ever Invented.

Are you curious to find out what this is all about?

Why not? You Have Nothing to Lose.

You can sign up for this system absolutely free of
charge and check it all out for yourself.

To start off we have a short video presentation we
would like you to watch. Just turn up your speakers
and click on the link below.

Have fun and open your mind!

It is all right here: 0 start-ups It successfully covers 0 start-ups related stuff.

Come and check it out now, Scott :-)

9:43 PM, November 19, 2005  
Blogger Ebook Manic said...

Hi Jeremy D. Miller,

I was busy looking for information on mydd and I came across your post " TDD Design Starter Kit – Responsibilities, Cohesion, and Coupling ". Very interesting. If possible, have you got any ideas of where to find the best resources on mydd.

Regards

4:53 AM, January 12, 2006  
Blogger Joe Langer said...

Jeremy D. Miller,
Great post. I found your post a good read. You may be interested in automated download software.

Any recommendations for the best infromation about automated download software? Nice one.

7:19 AM, January 16, 2006  
Blogger Joe Langer said...

Hello Jeremy D. Miller,

I am based in the UK and I stumbled across your post " TDD Design Starter Kit – Responsibilities, Cohesion, and Coupling ".
I'm quite keen on finding any useful information on automated ebooks. I found some good stuuf on automated ebooks but I wondered whether you may have any advice on the best resources out there.

Thanks in advance.

7:03 AM, January 18, 2006  
Blogger Matthew Humphries said...

Hi Jeremy D. Miller,

I see you share my interest in digital delivery. Your input would mean a lot to me. If you would be so kind to visit my site and give me your feedback, I would most appreciate it.

Thanks

7:52 AM, January 24, 2006  
Blogger Scott A. Edwards said...

Congradulations on your blog! NOW, check this out, You can join a program that starts at only $7 then the costs go down. Down, Down, Down. It only takes 5 minutes to set up your international account!

To find out more visit: low cost mlm site. It successfully exposes FREE information and covers low cost mlm related stuff.

12:45 AM, February 02, 2006  
Blogger Scott A. Edwards said...

Hello I am the traffic man, zip, zip, zip. make a new resolution to get a flood of traffic to your website this year. Let me show you how to get FREE traffic to your site. Yes I said FREE, FREE, FREE!!! Don't delay.

To find out more, visit my low cost mlm site. It successfully covers FREE information exposing FREE traffic and low cost mlm related stuff. Don't forget - FREE, FREE, FREE. You have nothing to lose!

12:50 AM, February 02, 2006  
Blogger Scott A. Edwards said...

Wow what a cool blog you have here! I am impressed. You really put a lot of time and effort into this. I wish I had your creative writing skills, progressive talent and self- discipline to produce a blog like you did. Your blog really does deserve an honest compliment. If you have some time, stop by my site. It deals with stuff like, click here: high profit and then feel free to e-mail me with your words of wisdom.

P.S. I'll sure put the word out about your site and I would appreciate any business you may send my. way... Later, Scott.

5:11 AM, March 22, 2006  
Anonymous Scott Arthur Edwards said...

Dear friend, here is an advertising portal you can join for FREE and you also get 100 FREE advertising credits just for signing up...
Click here: FREE PICTURE WITHOUT A CAMERA

10:19 AM, July 12, 2006  
Blogger Robert said...

We all need to realize it's about time to put google,yahoo and msn in their place.Paying for targeted traffic is way to exensive for the little people on the internet.

So why not show them where to get advertising budget when they ask for it.It make sense to help our affiliate's in creating Wealth from Scratch as New advertising budget techniques arive.
http://www.mybusinessmarketingcenter.com

10:56 PM, July 29, 2006  
Anonymous Anonymous said...

Well done!
[url=http://chlhcetx.com/vzjb/cosm.html]My homepage[/url] | [url=http://suzdlxsa.com/wmpc/cyyk.html]Cool site[/url]

1:27 PM, September 16, 2006  
Anonymous Anonymous said...

Well done!
My homepage | Please visit

1:27 PM, September 16, 2006  
Anonymous Anonymous said...

Good design!
http://chlhcetx.com/vzjb/cosm.html | http://exesxswg.com/bruz/hkvj.html

1:27 PM, September 16, 2006  
Anonymous Business Opportunity said...

Our "revolutionary" home based business idea pays out at 100% and 5 levels deep, earning YOU a very solid and long-term monthly income! With our monthly-paid referral bonuses, plus our unlimited 100% matching bonuses, you can be in profit quickly and easily!

8:54 PM, September 24, 2006  
Blogger Scott A. Edwards said...

WATCH CAREFULLY...Here it is... FREE advertising, FREE download. No cost to you! Get your FREE download NOW! Make money and get FREE advertising! This is a great program for you to take advantage of... Check this out now for FREE!

To find out more visit: low cost mlm site. It successfully exposes FREE information covering traffic and low cost mlm related stuff. Don't forget, FREE, FREE, FREE!!!

1:17 PM, October 22, 2006  
Blogger Scott A. Edwards said...

AMAZING...!!! Dear friends, Now you can Fulfill your destiny! LordoftheHits is the lord of all exchanges. One exchange to rule them all!!! Hook up NOW with this exiting program. Click here: FREE INFORMATION

4:57 PM, October 23, 2006  
Anonymous Scott Arthur Edwards said...

Dear friends, Now you can help yourself take advantage of the huge surpluses of FREE advertising in your spare time, in the comfort of your own home. Hook up NOW with this exiting program. Click here: FREE INFORMATION

7:30 PM, November 22, 2006  
Anonymous Anonymous said...

BUILD YOUR LIST, WIN THE WEEKLY DRAW, EARN MONEY! Learn more about it here: make a living online site. It pretty much covers make a living online related stuff and it is FREE to join.

11:07 PM, November 23, 2006  
Anonymous Anonymous said...

Hello
Take a look adipex | | didrex | meridia | adipex | phentermine | phentermine
phentermine | ativan | valium | xanax | sex for adult
Bye

7:47 PM, December 08, 2006  
Anonymous Anonymous said...

Hi
xanax phentermine xanax xanax phentermine phentermine phentermine tramadol tramadol tramadol levaquin levaquin levaquin norvasc sex babe casino casino
lroulette blackjack lpoker phentermine
G'night

11:14 PM, December 24, 2006  
Anonymous Anonymous said...

Great site

http://home-forclosure.eticketsontime.com home forclosure
http://beaded-bracelet.eurocarexpert.com beaded bracelet
http://bathroom-lighting.casinogamefactory.com bathroom lighting
http://chicago-single.casinogamefactory.com chicago single
http://silk-scarf.eurocarexpert.com silk scarf
http://diet-points.asapdeals.com diet points
http://garden-furniture.boulevardprivates.com garden furniture
http://chicago-florist.kisswings.com chicago florist
http://tote-bag.eurocarexpert.com tote bag
http://cpa-course.lysog.info cpa course
http://folding-table.lysog.info folding table
http://replica-handbag.lysog.info replica handbag
http://altima-nissan.lysog.info altima nissan
http://gym-bag.malig.info gym bag
http://adhd-treatment.malig.info adhd treatment

Thanks.

3:44 PM, January 22, 2007  
Anonymous Anonymous said...

Hi
Propose to join video chat & welcome to this useful site: lesbian

Bye

7:34 AM, February 04, 2007  
Anonymous Anonymous said...

Hello. Alone on Valentine's Day? Live adult chat
for fun

9:49 AM, February 12, 2007  
Anonymous Anonymous said...

Hi. Alone on Valentine's Day? Adult live chat & Movie Pages Try to find partner in your area!
Enjoy

10:56 AM, February 12, 2007  
Anonymous Anonymous said...

Hi. Use these helpful search engines BD search & movie pages and try to find all you need in your area!
for fun

12:48 PM, February 16, 2007  
Anonymous Anonymous said...

Hi. Use this search engine for best result: BD search Find all you need in your area!
Enjoy

11:58 AM, February 17, 2007  
Anonymous Anonymous said...

Hi all!. Use these helpful search engines TFO search & movie pages and try to find all you need in your area!
for fun

6:40 AM, February 21, 2007  
Anonymous Anonymous said...

Hello. Use this search engine for best result: BDsearch Find all you need in your area!
Enjoy

11:43 AM, February 23, 2007  
Anonymous Anonymous said...

Hi! Echt cooler Blog, den du da hast!
Würd mich freuen wenn du auch mal bei mir vorbeischaust: http://guenterlechner.fuelsaveteam.com/

11:45 AM, February 23, 2007  
Anonymous Anonymous said...

http://www.adquity.com

Classifieds for our community. Buy, sell, trade, date, events... post anything. Adquity Classifieds.

http://www.adquity.com

6:59 AM, March 01, 2007  
Anonymous Anonymous said...

Hi all!
Check out search engine:BD search You can find all in your area.
Bye

10:24 AM, March 01, 2007  
Anonymous Anonymous said...

Watiti.com
Join me and my circle of friends at http://www.watiti.com, an online social networking community that connects people from all over the world.

Meet new people, share photos, create or attend events, post free classifieds, send free e-cards, listen music, read blogs, upload videos, be part of a club, chat rooms, forum and much more!

See you around! Bring all your friends too!

Watiti.com

8:57 PM, March 06, 2007  
Anonymous Anonymous said...

I just thought I'd let you know that you've got a great blog here! I will bookmark it for sure!

My site Live Streaming Video pretty much covers Live Streaming Video related material.

Check it out when you have some time.

Kind regards,
http://www.VideoEmailJob.com

8:42 PM, March 11, 2007  
Anonymous Anonymous said...

I wonder ,.............. were to find boyfriend to my sister?................... Joke:)
My online friends propose this link to use - Fast finder - As for me,........ I think life is now!

7:41 AM, March 15, 2007  
Anonymous Anonymous said...

For Greetings give sweets!
The given place is privatized by sex and health! Whether probably for health to correct good sex?!
Thanks!
Welcome to sexy teens | movie pages | picture pages | webcam movie | adult friend finder.
Here pages devoted to health are published.
It is the large search robot, which can help to receive this or that information and as to get this or that goods!
Tramadol | Phentermine | Buy Xanax | Xanax and all that is necessary.
G'luck!

4:14 PM, April 01, 2007  
Anonymous Anonymous said...

Business Opportunities

7:34 PM, April 03, 2007  
Anonymous Anonymous said...

For Greetings give sweets!
The amateur of Sex and Health
Welcome to sexy teens | movie pages | picture pages | webcam movie | adult friend finder.
Here pages devoted to health are published.
See you soon!
_______________________________________________________________________________
http://hometown.aol.com/adultfashionxxx9/Movie+Pages.htm
http://hometown.aol.com/adultfashionxxx9/PicturePages.html
http://hometown.aol.com/adultfashionxxx9/WebcamMovie.html
http://hometown.aol.com/adultfashionxxx9/adultfriend.html

5:42 PM, April 14, 2007  
Anonymous viagra said...

Disaster is likely to wreak havoc in the life of an individual as soon as he becomes victim to erectile dysfunction and the most significant dreadful consequence of erectile dysfunction is that the afflicted man becomes incapable of facilitating erections required for sexual intercourse. The sexual vacuum resulted from erectile dysfunction prompts the sufferer to opt for anti-impotency pills, most especially the viagra medication that was approved by FDA (Food and Drugs Administration) as a clinically effective drug to cure erectile dysfunction in men. Viagra is meant to be administered by patients only after availing of viagra prescription from the doctor. The prescription for Viagra provided by the doctor spells out that the patient suffering from erectile dysfunction seriously need Viagra to treat his disorder and further authorizes the patient to avail of Viagra from the pharmacist.

2:09 AM, October 03, 2007  
Anonymous Anonymous said...


Also visit my site ... http://ophiocordyceps-sinensis.com/

11:01 AM, February 11, 2013  
Anonymous Anonymous said...



my blog post :: lottery software

3:19 AM, March 03, 2013  
Anonymous Anonymous said...

tramadol 50mg legal purchase tramadol online - tramadol 50 mg while breastfeeding

4:05 AM, April 15, 2013  
Anonymous Free Poker Money said...

One of your articles is good but other are average. Your blog is good anyway.
--------------------------
Poker bonus
no deposit poker bonus
Texas Hold'em is the most popular poker game in the world. Almost all the major tournaments are played in this version. You can use the set betting limits or no limit play. The basic rules of the game are relatively simple and can learn quite quickly, but in the achievement of mastery can take a long time. This article presents the most important rules of Texas Hold'em poker.
GAME
In Texas Hold'em usually plays in a standard 52 card deck. The game can participate from two to ten people. Before the start you need to determine which player will be a symbolic "dealer" in the hand. This does not mean that the player actually has to deal cards (online cards are dealt automatically and casinos deal with the dealers). Place this player marked a special ring (called "dealer button" or "button"). It lists which players must pay for their blinds (called "blinds"). These plants are the nucleus of the pot, so that every hand is "anything to win". Always two players must pay the first blind bets (they are put into the pot before the players see their cards). One person pays the "small blind" (called "small blind"), and another on the left - the "big blind" (called the "big blind"), usually two times the small blind. What symbolic hand dealer is changing, so that each of the players from time to time is the dealer, paid a small and big blind - all at the same frequency. Gratis bonos poker
Startkapital pokern bonus
When the blinds are already in the pot, every player gets two cards face down. These are the player's hole cards (called "hole cards"), for inspection only to the owner and he can only use them in the game. Then the middle of the table is gradually taught the five community cards that all players can use to create the best hand. Of the seven cards available to make the best possible five-card hand.
ROUND FOUR AUCTION
Before the flop (PRE-FLOP) - first round of betting. All play that occurs before detection of the first three community cards (the "flop") are referred to as "pre-flop". Players are watching their hole cards and have a chance to play first.
The player sitting to the left of the person who paid the big blind, the first decision. This item is called "under the gun" (literally "under the gun" / "under the gun"). This is because it is the worst in terms of decision-making.
Each player has at this stage to play the following options:
Belt / fitting of / Reset card (called fold): Playing it means giving up the game later in the hand and muck. Match, you lose the right to the money inserted into the pot, even if you have paid the blinds.
Check (called call): Playing it means that the alignment of early plant you want to continue playing the hand. Remember that after the flop will also be able "to wait" (called "check"), if no one before you has not paid the first bet.
Skewer (called raise): Playing it means that you want to place a bet higher than its predecessor. If your round of betting nobody else's set, your play will not puncture, only placing a bet (ang "bet").
instant money for texas holdem
Poker senza deposito

2:31 AM, June 18, 2013  

Post a Comment

<< Home