Contact: Web / Voice / Email / Tips
Simple Thoughts Blog - Java and Web Technologies

Simple solutions for complex problems.

 

How Crappy Is Your Java Source Code?

December 7th, 2007 by Angsuman Chakraborty

How crappy is your Java code? Often we find others code crappier than ours, especially if we are given to maintain their code. Crappy code authors are almost always happy with their code quality. Can we quantify crappiness?

Crap4J claims to be able to quantify crappiness in Java code with a simple formula (applicable to a method):
CRAP(m) = comp(m)^2 * (1 – cov(m)/100)^3 + comp(m)

Where comp(m) is the cyclomatic complexity of method m, and cov(m) is the test code coverage provided by automated tests (e.g. JUnit tests, not manual QA). 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. 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. 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.

Generally speaking, you can lower your CRAP score either by adding automated tests or by refactoring to reduce complexity.

Now you can go to your boss and quantitatively show him that your code is less crappier than your colleague*. After all managers like numbers, even if they have crappy & dubious validity.

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

Note: CRAP stands for Change Risk Anti-Patterns according to their definition.

Crap4J comes as an Eclipse plugin and can also be integrated with Ant. Check the video for a nice demonstration. The authors decided to use 30 as a threshold for crappiness.

I partially agree with the idea but I am not comfortable with the formula. 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.


Tags: Ant, Enterprise Software, Headline News, How To, Humor, Java Software, Open Source Software, Programming | | RSS 2.0 | Trackback this Article | Email this Article

You may also like to read

»How To Change Java Compiler in Ant on Fedora Core / Linux
»How To Connect To CVS Via Proxy Server
»Sun Open Sources Java (April Fool)
»Update On Super Simple Java ORM Replacement in 80 Lines
»How To Install JDK 6 / Java SE 6 (+ Tomcat) in Fedora Core 6 / Fedora 7 in 5 Minutes
»5 FaceBook Source Code Secrets
»Open Source Java Months Away Says Sun Executive
»Open Source J2Se is an absolute must for Java developers; All hail Apache Harmony
»How To Clean Subversion Directories
»Top 10 Java Features (or What Makes Java Great)
»Sun Open Sources Sun Java Studio Enterprise - Enterprise-Class Java EE Development Tool
»Sample Code: Making Robots in Java (JRobot)
»How to run javac 1.5 (or beyond) compiler for JSP compilation in Tomcat 5.5 with generics enabled (and other Java 1.5 only features like autoboxing)
»GCJ is Dangerously Buggy
»How to Develop Rule Based Java Software Using Java Rule Engine API (JSR 94)

3 Responses to “How Crappy Is Your Java Source Code?”

  1. krishna Says:

    OHH NO, Another plugin :)
    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. But it’s a 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).

    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.

    The inspection say’s this :
    This inspection reports any instances of methods that have too high a cyclomatic complexity. Cyclomatic complexity is basically a measurement of the number of branching points in a method. Methods with too high a cyclomatic complexity may be confusing and difficult to test.

    Another reason for me to love it ;)

    BTW, your spam guard kicked me out twice when using Opera. Iam trying this with FF.Let’s see

  2. Angsuman Chakraborty Says:

    > Anyway,I don’t know anybody who wants their code to be called crap.

    I have seen code worse than crap. However as you said nobody would like to call their own code 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. 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.
    2) What’s out there in the market (both free and commercial) that does the same thing(or similar).
    3) If there are any OSS products, why did they choose to create this one instead of contributing to the other.
    3) Why is their product different.

    I think, lot of people , who wasted countless hours evaluating these products can relate to what Iam saying.

    Another metric I apply to OS products is this: If it costed x dollars would I buy it?

Looking forward to hear your thoughts.



Please enter the code shown below ( to verify that you are human ) before you click Submit Comment.

No. 1 method to ethically increase your blog traffic and reach.

Translate

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanPřeložit do Čech/CzechOversætte hen til Dansk/DanishKääntää jotta Finnish/FinnishLefordít -hoz Magyar/HungarianÞýða til Íslenska/IcelandicTraducir a Latinoamericano Español/Latin American Spanishtagapagsalin sa Filipino/FilipinoTłumaczyć wobec Polski/PolishA traduce la spre Român/RomanianPrevesti za Srpski/Serbiantolmačiti v slovenski/SlovenianÖversätta till Svensk/SwedishChyfieitha at Cymraeg/Welshtercüme etmek -e doğru Türk/TurkishPrevesti to Hrvatski/CroatianПревеждам към Българин/BulgarianTraduzca al Español/SpanishTraduisez au Français/FrenchTraduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplifiedترجمة الى العربية/ArabicVertaal aan het Nederlands/DutchΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/Norwegian中文翻译/Chinese TraditionalTraduzir a Língua portuguesa brasileira/Brazilian PortugueseReddo ut Latin/Latin

Taragana Network

»Ctrl-S
»Enterprise Blog
»Free Book on Eye Care by Natural Therapy
»Health Care Blog
»Hot Computer Jobs Blog
»Pet Care & Grooming News and Tips
»Phil Law Blog
»Taragana - Software Outsourcing
»The Angsuman Chakraborty Blog
»The Diabetes Cure Blog
»The Eye Treatment Blog
»The Stem Cell Blog
»Weblog Hosting Blog
"Common sense is the collection of prejudices acquired by age eighteen." - Albert Einstein