This article will not describe how to use the # Region Command. Because every C # developer should have seen and used the # Region Command. This article will discuss how to use it correctly in the code. # Region is used to organize and collapse associated code. In this way, you can't see a very long headache code segment in a function. For example:
public void DoSomething()
{
bool shouldIDoSomething;
#region Decide if I should do something
if(needToDoSomething && haventDoneSomethingThisDay)
shouldIDoSomething = true;
else
{
// do some other logic to decide and set shouldIDoSomething to some value
}
#endregion
if(shouldIDoSomething)
{
done++;
}
}
Of course, this code is very simple. In actual projects, you may see hundreds or more lines of code in a # region. If you fold it. It looks neat. Right?
public void DoSomething()
{
bool shouldIDoSomething;
[Decide if I should do something]
if(shouldIDoSomething)
{
done++;
}
}
Just put some code and some variables together in # region. If you think about it, we actually create a new function, just to build these methods into the current function. A function only does one single thing. This is a principle in the book "clean code. Why don't we extract it as a function? In this way, a function only does one thing.
public void DoSomething()
{
if(ShouldIDoSomething())
{
done++;
}
}
private bool ShouldIDoSomething()
{
if(needToDoSomething && haventDoneSomethingThisDay)
shouldIDoSomething = true;
else
{
// do some other logic to decide and set shouldIDoSomething to some value
}
}
The above seems much clearer, because we have reduced the complexity of the previous dosomething function. The two functions can be tested separately to ensure that there are no logical errors.
Summary 1:# Region is not suitable for use in the generous method. When you use # region in a method, stop and think about what code you just wrote? Most of the time, you can separate these code segments into a function.
Let's take a look at the following code:
#region Get Customer
public void GetCustomer()
{
// code to get the customer
}
#endregion
#region Save Customer
public void SaveCustomer()
{
// code to save the customer
}
#endregion
After it is folded, it becomes as follows:
[Get Customer]
[Save Customer]
Is this easy to read? What is the purpose of this? Is code folding better? I think this will only make the code more difficult to read, because every time you look at the code in region, You have to expand it once.
Summary 2:Do not use # region because you can.
Let's look at the example below.
public class PriceCalculator
{
public decimal CalculatePrice()
{
decimal price = 100m;
decimal discount = CalculateDiscount();
return price * (1m - discount));
}
#region Discount Calculation
private void CalculateDiscount()
{
decimal discount = 0m;
if(CanApplyDiscount())
discount = 0.05m;
return discount;
}
private void CanApplyDiscount()
{
// some logic, other method calls
}
// some other discount calculation methods
...
#endregion
}
If you compare this example with the first example in this article, you may see what they have in common. They are the same, but one is in the class, the other is in the function, and the level is different. Here we propose a principle: single responsibility principle, one class should have only one responsibility. Looking at the above class, you can easily see that it has two responsibilities: price calculation and Discount calculation. The discount calculation method is put into a # region. Similarly, they can be extracted as a new class.
Summary 3: a group of related functions can be extracted into a new class with a single responsibility.
How can we use # region. Grouping things with it is very useful. There are more or less several # region in the class I wrote, which is used to group different structures in the class. For example, fields, properties, methods, events, and types. If you open the class file I wrote, you will see the structure as follows:
public class SomeClass
{
[Events]
[Fields]
[Properties]
[Methods]
}
In general:I think of # region as a way to control the complexity of reading source code. Because you can put some related code in a region (# region. However, this is not an excuse for creating new methods or classes. In fact, region does not eliminate complexity. It only hides some code when you read the code. You must write small, clear, and highlighted methods and classes to control the complexity of the Code. When you do this, you may even find that # region is unnecessary.
About # region Preprocessor Directive