How crappy is your Java code? Come crappy è il vostro codice Java? Often we find others code crappier than ours, especially if we are given to maintain their code. Spesso troviamo altri codice di crappier la nostra, specialmente se ci è dato di mantenere il loro codice. Crappy code authors are almost always happy with their code quality. Crappy codice autori sono quasi sempre felice con il loro codice di qualità. Can we quantify crappiness? Possiamo quantificare crappiness?

Crap4J claims to be able to quantify crappiness in Java code with a simple formula (applicable to a method): afferma di essere in grado di quantificare in Java crappiness codice con una semplice formula (applicabile a un metodo):
CRAP(m) = comp(m)^2 * (1 – cov(m)/100)^3 + comp(m) CRAP (m) = comp (m) ^ 2 * (1 - cov (m) / 100) ^ 3 + obbl (m)

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). Dove comp (m) è la Complessità ciclomatica del metodo m, e cov (m) è il codice di prova forniti da copertura test automatizzati (ad esempio JUnit test, non manuale 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. Complessità ciclomatica è un noto e ampiamente utilizzati metriche ed è calcolato come uno più il numero di decisioni in unico metodo. For code coverage we use basis path coverage. Per il codice di copertura base che usiamo percorso di copertura. 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. Basso CRAP indicare numeri di codice con il cambiamento relativamente basso e la manutenzione di rischio - perchè non è troppo complesso e / o è ben protetto da automatizzato e ripetibile prove. 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 elevato numero di codice che indica il rischio di cambiare a causa di una pericolosa combinazione di alta e bassa complessità, o non, di test automatizzati di copertura.

Generally speaking, you can lower your CRAP score either by adding automated tests or by refactoring to reduce complexity. In linea generale, si può abbassare la CRAP cliente o aggiungendo le prove o automatizzati di refactoring a ridurre la complessità.

Now you can go to your boss and quantitatively show him that your code is less crappier than your colleague*. Ora potete passare al tuo capo e quantitativamente mostra lui che il suo codice è meno di quanto il tuo crappier collega *. After all managers like numbers, even if they have crappy & dubious validity. Dopo tutti i manager come numeri, anche se hanno crappy & dubbia validità.

*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. * Ricordarsi di dimenticare che esiste anche Crap4J, o di sconto è fortemente in qualsiasi menzione, se il tuo codice si rivela essere crappier di quanto il tuo concorrente.

Note: CRAP stands for Change Risk Anti-Patterns according to their definition. Nota: Crap sta per Cambia rischio Anti-pattern in base alla loro definizione.

Crap4J comes as an Eclipse plugin and can also be integrated with Ant. Crap4J arriva come un plug in di Eclipse e può anche essere integrato con Ant. Check the video for a nice demonstration. Controllare il video per una bella dimostrazione. The authors decided to use 30 as a threshold for crappiness. Gli autori hanno deciso di utilizzare 30 come soglia per crappiness.

I partially agree with the idea but I am not comfortable with the formula. I parzialmente d'accordo con l'idea ma non sono proprio agio con la 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. Ignora anche cross-metodo introdotto complessità e la complessità di una classe nel suo complesso che non può essere sempre la somma della complessità dei metodi.


You may also like to read Si può anche leggere come

3 Responses to “How Crappy Is Your Java Source Code?” 3 risposte a "Come Crappy Java è il vostro codice sorgente?"

  1. Commenter krishna Says: Krishna dice:

    OHH NO, Another plugin OHH NO, Un altro plug in :)
    Don’t we already have plugins that do that ? Non abbiamo già plugin che fare?
    Also, It seem’s to check for two things only (complexity and coverage) Inoltre, sembra's a verificare la presenza di solo due cose (la complessità e la copertura)

    Anyway,I don’t know anybody who wants their code to be called crap. Comunque, non so che nessuno vuole che il loro codice di essere chiamato crap. But it’sa nice way to get our attention for their plugin. Ma è un buon modo per ottenere la nostra attenzione per il loro plug-in.

    I use IntelliJ IDE and most of what I do is web developement (JSP,javascript and form handlers). Io uso IntelliJ IDE e la maggior parte di quello che faccio è sviluppo web (JSP, javascript e gestori di forma).

    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). La mia esperienza mi dice che la spesa un po 'di tempo per l'inizio a pensare attraverso il problema andrà un lungo cammino per iscritto la qualità del codice e non è necessario in quanto il mio test di casi (in particolare quelle banali).

    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. Tornando al IntelliJ, l'IDE ha molti controlli di default che può girare su il vostro codice e sono tornato a verificare la presenza e l'ha trovata.

    The inspection say’s this : L'ispezione dire è questo:
    This inspection reports any instances of methods that have too high a cyclomatic complexity. Questo i rapporti di ispezione eventuali casi di metodi che sono troppo alto un Complessità ciclomatica. Cyclomatic complexity is basically a measurement of the number of branching points in a method. Complessità ciclomatica è fondamentalmente una misura del numero di punti di ramificazione in un metodo. Methods with too high a cyclomatic complexity may be confusing and difficult to test. Con metodi troppo alto un Complessità ciclomatica può essere confusa e di difficile prova.

    Another reason for me to love it Un altro motivo per me di amarla ;)

    BTW, your spam guard kicked me out twice when using Opera. BTW, il tuo spam guardia calci me out due volte quando si utilizza Opera. Iam trying this with FF.Let’s see Ora di provare questa con FF.Let 's vedere

  2. Commenter Angsuman Chakraborty Angsuman Chakraborty Says: Dice:

    > Anyway,I don’t know anybody who wants their code to be called crap. > In ogni caso, non so che nessuno vuole che il loro codice di essere chiamato crap.

    I have seen code worse than crap. Ho visto codice peggio di crap. However as you said nobody would like to Tuttavia, come lei ha detto che nessuno vorrebbe call their own code crap chiamano il loro proprio codice 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). > La mia esperienza mi dice che la spesa un po 'di tempo per l'inizio a pensare attraverso il problema andrà un lungo cammino per iscritto la qualità del codice e non è necessario in quanto il mio test di casi (in particolare quelle banali).

    While it may be true for the experienced programmers, even they can benefit from good unit testing. Anche se può essere vero per i programmatori esperti, anche di poter beneficiare di una buona unità di test. As for the rest unit tests go a long way in creating reusable quality code. Per il resto unità di test fare un lungo cammino nella creazione di codice riutilizzabili qualità.

    I have sent you an email wrt. Ho inviato una e-mail WRT. your problem with our comment protection. il tuo problema con il nostro commento di protezione.

  3. Commenter krishna Says: Krishna dice:

    In my first comment I said ‘Not another plugin’. Nel mio primo commento ho detto 'Non un altro plug'. I did that because often times I question the motives behind what some of the OSS guys are upto. Che ho fatto perché spesso mi chiedo le motivazioni dietro ciò che alcuni dei ragazzi sono OSS fino.

    My suggestion for anyone coming up with an open source product is to provide justification for it by 1) Telling what they are doing. Il mio suggerimento per chiunque venire con un prodotto open source è quello di fornire una giustificazione per essa di 1) che raccontano ciò che stanno facendo.
    2) What’s out there in the market (both free and commercial) that does the same thing(or similar). 2) Che cosa c'è là fuori sul mercato (sia libero e commerciale) che fa la stessa cosa (o simili).
    3) If there are any OSS products, why did they choose to create this one instead of contributing to the other. 3) Se ci sono prodotti OSS, il motivo per cui hanno scelto di creare questo uno invece di contribuire agli altri.
    3) Why is their product different. 3) Perché è il loro prodotto diverso.

    I think, lot of people , who wasted countless hours evaluating these products can relate to what Iam saying. Credo che, molte persone, che spreco di ore e ore a valutare questi prodotti possono riferirsi a ciò che dice ora.

    Another metric I apply to OS products is this: If it costed x dollars would I buy it? Ho un altro parametro si applicano ai prodotti OS è questo: Se x costi di dollari sarebbe acquistarlo?

Looking forward to hear your thoughts. In attesa di sentire i vostri pensieri.



Please enter the code shown below ( to verify that you are human ) before you click Submit Comment . Inserisci il codice qui sotto (per verificare se è umano) prima di fare clic su Invia commento.