Los Techies : Blogs about software and anything tech!

Need Help Spotting that Hard to Test Code?


I’m a big fan of Jon Lam's Vibrant Ink Visual Studio theme. Here’s why: If you look at the syntax highlighting in the following code, you’ll see some yellow text. In this theme, or any any other theme that does it well, you’ll notice class names stand out a bit on their own. Referencing a class name means you’re probably either creating a new object or referencing a static function or property. Both of these make for hard to test code. Code that is hard to test is brittle. Apply Murphy’s Law and you’re in trouble.

public class CustomerNotifier
{
private readonly EmailService emailService;

public CustomerNotifier()
{
emailService =
new EmailService();
}

public void SendNotification(IUser user, string subjectText, string messageText)
{
if(user.Subscriptions().Contains(this))
{
MailMessage mailMessage = new MailMessage("customerService@company.com",
user.Email, subjectText, messageText);
emailService.SendTo(user, mailMessage);
}
}
}

If I want to test whether the SendTo message was called on the email service when the instance is contained in the user’s subscriptions and NOT called when the user isn’t subscribed to it. I can’t really think of a good way to do so.

Time to Redo This a Bit

My theme settings are telling me that EmailService and MailMessage should be abstracted. This is because they’re “yellow”. Not really the reason, but those are what I see as flags for improvement. Instead of newing-up a MailMessage, I’m going to create my own abstraction. I’m also going to inject the EmailService (as an abstraction) into the constructor. Later on, we can make assertions on it using a mocked object.

public class CustomerNotifier
{
private readonly IEmailService emailService;

public CustomerNotifier(IEmailService emailService)
{
this.emailService = emailService;
}

public void SendNotification(IUser user, IMailMessage mailMessage)
{
if(user.Subscriptions().Contains(this))
{
mailMessage.From =
"customerService@company.com";
emailService.SendTo(user, mailMessage);
}
}
}

This helps me quickly identify areas of my code that seem to be designed poorly. When I see a lot of whatever color my class names are, I see areas that can be improved. This code is much easier to test now.

Kick It on DotNetKicks.com
Posted Mar 10 2009, 02:13 AM by Chris Missal

Comments

Scott Muc wrote re: Need Help Spotting that Hard to Test Code?
on 03-10-2009 3:09 AM

It's funny that you post this because I use the same theme and I get a sense of happiness when I see lots of blue Interfaces in my classes. Never really thought about why I like those classes until you pointed this out!

Harry M wrote re: Need Help Spotting that Hard to Test Code?
on 03-10-2009 5:16 AM

You should make strings a horrible pink

DotNetShoutout wrote Need Help Spotting that Hard to Test Code? - Chris Missal -
on 03-10-2009 8:22 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Chris Missal wrote re: Need Help Spotting that Hard to Test Code?
on 03-10-2009 9:22 AM

@Harry

I've thought about doing something like that. I read one suggestion that you use a yellow background with red text just to make you want to rethink using it. Instead I removed my quote key from my keyboard! :P

Anne Epstein wrote re: Need Help Spotting that Hard to Test Code?
on 03-10-2009 6:36 PM

Not sure that color scheme is for me (cazy though it sounds, I've gotten used to the white bg), but I really like the idea - think I'll try customizing what I've got to do that too.  Thanks!

Farger i Visual Studio editoren wrote Farger i Visual Studio editoren
on 03-18-2009 10:43 AM

Pingback from  Farger i Visual Studio editoren

Gunnar Peipman's ASP.NET blog wrote Links 2009-04-06
on 04-06-2009 3:59 PM

SharePoint Customizing SharePoint Context Menus Hidden SharePoint Lists, Fields, and other Advanced List

Koistya `Navin wrote re: Need Help Spotting that Hard to Test Code?
on 04-07-2009 8:29 AM

Looks like IoC becomes more and more popular..

Gunnar Peipman's ASP.NET blog wrote Links 2009-04-16
on 04-16-2009 2:44 PM

SharePoint Logging to the Workflow History List in SharePoint workflows Inconvenient SPWeb.GetListItem

Add a Comment

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

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems