How crappy is your Java code?如何crappy是您的Java代码? Often we find others code crappier than ours, especially if we are given to maintain their code.我们常常觉得别人的代码crappier比我们,尤其是如果我们考虑到,以维持其代码。 Crappy code authors are almost always happy with their code quality. crappy代码的作者几乎总是乐意与他们的代码质量。 Can we quantify crappiness?我们可以量化crappiness ?

Crap4J crap4j claims to be able to quantify crappiness in Java code with a simple formula (applicable to a method):声称要能够量化crappiness在Java代码与一个简单的公式(适用于一种方法) :
CRAP(m) = comp(m)^2 * (1 – cov(m)/100)^3 + comp(m) crap (米) =可比(米) ^ 2 * ( 1 -冠状病毒(米) / 1 00) ^ 3 +可比(米)

Where comp(m) is the cyclomatic complexity of method m, and cov(m) is the test code coverage provided by automated tests (eg JUnit tests, not manual QA).那里可比(米)是cyclomatic的复杂性方法米,和冠状病毒( M )是测试代码覆盖所提供的自动化测试(如junit测试,没有质量保证手册) 。 Cyclomatic complexity is a well-known and widely used metric and it’s calculated as one plus the number of unique decisions in the method. cyclomatic的复杂性是众所周知和广泛使用的度量和它的计算公式为一,再加上许多独特的决定方法。 For code coverage we use basis path coverage.为代码覆盖,我们使用的基础上路径覆盖。 Low CRAP numbers indicate code with relatively low change and maintenance risk – because it’s not too complex and/or it’s well-protected by automated and repeatable tests.低crap数字表示代码与相对较低的改变和维修的风险-因为这不是太复杂,和/或它的良好的保护,自动化和可重复的测试。 High CRAP numbers indicate code that’s risky to change because of a hazardous combination of high complexity and low, or no, automated test coverage.高crap数字表示的代码的风险变化的影响,因为一个危险的组合高的复杂性和低,或根本没有,自动测试范围。

Generally speaking, you can lower your CRAP score either by adding automated tests or by refactoring to reduce complexity.一般来说,您可以降低您的crap评分要么加入自动化测试或由重构,以减少复杂性。

Now you can go to your boss and quantitatively show him that your code is less crappier than your colleague*.现在,您可以去你的老板和定量显示他说,您的代码是少crappier比你的同事*. After all managers like numbers, even if they have crappy & dubious validity.之后,所有管理人员一样,号码,即使他们crappy &不三不四的有效性。

*Remember to forget that Crap4J even exists, or discount it heavily at any mention, if your code turns out to be crappier than your competitor. *记得要忘记, crap4j ,甚至存在,或折扣,它重于任何提及,如果您的代码,原来要crappier比你的竞争对手。

Note: CRAP stands for Change Risk Anti-Patterns according to their definition.注: crap主张改变风险的反模式,根据他们的定义。

Crap4J comes as an Eclipse plugin and can also be integrated with Ant. crap4j来作为Eclipse的插件,也可以结合蚂蚁。 Check the video for a nice demonstration.检查视频一个很好的示范。 The authors decided to use 30 as a threshold for crappiness.作者决定使用30作为一个门槛, crappiness 。

I partially agree with the idea but I am not comfortable with the formula. i部分同意的想法,但我不舒服的公式。 Also it ignores cross-method complexity introduced and complexity of a class as a whole which may not always be the sum of the complexity of the methods.也忽略了两岸关系的复杂性的方法介绍和复杂的一类作为一个整体,这可能并不总是总和的复杂性的方法。


You may also like to read您也可以想读

