系列文章目录:

    使用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.cs类的checkLevel() 方法从类外的任何方法都不可达,它是死亡的代码。死亡代码是从未被公共方法直接或间接的调用,或者被其他的死亡代码调用。    

  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.)

值得注意的是,这个问题(死亡代码)有可能是个伪命题,因为有可能这个方法是通过反射来调用的。(这是不规范的实践,只通过反射调用的私有方法应该有很好的记录来说明。)

Unused Method(不再使用的方法)——Dead Code(死亡代码)的更多相关文章

  1. myeclipse 写java代码提示 dead code 原因

    经常使用MyEclipse要么Eclipse编辑写java程序猿代码.您可能经常会遇到一个黄色警戒线:dead code:一般程序猿遇到这些问题都会置之不理,反正也不影响程序的编译运行.对,这不是bu ...

  2. java 异常 之 实战篇(trows 和 try catch Dead Code)

    一:throws 和 trycatch 差别 (1)比如.publicFileWriter(String fileName) throws IOException{} 我在mian中创建一个FileW ...

  3. ASM(四) 利用Method 组件动态注入方法逻辑

    这篇继续结合样例来深入了解下Method组件动态变更方法字节码的实现.通过前面一篇,知道ClassVisitor 的visitMethod()方法能够返回一个MethodVisitor的实例. 那么我 ...

  4. myeclipse 编写java代码提示 dead code 原因

    经常使用MyEclipse或Eclipse编辑器编写java代码的程序员,可能经常遇到一个黄线警告提示:dead code:一般程序员遇到这些问题都会置之不理,反正也不影响程序的编译执行.对,这不是b ...

  5. 35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n

      35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现in ...

  6. SSIS Error The Execute method on the task returned error code 0x80131621

    Error Message: The Execute method on the task returned error code 0x80131621 (Mixed mode assembly is ...

  7. 查找无用代码Dead Code的一些心得

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:查找无用代码Dead Code的一些心得.

  8. JAVA进阶之旅(二)——认识Class类,反射的概念,Constructor,Field,Method,反射Main方法,数组的反射和实践

    JAVA进阶之旅(二)--认识Class类,反射的概念,Constructor,Field,Method,反射Main方法,数组的反射和实践 我们继续聊JAVA,这次比较有意思,那就是反射了 一.认识 ...

  9. Knowledge Point 20180308 Dead Code

    不知道有没有前辈注意过,当你编写一段“废话式的代码时”会给出一个Dead Code警告,点击警告,那么你所写的废物代码会被编译器消除,那么如果你不理睬这个警告呢?编译后会是什么样的呢?下面我们写点代码 ...

随机推荐

  1. Android 如何执行java命令

    android的程序基于java开发,当我们接上调试器,执行adb shell,就可以执行linux命令,但是却并不能执行java命令. 那么在android的shell中是否就不能执行java程序了 ...

  2. onlyoffice文档协作的权限开发,利用casbin和golang语言

    登录用户,对于已经进行了权限设置的文档,将根据权限数据库,比对用户名,当与用户有关时,就显示相对应的权限,当都与登录用户无关时,则显示拒绝访问: 对于未登录用户,已经设置了权限的文档,都将显示拒绝访问 ...

  3. JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)

    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...

  4. Java:运算符的问题

    算术运算符: 算术运算符的注意问题 1. 如果对负数取模,可以把模数负号忽略不记,如:5%-2=1.但被模数是负数就另当别论. -5%3=-2:-5%-3=-2:符号跟被模数(左边). 2. 对于除号 ...

  5. 阿里云rds实例恢复到本地

    摘要: 前提: 1,阿里云数据库备份实例,恢复数据的时候需要将数据恢复到本地数据库,是不能直接恢复到RDS上的. 2,需要在本地服务器上下载一个数据库,尽量和RDS数据库版本保持一致.(我现在用的是5 ...

  6. __细看InnoDB数据落盘 图解 MYSQL

    http://hatemysql.com/?p=503 1.  概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往下,看看数据从InnoDB的 ...

  7. fab提供远程IP和账号密码

    #!/usr/bin/python #-*- coding: UTF-8 -*- from fabric.api import * from fabric.context_managers impor ...

  8. OpenGL超级宝典笔记——画三角形(转)

    http://my.oschina.net/sweetdark/blog/161002 学习了画线的知识,我们可以使用GL_LINE_LOOP来画闭合的多边形.但是使用这种方式画出来的只有线框,多边形 ...

  9. MySQL基础之 排序与限制,聚合

    排序与限制 ORDER BY 作用:取出按照某个字段进行排序后的记录结果集. 配合:常与DESC  和ASC一块使用:默认是ASC,表示升序.DESC表示降序 LIMIT 作用:用于显示数据的一部分记 ...

  10. kdTree相关原理及c++实现

    kdTree概念 kd-tree或者k维树是计算机科学中使用的一种数据结构,用来组织表示k维空间中点的集合.它是一种带有其他约束条件的二分查找树.Kd-tree对于区间和近邻搜索十分有用.一般位于三维 ...