C#中使用#region指令的一些想法

作者: 麒麟  來源: 博客園  發布時間: 2010-09-09 21:25  閱讀: 23830 次  推薦: 1   原文鏈接   [收藏]  

  這篇文章我將不會去介紹如何使用#region指令。因為每個C#開發人員都應該見過和使用過#region指令的。這篇文章將討論如何在代碼中正確的使用它。使用#region就是將一些有關聯的代碼組織在一起,然后折疊起來。這樣你就在一個函數中看不到很長的令人頭痛的代碼段。例如:

 
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
++;
}
}

  當然這段代碼很簡單。在實際項目中,你可能看到上百行甚至更多的代碼在一個#region中。如果把它折疊起來。看起來就會很整潔。是吧?

 
public void DoSomething()
{

bool shouldIDoSomething;

[Decide
if I should do something]

if(shouldIDoSomething)
{
done
++;
}
}

  只是把一些代碼和一些變量組合起來放在#region中。如果你在仔細想想,其實我們相當與創建了一個新的函數,只是將這些方法內置到當前函數中。一個函數只做單一的一件事情,這是Clean Code這本書的一個原則。為什么我們不把它提取為一個函數呢,這樣一來,一個函數就只做一件事情了。

 
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
}
}

  上面看起來就清楚很多,因為我們降低了之前的DoSomething函數的復雜度。兩個函數可以分開測試,確保沒有邏輯錯誤。
  小段總結1:  #region 不適合在大方法中使用,當你在一個方法中使用#region 的時候,停下來想想你剛剛寫了什么代碼?大多數時候,你可以將這些代碼段獨立成一個函數。

  看看下面這段非常漂亮的代碼:

 
#region Get Customer

public void GetCustomer()
{

// code to get the customer
}

#endregion

#region Save Customer

public void SaveCustomer()
{

// code to save the customer
}

#endregion

  將它折疊之后,變成下面這樣:

 
[Get Customer]

[Save Customer]

  這樣做很容易閱讀嗎?這樣做的目的是什么,我不明白?代碼折疊就會變得更好?我覺得這樣做只會讓代碼更難以閱讀,因為你每次要看region中的代碼,你都要展開一次。

  小段總結2:不要因為你能,你就使用#region 。

  再看下面這個例子

 
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
}

  如果你將這個例子和本文中的第一個例子做下比較,你可能會看到它們的共同點。他們是相同的,不過一個是在類中,一個是在函數中,層級不同而已。這里在提一個原則:單一職責原則,一個類應該只有一個職責。看上面的類,你可以很容易看出它有兩個職責:價格計算和折扣計算。折扣計算的方法被放到一個#region中。同樣,可以將它們提取出來做為一個新類。

  小段總結3:可以將一組相關的函數提取到一個職責單一的新類中。

  那我們到底怎么使用 #region 呢。將東西用它來分組,它是非常有用的。在我寫的類中或多或少有幾個#region,用來對類中不同的結構進行分組。比如: fields, properties, methods, events, types等。如果你打開我寫的類文件,你會看到結構如下:

 
public class SomeClass
{
[Events]

[Fields]

[Properties]

[Methods]
}

  總的來說:我將#region看成能控制閱讀源代碼的復雜度的一種方式。因為你可以將一些相關的代碼放在一個區域(#region)里面。但是,這不是你隨便就創建新方法或者新類的借口。其實Region并不能消除復雜度,它只是在你閱讀代碼的時候,隱藏了部分代碼。你必須通過寫出小巧,清晰,重點突出的方法和類,才能控制代碼的復雜度。當你做到這些的時候,你甚至會發現#region是不必要的。

  本文翻譯:About #region preprocessor directive

1
0
 
標簽:C#
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()