如何定义“不需要的代码”?
您如何定义“不需要的代码”?
编辑:
恕我直言,任何具有 0 个活动调用成员(递归检查)的代码成员都是不需要的代码。 (函数、方法、属性、变量都是成员)
How would you define "unwanted code"?
Edit:
IMHO, Any code member with 0 active calling members (checked recursively) is unwanted code. (functions, methods, properties, variables are members)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我对不需要的代码的定义:
您可以删除此类代码或重构它,但您不想保留它。
Here's my definition of unwanted code:
You can either remove such code or refactor it, but you don't want to keep it as it is around.
0 个活跃通话,近期不可能使用。 由于我使用 SVN(源代码管理),所以我宁愿永远不要注释掉任何内容,以防以后需要它。
0 active calls and no possibility of use in near future. And I prefer to never comment out anything in case I need for it later since I use SVN (source control).
就像您在另一个线程中所说的那样,根本不在任何地方使用的代码几乎是不需要的。 至于如何找到它,如果您使用的是 Java,我建议您使用 FindBugs 或 CheckStyle,因为这些工具会检查函数是否在任何地方使用,如果没有则将其标记为未使用。 非常适合摆脱不必要的体重。
Like you said in the other thread, code that is not used anywhere at all is pretty much unwanted. As for how to find it I'd suggest FindBugs or CheckStyle if you were using Java, for example, since these tools check to see if a function is used anywhere and marks it as non-used if it isn't. Very nice for getting rid of unnecessary weight.
经过短暂思考后,我想到了这三点:
我敢打赌还有很多,但这就是我定义不需要的代码的方式。
Well after shortly thinking about it I came up with these three points:
I bet there is a lot more but, that's how I'd define unwanted code.
在java中,我会用@Deprecated标记方法或类。
In java i'd mark the method or class with @Deprecated.
没有活动调用成员的任何 PRIVATE 代码成员(递归检查)。 否则你不知道你的代码是否没有在你的分析范围之外使用。
Any PRIVATE code member with no active calling members (checked recursively). Otherwise you do not know if your code is not used out of your scope analysis.
有些东西已经发布了,但这里还有另一个:
几乎做同样事情的函数。 (只有一个小的变量变化,因此整个函数被复制粘贴并且该变量被更改)
Some things are already posted but here's another:
Functions that almost do the same thing. (only a small variable change and therefore the whole functions is copy pasted and that variable is changed)
通常我会告诉编译器尽可能地发出烦人的噪音,它会选择我需要检查的 60% 的内容。 几个月前未使用的函数(在与 VCS 检查后)通常会被淘汰,除非它们的作者告诉我它们何时会实际使用。 缺少原型的东西也立即受到怀疑。
我认为尝试实现自动房屋清洁就像尝试制作一个 USB 设备来保证您“安全”地玩 俄罗斯轮盘赌。
最难检查的部分是添加到构建系统中的组件,很少有人注意到这些,未使用的杂物被留下来收集苔藓。
除此之外,我通常想要代码,我只希望其作者对其进行一些重构,并使他们的风格与项目的其余部分相同。
另一个有用的工具是 doxygen,它确实可以帮助您(直观地)查看源树中的关系..但是,如果它设置为不提取静态符号/对象,那么它不会非常彻底。
Usually I tell my compiler to be as annoyingly noisy as possible, that picks 60% of stuff that I need to examine. Unused functions that are months old (after checking with the VCS) usually get ousted, unless their author tells me when they'll actually be used. Stuff missing prototypes is also instantly suspect.
I think trying to implement automated house cleaning is like trying to make a USB device that guarantees that you 'safely' play Russian Roulette.
The hardest part to check are components added to the build system, few people notice those and unused kludges are left to gather moss.
Beyond that, I typically WANT the code, I just want its author to refactor it a bit and make their style the same as the rest of the project.
Another helpful tool is doxygen, which does help you (visually) see relations in the source tree.. however, if its set at not extracting static symbols / objects, its not going to be very thorough.