3 Responses to “How Crappy Is Your Java Source Code?” 3回应“如何crappy是您的Java源代码” ?

  1. 评论 krishna Says: 克利须那说:

    OHH NO, Another plugin ohh没有,另一个插件 : )
    Don’t we already have plugins that do that ?不,我们已经有插件认为这样做呢?
    Also, It seem’s to check for two things only (complexity and coverage)此外,它似乎的检查,只有两件事(的复杂性和范围)

    Anyway,I don’t know anybody who wants their code to be called crap.无论如何,我不知道任何人谁希望其代码被称为crap 。 But it’sa nice way to get our attention for their plugin.不过,这是很好的方式,以获得我们的注意,为他们的插件。

    I use IntelliJ IDE and most of what I do is web developement (JSP,javascript and form handlers).我用intellij IDE和大部分我做的是网络开发( JSP技术, JavaScript和表格处理) 。

    My experience tells me that spending a little time in the beginning to think through the problem will go a long way in writing quality code and you don’t need as my test cases(especially trivial ones).我的经验告诉我,花了一点时间在开始时认为,通过的问题,将还有很长的路要走,在写作质量的代码和您不需要为我的测试案例(尤其是微不足道的) 。

    Coming back to IntelliJ , the IDE has many inspections by default that it can run on your code and I went back to check for this and found it.未来回到intellij , IDE中有许多检查默认情况下,它可以运行在您的代码和我回到检查此发现它。

    The inspection say’s this :检查说的:
    This inspection reports any instances of methods that have too high a cyclomatic complexity.这个检验报告,任何实例的方法有太高的cyclomatic的复杂性。 Cyclomatic complexity is basically a measurement of the number of branching points in a method. cyclomatic复杂,基本上是一个测量的数目在分支点的一种方法。 Methods with too high a cyclomatic complexity may be confusing and difficult to test.方法与过高cyclomatic的复杂性可能会混乱和困难的考验。

    Another reason for me to love it另一个原因,我爱它 ; )

    BTW, your spam guard kicked me out twice when using Opera.的BTW ,您的垃圾邮件防范踢我两次,当使用Opera 。 Iam trying this with FF.Let’s see IAM的尝试,这与ff.let奇摩见

  2. 评论 Angsuman Chakraborty 日由Angsuman Chakraborty Says:说:

    > Anyway,I don’t know anybody who wants their code to be called crap. >无论如何,我不知道任何人谁希望其代码被称为crap 。

    I have seen code worse than crap.我所看到的代码不如crap 。 However as you said nobody would like to但正如你所说,没有人想 call their own code crap呼吁他们自己的代码crap .

    > My experience tells me that spending a little time in the beginning to think through the problem will go a long way in writing quality code and you don’t need as my test cases(especially trivial ones). >我的经验告诉我,花了一点时间在开始时认为,通过的问题,将还有很长的路要走,在写作质量的代码和您不需要为我的测试案例(尤其是微不足道的) 。

    While it may be true for the experienced programmers, even they can benefit from good unit testing.虽然它可能是事实,为经验丰富的程序员,即使他们可受惠于良好的单元测试。 As for the rest unit tests go a long way in creating reusable quality code.至于其余的单元测试还有很长的路要走,在建立质量可重复使用的代码。

    I have sent you an email wrt.我有您发送了一封电子邮件wrt 。 your problem with our comment protection.您的问题,与我们的评论的保障。

  3. 评论 krishna Says: 克利须那说:

    In my first comment I said ‘Not another plugin’.在我的第一点评论,我说'不是另一个插件' 。 I did that because often times I question the motives behind what some of the OSS guys are upto.我本来认为,由于很多时候,我怀疑背后的动机是什么部分的开放源码软件的家伙是最多。

    My suggestion for anyone coming up with an open source product is to provide justification for it by 1) Telling what they are doing.我的建议,任何人来与开放源代码产品是提供理由,它由1 )告诉他们在做什么。
    2) What’s out there in the market (both free and commercial) that does the same thing(or similar). 2 )什么的,有在市场上(包括免费和商业) ,但这同样的事情(或类似) 。
    3) If there are any OSS products, why did they choose to create this one instead of contributing to the other. 3 )如果有任何开放源码软件产品,他们为什么选择创建此一贡献,而不是向其他。
    3) Why is their product different. 3 )为什么他们的产品不同。

    I think, lot of people , who wasted countless hours evaluating these products can relate to what Iam saying.我认为,很多人,谁浪费了无数的时间评估这些产品可以涉及到什么IAM的说。

    Another metric I apply to OS products is this: If it costed x dollars would I buy it?另一个指标申请,以操作系统产品,是这样的:如果它的费用x美元,我会买它呢?

Looking forward to hear your thoughts.期待着听到您的想法。



Please enter the code shown below ( to verify that you are human ) before you click Submit Comment . 请输入代码如下所示(以验证您是否人类) ,然后单击提交评论