C# 斷言 Assert


重構-斷言

現象:某一段代碼需要對程序狀態做出某種假設
做法:以斷言明確表現這種假設
動機:
常常有這種一段代碼:只有某個條件為真是,該改名才能正常運行。
通常假設這樣的假設並沒有代碼中明確表現出來,必須閱讀整個算法才能看出。
有時程序員會注釋這樣的代碼。
而現在這種重構介紹一種更好的技術:使用斷言明確標明這些假設。
斷言是一個條件表達式,應該總是為真。如果他失敗,就是bug。
因此斷言的失敗應該是一個非受控異常,斷言絕對不能被系統其它部分使用。實際上,程序最后的成品往往將斷言系統統統刪除,因此,標記某些東西是個斷言是很重要。
斷言作為調試和交流的輔助,在交流角度,斷言可以幫助程序閱讀者理解代碼所做的假設;在調試的角度,斷言可以在距離bug最近的地方抓住它們。
做法:
如果程序員不犯錯,斷言就不會對系統造成任何影響,所以加入斷言永遠不影響程序的行為。
如果發現某個條件始終為真,就加入一個斷言說明這種情況。
可以引入一個assert類,用於處理各種情況下的斷言。
不要濫用斷言,請不要用來檢查你認為應該為真的條件,請只用來檢查一定為真的條件。濫用斷言可能會造成難以維護的重復邏輯。
在一段代碼中引用斷言是有好處的,他迫使你重新考慮這段代碼的約束條件。
如果不滿足這些約束條件,程序也可以正常運行,斷言就不能帶來任何幫助,只會把代碼變得混亂,並且可能妨礙以后的修改。
如果斷言的所指示的約束條件不能滿足,代碼是否正常運行?如果可以就把斷言拿掉。
還需要注意斷言中重復的代碼。
C# 斷言代碼
  1. Boolean b1 = false;
  1. System.Diagnostics.Debug.Assert(b1);

斷言(Assert)與異常(Exception)

 

斷言是被用來檢查非法情況而不是錯誤情況,即在該程序正常工作時絕不應該發生的非法情況,用來幫助開發人員對問題的快速定位。異常處理用於對程序發生異常情況的處理,增強程序的健壯性、容錯性,減少程序使用中對用戶不有好的行為,不讓(通常也不必)用戶知道發生了什么錯誤。

  實際開發中,我們通常將Assert與異常混淆, 不知道什么時候使用Assert,什么時候使用異常處理。或者不用Assert,將一切情況都歸為異常。這樣一來,就掩蓋了問題,當問題發生的時候,很難進行定位,而這些問題本該是在開發的時候就解決掉的。同時,也增加了開銷(在c#中,debug.Assert()編譯成release版本時,不會產生任何代碼,而try/catch在debug/release版本中都是有代碼產生,運行時需要開銷)。

 

  Assert類位於Microsoft.VisualStudio.TestTools.UnitTesting 命名空間下,該命名空間提供支持單元測試的類。此命名空間包含許多屬性,這些屬性為測試引擎標識有關數據源、方法執行順序、程序管理、代理/主機信息以及部署數據的測試信息。Microsoft.VisualStudio.TestTools.UnitTesting 命名空間還包含自定義單元測試異常。

  MSDN中,有詳細的Assert類的公共方法:http://msdn.microsoft.com/zh-cn/library/Microsoft.VisualStudio.TestTools.UnitTesting.Assert(v=vs.100).aspx

本文簡單歸類一些簡單的使用方法。掌握Assert類,一個BestPractice就是,了解Assert的幾個主要方法,然后熟悉其重寫方法即可,整理表格如下:

Name

Description

AreEqual(+18)

AreNotEqual(+18)

Verifies that specified values are equal(or not equal). The assertion fails if the values are not equal(or equal).

Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

AreSame(+3)

Verifies that specified object variables refer to the same object. The assertion fails if they refer to different objects.

Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

Equal

Determines whether two objects are equal.

Fail(+3)

Fails an assertion without checking any conditions.

Displays a message, and applies the specified formatting to it.(if available)

InConclusive(+3)

Indicates that an assertion cannot be proven true or false. Also used to indicate an assertion that has not yet been implemented.  

Displays a message, and applies the specified formatting to it.(if available)

IsFalse(+3)

IsTrue(+3)

Verifies that a specified condition is false(or true). The assertion fails if the condition is true(not true).

Displays a message, and applies the specified formatting to it.(if available)

IsInstanceOfType(+3)

IsNotInstanceOfType(+3)

Verifies that a specified object is(or is not) an instance of the specified type. The assertion fails if the type is not(or is) found in the inheritance hierarchy of the object.

Displays a message, and applies the specified formatting to it.(if available)

IsNull(+3)

IsNotNull(+3)

Verifies that a specified object is null(or not null) . The assertion fails if it is not null(or is null).

Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

ReplaceNullChars

In a string, replaces null characters ('\0') with "\\0".

  

  關於異常處理, 在編寫代碼的時候,應充分考慮各種具體異常,而不簡單的catch到Exception,寫出更健壯的代碼。

  通常來說,能夠用Assert的地方,都可以用try/catch處理 。但這不是好習慣。We need to "Writing Clean Code".

 

  文中相關擴展連接,  

  Microsoft.VisualStudio.TestTools.UnitTesting Namespace

  Assert Class

  A Unit Testing Walkthrough with Visual Studio Team Test


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM