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 . 請輸入代碼如下所示(以驗證您是否人類) ,然後單擊提交評論