从应用程序中删除无效代码的最佳方法是什么? [英] What is the best way to remove dead code from your application?

查看:191
本文介绍了从应用程序中删除无效代码的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常感到,在遍历我的代码多次之后,我通常会得到一些函数,类或其他代码行,这些在上一修订版中是有意义的,但对新修订版不是很有用.我知道分析器可以告诉您在运行测试用例时调用了代码的哪一部分?但是,如何确定从未调用过代码的哪一部分来删除它,以使剩下的内容更易读呢?例如,有一种快速的方法来知道您的代码中的哪些函数没有从任何地方调用,并且可以安全地删除.对于小的代码库,这听起来像是一个琐碎的问题,但是当您的代码库多年来增长时,这将成为一个重要的而不是琐碎的问题.

I often feel that after iterating over my code for a number of times I am left with functions or classes or other lines of code in general which made sense in the previous revision but are not very useful for the new revision. I know that a profiler can tell you what part of your code was called when you run your test cases? But how does one go about identifying what part of the code never got called to remove it so that whats left is more readable? For example, is there a quick way to know which functions in your code are not being called from anywhere and can be safely removed. It may sound like a trivial question for a small code base, but when your code base grows over the years, this becomes an important and not so trivial question.

总结一下问题,对于不同的语言,删除无效代码的最佳方法是什么?是否有任何无关紧要的解决方案或策略.还是每种语言都提供了一种用于识别无效代码的工具.

To summarize the question, for different languages, what is the best approach to remove dead code? Are there any lanaguage agnostic solutions or strategies for this. Or does each language provide a tool for identifying dead code.

我们通常使用Java或Python或Objective-C进行编程.

We normally program in Java or Python or Objective-C.

推荐答案

您要查找的术语是代码覆盖率",其中有 really 大量测试集才能实现

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

:如何在其中找到未使用的函数Python代码?

对于Java:如何在Java项目 Java:消除无效代码

对于Objective-C: Xcode-在项目中查找无效方法清理Objective-C代码

for Objective-C: Xcode -- finding dead methods in a project, Cleaning up Objective-C code

这篇关于从应用程序中删除无效代码的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