At one point or another, every developer has looked at a piece of code and
said aloud (or to themselves), “This is crap.” Until now, however, the definition of
what actually constitutes bad code has not been an exact science with a
proper formula or analytics.
Thanks to the efforts of Alberto Savoia, founder and CTO of Agitar Software,
there is now a statistical measure to monitor Java code. Called crap4j, CRAP is an acronym for Change Risk Analyzer and Predictor.
Savoia said that code complexity and a lack of testing lead to code that is difficult to maintain and enhance. Actually determining how inefficiently complex a piece of code might be, on the other hand, is what the CRAP score is all about.
“Even though the name is humorous, CRAP is meant for a very serious purpose,” Savoia told InternetNews.com. “There is no standardization for code
quality. Our goal is to come up with a metric for code quality that people
would use.”
The basis of how crap4j determines inefficient complexity is by measuring
how much complexity there is in the Java methods included in a particular
piece of code. Too much complexity in the form of too many
branches in Java code methods is not a good thing, Savoia said.
The CRAP formula also tries to determine whether there are automated tests for the code, since untested code is also an indicator of CRAP. “The more branches, the more pieces of code [then] the harder it is to maintain,” Savoia said.
The general idea is that the higher the CRAP score, the greater the effort a developer will need to deal with in order to maintain the code. That said, the CRAP score is not necessarily an indicator of code security.
“The reality is that no metric is infallible,” Savoia said. “Having a
low CRAP score doesn’t mean you don’t have bugs; but having a high CRAP score
means you’ve got a sloppy development. A high CRAP score likely indicates
you’ve got problems in more than one area.”
Though the crap4j effort has been spearheaded by Savoia and his company
Agitar, he expects no direct financial gain from the effort and intends
on running the project as an open source endeavor. The crap4j tool itself is
an Eclipse plug-in, and Savoia noted that the plan is to move the source code
over to the Eclipse Foundation, of which Agitar is a member.
“It will be more fun if others pick CRAP up,” Savoia said. “The metric
should not be controlled.”
For Savoia, the CRAP effort is all part of his own crusade to
improve code quality for all.
“Software is such a black-box thing, and for the industry to be open about
the degree of testing — and hopefully by trying not to have their code labeled
as ‘crappy’ — we can improve software.”