Unused Method(不再使用的方法)——Dead Code(死亡代碼)


    系列文章目錄:

    使用Fortify進行代碼靜態分析(系列文章)

 

Unused Method(不再使用的方法)

    示例:        

1 private bool checkLevel(string abilitySeqno, string result) 2 { 3     return hrDutyexamProjectAbilityDS.CheckImportLevel(abilitySeqno, result); 4 }

    Fortify解釋:

    The method checkLevel() in AbilityImport..cs is not reachable from any method outside the class. It is dead code. Dead code is defined as code that is never directly or indirectly executed by a public method,or is only called from other dead code.

     AbilityImport.cscheckLevel() 方法從類外的任何方法都不可達它是死亡的代碼死亡代碼是從未被公共方法直接或間接的調用,或者被其他的死亡代碼調用。    

  Fortify示例1:    

1 public class Dead { 2   private void DoWork() {//永遠不會被調用
3     Console.Write("doing work"); 4  } 5   public static void Main(string[] args) { 6     Console.Write("running Dead"); 7  } 8 }

     Fortify示例2:    

 1 public class DoubleDead {  2   private void DoTweedledee() {  3  DoTweedledumb();  4  }  5   private void DoTweedledumb() {  6  DoTweedledee();  7  }  8   public static void Main(string[] args) {  9     Console.Write("running DoubleDead"); 10   }

      In the following class, two private methods call each other, but since neither one is ever invoked from anywhere else, they are both dead code.

在這個類中,兩個私有方法相互調用,但是它們其中任意一個都沒有被其他的類調用,它們是死亡代碼.

A dead method may indicate a bug in dispatch code.

死亡方法可能意味着在分支代碼中存在BUG 

Fortify示例: 

 1 public ScaryThing GetScaryThing(char st) {  2   switch(st) {  3     case 'm':  4       return GetMummy();  5     case 'w':  6       return GetMummy();  7     default:  8       return GetBlob();  9  } 10 }

 If method is flagged as dead named GetWitch() in a class that also contains the following dispatch method, it may be because of a copy-and-paste error. The 'w' case should return GetWitch() not GetMummy().

如果類中的死亡方法 GetWitch() 也存在上面的分支代碼邏輯,有可能這是復制粘貼代碼時造成的錯誤,當case匹配w時,應該調用GetWitch() 而不是GetMummy()方法。

In general, you should repair or remove dead code. To repair dead code, execute the dead code directly or indirectly through a public method. Dead code causes additional complexity and maintenance burden without contributing to the functionality of the program.

通常,你應該修復或者移除死亡代碼,你可以通過在公共方法直接或間接執行這個方法來修復它。死亡代碼增加了復雜性和維護的工作量,同時對系統的功能無所裨益。

This issue may be a false positive if the program uses reflection to access private methods. (This is a non-standard practice. Private methods that are only invoked via reflection should be well documented.)

  值得注意的是,這個問題(死亡代碼)有可能是個偽命題,因為有可能這個方法是通過反射來調用的。(這是不規范的實踐,只通過反射調用的私有方法應該有很好的記錄來說明。)


免責聲明!

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



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