Click here to Skip to main content
13,201,050 members (58,500 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.3K views
4 bookmarked
Posted 22 Jul 2011

Single Responsibility Principle

, 22 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Single responsibility principle

SRP is one of the easiest principles to learn, but one of the hardest to master. The reason for this is that it can be quite hard to see if a method or class breaks SRP or not. I got a few simple rules that will help you check if you are breaking the principle.

First of all, let's check the definition of SRP:

Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.

Hence my interpretation varies a bit from the original statement, as I like to apply it to classes, methods and interfaces. If you do not agree: Go buy an ice-cream, eat it, put a smile on your face and continue reading.

XmlDoc is an excellent tool if you use it correctly. So many users abuse it and scream out to the world their product/library/etc. has DOCUMENTATION! The DoXZ! yay! What they fail to mention is that the documentation is simply a reflection of the methods and classes. Read my other blog posts to get a few tips on how to write good documentation. Back to SRP. Documentation can be a tool for SRP too. If you find yourself using ANDs when describing a method, class or an method argument, you have most likely broken SRP. Refactor.

When talking about SRP, it’s important to know what is meant by single responsibility. When it comes to methods, they should only contain logic for one type of responsibility. Here is a small example:

public class UserService
{
     public void Register(string email, string password)
     {
          if (!email.Contains("@"))
              throw new ValidationException("Email is not an email!");

         var user = new User(email, password);
         _database.Save(user);

         _smtpClient.Send(new MailMessage("mysite@nowhere.com", email){Subject="HEllo fool!"});
     }
}

The name Register suggests that the method should register the user in the system. Doing email validation doesn’t seem to belong in an register method. Let’s break it out into a new method.

public class UserService
{
     public void Register(string email, string password)
     {
          if (!ValidateEmail(email))
              throw new ValidationException("Email is not an email!");

         var user = new User(email, password);
         _database.Save(user);

         _smtpClient.Send(new MailMessage("mysite@nowhere.com", email){Subject="HEllo fool!"});
     }

     public bool ValidateEmail(string validateEmail)
     {
         return email.Contains("@");
     }
}

One might wonder why that line of code should be moved. It’s so small. In reality, email validation might be larger, but that’s not really important. Following SRP usually makes it easier to follow DRY (Don’t repeat yourself) too, since you make methods smaller and let them solve a specific problem. Hence it’s a lot easier to reuse code.

If we continue to look at the method, we’ll see that it also sends an email and is therefore also responsible of delivering the email. Let's move that to a new method too.

public class UserService
{
     public void Register(string email, string password)
     {
          if (!ValidateEmail(email))
              throw new ValidationException("Email is not an email!");

         var user = new User(email, password);
         _database.Save(user);

         SendEmail(new MailMessage("mysite@nowhere.com", email){Subject="HEllo fool!"});
     }

     public bool ValidateEmail(string validateEmail)
     {
         return email.Contains("@");
     }

     public bool SendEmail(MailMessage message)
     {
         _smtpClient.Send(message);
     }
}

OMG! We did it again. A single line method. Is this sh!t really necessary? Yep, it is. When the application grows, you’ll see that you might want some templating. By using a separate method, you could replace commonly used macros like #SITEURL#, #PRODUCTNAME#, etc. in that method instead of duplicating the code in each method that wants to send an email.

Now it’s time to move on and look at the class. Does it follow the SRP? Do it? Nooo, don’t try to fool me. I got some NINJA zkillz. Promise! SendEmail and ValidateEmail has nothing to do with a class named UserService. Let’s refactor.

public class UserService
{
     EmailService _emailService;

     public UserService(EmailService emailService)
     {
         _emailService = emailService;
     }
     public void Register(string email, string password)
     {
          if (!_emailService.ValidateEmail(email))
              throw new ValidationException("Email is not an email!");

         var user = new User(email, password);
         _database.Save(user);

         emailService.SendEmail(new MailMessage("mysite@nowhere.com", email){Subject="HEllo fool!"});
     }
}

public class EmailService
{
     public bool ValidateEmail(string validateEmail)
     {
         return email.Contains("@");
     }

     public bool SendEmail(MailMessage message)
     {
         _smtpClient.Send(message);
     }
}

Actually, I’m unsure of whether the ValidateEmail method belongs in the EmailService class. What do you think? Leave a comment.

SRP is nothing that you do once. It’s something that you should keep doing during all types of development (like when writing new code and when you maintain the existing one).

Summary

Try to follow the rules given below to get a hang of SRP:

  1. Document your code. Using ANDs should make you check your code again.
  2. You should be able to associate all method names with the class/interface name. (A method called ValidateEmail cannot be associated with a class called UserService).
  3. A method should only contain logic that can be associated with the method name.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 22 Jul 2011
Article Copyright 2011 by jgauffin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid