Click here to Skip to main content
Click here to Skip to main content
Go to top

Violating Single Responsibility Principle using VisualStudio Region

, 28 Apr 2010
Rate this:
Please Sign up or sign in to vote.
This article shows how you may violate Single Responsibility Principle using Visual Studio's region feature and how to correct it.

Single Responsibility Principle (SRP) says that "THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.". Although this article mentions only about class, I think the SRP also applies to methods within the class: there should never be more than one reason for a method to change.

Visual Studio provides a good way to mark off sections of file in the form of "region" so they can be collapsible and the code can be organized. Many people use regions in a big method to organize the code. For example:

public void CreeateOrder(/*some parameters*/)
{
	#region Validate the parameters
	//code goes here
	#endregion

	#region create the order
	//insert the order data in the database
	#endregion

	#region create the order item
	//insert the item data in the database
	#endregion
}

Note that not all people use regions like this. Many people use comments instead of regions in these kind of methods.

As you can see, this is a clear violation of the single responsibility principle. The method does more than one thing: it validates the order data, creates a top level order and creates order items. This can certainly be put into a separate method.

private bool ValidateOrderData(/*some parameters*/)
{
	#region Validate the parameters
	//code goes here
	#endregion
}

private bool InsertOrder(/*order related parameter*/)
{
	#region create the order
	//insert the order data in the database
	#endregion
}

private bool InsertOrderItem(/*order item related parameter*/)
{
	#region create the order item
	//insert the item data in the database
	#endregion
}

public  void CreateOrder(/*Some parameter*/)
{
	If(ValidateOrder(/*parameter list*/))
	{
		if(InsertOrder(/*order parameter*/))
		{
			InsertOrderItem(/*order item parameter*/);
		}	
	}
}

As you can see, wherever you are using "region" in your method to demarcate the code, you can very well put that code in a separate method.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ashish Sheth
Software Developer (Senior) McAfee
India India
a 24/7 Programmer. Apart from programming, I do eat, sleep, breath for living. My areas of interests are C#, .Net Framework and Software development in general.
When I am not programming and not doing anything else(?), I play chess.

Comments and Discussions

 
Question3 Methods = 3 Reasons for Class to Change? Pinmemberloresayer28-Apr-10 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Apr 2010
Article Copyright 2010 by Ashish Sheth
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid