The term you're looking for is "code coverage" and there are various tools that will generate that information. You would have to make sure that you exercise every possible path through your code in order to be able to detect "dead code" with such a tool though, which is only possible with a really extensive set of tests.
Most compilers have some level of dead code detection, but that only detects code that cannot possibly be called, not code that will never be called due to program logic, etc..
For functions, try a global search on the function name, and analyze what you get. Dead code inside a function will usually be findable.
If you suspect a function of being unused, you can remove it, or comment it out, and see if what you've got still compiles.
This only works on functions that are unused because they are no longer called. Functionality that is never used because the control path through the code is no longer active is harder to find, and code analysis tools won't do well at finding it either.
发布评论
评论(3)
您要查找的术语是“代码覆盖率”,有各种工具< /a> 将生成该信息。您必须确保在代码中执行所有可能的路径,以便能够使用这样的工具检测“死代码”,而这只有通过一组真正的测试才能实现。
大多数编译器都有一定程度的死代码检测,但这只检测不可能被调用的代码,而不是由于程序逻辑等而永远不会被调用的代码。
编辑:
特别针对Python: 如何在 Python 代码中找到未使用的函数?
对于 Java:如何在 java 项目中查找未使用/死代码,Java:Objective-C 的死代码消除
:Xcode -- 查找项目中的死方法,清理 Objective-C 代码
The term you're looking for is "code coverage" and there are various tools that will generate that information. You would have to make sure that you exercise every possible path through your code in order to be able to detect "dead code" with such a tool though, which is only possible with a really extensive set of tests.
Most compilers have some level of dead code detection, but that only detects code that cannot possibly be called, not code that will never be called due to program logic, etc..
edit:
for Python specifically: How can you find unused functions in Python code?
for Java: How to find unused/dead code in java projects, Java: Dead code elimination
for Objective-C: Xcode -- finding dead methods in a project, Cleaning up Objective-C code
对于函数,尝试对函数名称进行全局搜索,并分析所得到的结果。函数内的死代码通常是可以找到的。
如果您怀疑某个函数未被使用,您可以将其删除或注释掉,然后查看您所获得的内容是否仍然可以编译。
这只适用于未使用的函数,因为它们不再被调用。由于通过代码的控制路径不再活动而从未使用过的功能更难找到,并且代码分析工具也无法很好地找到它。
For functions, try a global search on the function name, and analyze what you get. Dead code inside a function will usually be findable.
If you suspect a function of being unused, you can remove it, or comment it out, and see if what you've got still compiles.
This only works on functions that are unused because they are no longer called. Functionality that is never used because the control path through the code is no longer active is harder to find, and code analysis tools won't do well at finding it either.
您可以使用代码覆盖率报告来找出未使用的函数或从未执行过的函数的一部分。
根据逻辑,您可以将它们视为死代码/未使用的代码。
可以使用的流行代码覆盖工具:
You can use the code coverage report to find out the function which are not used or part of function which is never executed.
Based on the logic, you can treat them as dead code/unused code.
Popular code coverage tools that can be used: