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:
public void CreeateOrder()
{
#region Validate the parameters
#endregion
#region create the order
#endregion
#region create the order item
#endregion
}
Note that not all people use region
s like this. Many people use comments instead of region
s 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()
{
#region Validate the parameters
#endregion
}
private bool InsertOrder()
{
#region create the order
#endregion
}
private bool InsertOrderItem()
{
#region create the order item
#endregion
}
public void CreateOrder()
{
If(ValidateOrder())
{
if(InsertOrder())
{
InsertOrderItem();
}
}
}
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.