Click here to Skip to main content
15,907,497 members
Articles / Programming Languages / C#

Violating Single Resposibility Principle using Visual Studio Region

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
2 Jul 2012CPOL 8.8K   3   5
Wherever you are using "region" in your method to demarcate code, you can very well put that code in a separate method.

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 section of file in the form of "region" so they can be collapsible and the code can be organized. Many people use region in a big method to organize code. For example:

C#
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.

C#
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 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)


Written By
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

 
GeneralMy vote of 2 Pin
Chris Steenkamp18-Jul-12 3:46
Chris Steenkamp18-Jul-12 3:46 
In my opinion, code regions should be used to group your code in a logical structure in order to make it more human readable. They should not be used as some kind of abstraction of what the code is trying to accomplish.

I use regions to logically group my code into constants, private variables, private methods, functions, etc. so that I can minimize clutter when I am going through the code (this is the exact same principle behind code-folding, hiding the implementation details).

While some people may use regions to split code into a sort of "pseudo-object", your assertion that any region can be extracted into its own method is incorrect.

I find it much easier to understand another persons code if they have used regions correctly.
GeneralPerspective on SRP Pin
paritoshcoder06869-Jul-12 6:37
paritoshcoder06869-Jul-12 6:37 
Question[My vote of 2] 2 Pin
Kevin Marois6-Jul-12 5:51
professionalKevin Marois6-Jul-12 5:51 
GeneralComments too Pin
Simon_Whitehead3-Jul-12 14:03
Simon_Whitehead3-Jul-12 14:03 
GeneralMy vote of 5 Pin
Ramin Tarhande3-Jul-12 1:11
Ramin Tarhande3-Jul-12 1:11 

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

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