views:

1618

answers:

26

Code lines per file, methods per class, cyclomatic complexity and so on. Developers resist and workaround most if not all of them! There is a good Joel article on it (no time to find it now).

What code metric(s) you recommend for use to automatically identify "crappy code"?

What can convince most (you can't convince all of us to some crappy metric! :O) ) of developers that this code is "crap".

Only metrics that can be automatically measured counts!

+11  A: 

Number of warnings the compiler spits out when I do a build.

tloach
Fun, but I assume we want 0 warnings and other compiler messages.
Gamecat
@Gamecat: I've worked with drivers that returned an integer that was actually a pointer to a struct somewhere else in memory. In that case, I'm not sure there is a way to avoid a warning of some sort...
tloach
THIS IS NOT AN ANSWER unless you specify what kind of warnings. Warnings depend on what kind of polices are enabled. This can produce completely different results on different machines without any code change! Please narrow your answer.
Dandikas
@Dandikas, Looks like others dont quite agree with you. I think in a general vague sense this is a valid answer, and it is automatically counted by the compiler.
mattlant
Is it possible to setup automated code check using this answer? ... The answer is equal to saying "Number of bad things in code" WHAT ARE the bad things!!!? Compiler spits what it is configured to spit. This answer does not tell what compiler should spit so it does not answer the question.
Dandikas
@Dandikas: Ask a vague question, get a vague answer. Language specific I could tell you to use -WALL, but that's only gcc. The type of warning is not generally important, if there is a warning then there is probably something risky going on.
tloach
A: 

Code coverage has some value, but otherwise I tend to rely more on code profiling to tell if the code is crappy.

Danimal
+1  A: 

My bet: combination of cyclomatic complexity(CC) and code coverage from automated tests(TC).

CC | TC

 2 | 0%  - good anyway, cyclomatic complexity too small

10 | 70% - good

10 | 50% - could be better

10 | 20% - bad

20 | 85% - good

20 | 70% - could be better

20 | 50% - bad

...

crap4j - possible tool (for java) and concept explanation ... in search for C# friendly tool :(

Dandikas
@Dandikas: I think the Iterative Programming method falls apart due to human factors not due to technical capability. I've seen long running code with CC in the order of 67 without failure, but that it due to meta-programming not human code creation.
_ande_turner_
I agree, but we can't measure human factor, and developers do not accept most of metrics ... I'm in search for something that can be automated, accepted by developers and can provide some warning to current code state.
Dandikas
Seems reasonable. However the CC levels seem fairly moderate for large scale programs I've seen, I'd say they seem ok for individual modules
Robert Gould
+7  A: 

number of global variables.

tloach
I thought they went out together with the dinosaurs...
Treb
We like to imagine they did. In reality, even in something like C# where everything is in classes, I've still seen static public classes used as global variable collections.
Matthew Scharley
Ugh. Don't remind me...
Paul Nathan
+5  A: 
  • Non-existent tests (revealed by code coverage). It's not necessarily an indicator that the code is bad, but it's a big warning sign.

  • Profanity in comments.

Jon Skeet
Are you serious about the profinity thing? I do not see how it correlates to code quality. Maybe to the quality of the working environment...
Treb
Profanity usually means the one programmer is swearing at another - possibly on another project. It may mean that they've had to fix poor code in the same module, or that they've got to workaround bugs elsewhere. Either way it's worth knowing about.
Jon Skeet
Sometimes you have to interoperate with third-party products and technologies, which may lead to profanity in comments too.
Alex B
I've general found that profanity in comments indicates that the coder is rather dismissive of the project and not taking his job seriuosly. e.g. "// fix to handle the other s#!t" usually means he just hacked something together to close a bug report.
James Curran
A: 

Ratio of comments that include profanity to comments that don't.

Higher = better code.

Rich Bradshaw
Funny, but not useful.
DJClayworth
And NOT automated :(
Dandikas
Could be automated: You can measue F***Cs/LOC and S**Ts/LOC (regex is your friend). Should give at least a good approximation of the general profanity. As for the usefulness, well...
Treb
+14  A: 
Tom Ritter
Sorry, I'm really interested in automatic solution ... your is the best I know that is not automated.
Dandikas
You could automate it with a WTF button that the reviewers press during code reviews: or really good speech recognition software.
DJClayworth
+19  A: 

No metrics regarding coding-style are part of such a warning.

For me it is about static analysis of the code, which can truly be 'on' all the time:

I would put coverage test in a second step, as such tests can take time.


Do not forget that "crappy" code are not detected by metrics, but by the combination and evolution (as in "trend) of metrics: see the What is the fascination with code metrics? question.

That means you do not have just to recommend code metrics to "automatically identify "crappy code"", but you also have to recommend the right combination and trend analysis to go along those metrics.


On a sidenote, I do share your frustration ;), and I do not share the point of view of tloach (in the comments of another answers) "Ask a vague question, get a vague answer" he says... your question deserve a specific answer.

VonC
100% correct understanding of a question. Useful +1
Dandikas
+1 For "No metrics regarding coding-style are part of such a warning."This is one of those things where the people who need it most are the ones not doing it.
Gazzonyx
+1  A: 

Number of worthless comments to meaningful comments:

'Set i to 1'
Dim i as Integer = 1
Bob King
I agree - but how would you determine the worthiness automatically?
Treb
automated? Don't think so.
Dandikas
Whoops. Missed "automatically"...
Bob King
I think it can be automated. We parse the program and create a set of possible descriptions of a certain piece of commented code. Then, we just need a good text correlation algorithm and pass the set of possible descriptions and the comment to them and get a measure of usefulness of the comment. :)
Tetha
A: 

Lines of comments / Lines of code

value > 1 -> bad (too many comments)

value < 0.1 -> bad (not enough comments)

Adjust numeric values according to your own experience ;-)

Treb
Depends, if you're using comments to document code (doxygen), or if you automatically insert comments to track changes, then your number of comments could easily be higher than LOC.
tloach
You're right - so the values would need to be adjusted according to the circumstances. And nobody has time for this...
Treb
Furthermore, there are persons who say: most comments in code are a code smell and the code should be refactored to be more readable. Those people would say: a value of 0 is the best ;)
Tetha
Yes, I know, I just don't buy it. I am getting less and less verbose with my comments, but some comments (for example for people who do not know the project yet) are necessary IMHO.
Treb
+5  A: 

Metrics alone do not identify crappy code. However they can identify suspicious code.

There are a lot of metrics for OO software. Some of them can be very useful:

Average method size (both in LOC/Statements or complexity). Large methods can be a sign of bad design. Number of methods overridden by a subclass. A large number indicates bas class design. Specialization index (number of overridden methods * nesting level / total number of methods). High numbers indicate possible problems in the class diagram.

There are a lot more viable metrics, and they can be calculated using tools. This can be a nice help in identifying crappy code.

Gamecat
+2  A: 

I don't believe there is any such metric. With the exception of code that actually doesn't do what it's supposed to (which is a whole extra level of crappiness) 'crappy' code means code that is hard to maintain. That usually means it's hard for the maintainer to understand, which is always to some extent a subjective thing, just like bad writing. Of course there are cases where everyone agrees the writing (or the code) is crappy, but it's very hard to write a metric for it.

Plus everything is relative. Code doing a highly complex function, in minimal memory, optimized for every last cycle of speed, will look very bad compared with a simple function under no restrictions. But it's not crappy - it's just doing what it has to.

DJClayworth
+3  A: 

My personal favourite warning flag: comment free code. Usually means the coder hasn't stopped to think about it; plus it automatically makes it hard to understand, so ups the crappy ratio.

DJClayworth
That really depends on the code, imo. Imagine something like:for(Widget widget : widgets){frobulator.frob(widget);}Does that REALLY need a comment that says "use frobulator to frob each widget in widgets"?
Adam Jaskiewicz
Yeah, but WHY are we frobbing the widgets? Didn't we do that in another module as well? What are the circumstances in which each module should be used?
CindyH
+2  A: 

Unfortunately there is not a metric that I know of. Something to keep in mind is no matter what you choose the programmers will game the system to make their code look good. I have seen that everywhere any kind of "automatic" metric is put into place.

StubbornMule
+11  A: 

Number of commented out lines per line of production code. Generally it indicates a sloppy programmer that doesn't understand version control.

polara
+1 - The programmer is simply doing trial and error.
n002213f
A: 

I take a multi-tiered approach with the first tier being reasonable readability offset only by the complexity of the problem being solved. If it can't pass the readability test I usually consider the code less than good.

ChalkTrauma
+2  A: 

A lot of conversions to and from strings. Generally it's a sign that the developer isn't clear about what's going on and is merely trying random things until something works. For example, I've often seen code like this:

   object num = GetABoxedInt();
//   long myLong = (long) num;   // throws exception
   long myLong = Int64.Parse(num.ToString());

when what they really wanted was:

   long myLong = (long)(int)num;
James Curran
+3  A: 
  • global variables
  • magic numbers
  • code/comment ratio
  • heavy coupling (for example, in C++ you can measure this by looking at class relations or number of cpp/header files that cross-include each other
  • const_cast or other types of casting within the same code-base (not w/ external libs)
  • large portions of code commented-out and left in there
Milan Babuškov
+2  A: 
  • Watch out for ratio of Pattern classes vs. standard classes. A high ratio would indicate Patternitis
  • Check for magic numbers not defined as constants
  • Use a pattern matching utility to detect potentially duplicated code
Andrew from NZSG
Nice idea but how do you identify Pattern classes, unless someone is using the pattern name in the class name. There's also the possibility that they are legitimately mainly applying patterns - I've written stuff where almost all of the classes could be called "pattern classes" because they were all participants in Composite, Visitor or Observer patterns.
Andy Dent
+5  A: 

Developers are always concerned with metrics being used against them and calling "crappy" code is not a good start. This is important because if you are worried about your developers gaming around them then don't use the metrics for anything that is to their advantage/disadvantage.

The way this works best is don't let the metric tell you where the code is crappy but use the metric to determine where you need to look. You look by having a code review and the decision of how to fix the issue is between the developer and the reviewer. I would also error on the side of the developer against the metric. If the code is still popping on the metric but the reviewers think it is good, leave it alone.

But it is important to keep in mind this gaming effect when your metrics start to improve. Great, I now have 100% coverage but are the unit tests any good? The metric tells me I am ok, but I still need to check it out and look at what got us there.

Bottom line, the human trumps the machine.

Flory
"The way this works best is don't let the metric tell you where the code is crappy but use the metric to determine where you need to look." That is exactly the idea. Useful +1
Dandikas
Even more useful, observe how the metric changes over time. That way you're not calling people on a complexity line drawn in the sand but saying "your code is getting **more** complex"
Andy Dent
+1  A: 

Sometimes, you just know it when you see it. For example, this morning I saw:

void mdLicense::SetWindows(bool Option) {
  _windows = (Option ? true: false);
}

I just had to ask myself 'why would anyone ever do this?'.

Marc Bernier
Right up there with if (true) and if (someBool) return true; else return false;
tloach
That's exactly how the Get's were written!
Marc Bernier
+1  A: 

I am surprised no one has mentioned crap4j.

Eric Weilnau
I did!!!!! ... would love to hear any response form someone who actually using it.
Dandikas
I missed the link in your original answer. The current edit makes it clearer.
Eric Weilnau
+2  A: 

At first sight: cargo cult application of code idioms.

As soon as I have a closer look: obvious bugs and misconceptions by the programmer.

bart
A: 

This hilarious blog post on The Code C.R.A.P Metric could be useful.

0A0D
A: 

TODO: comments in production code. Simply shows that the developer does not execute tasks to completion.

n002213f
I hate them because they should be in the issue tracking system. It's OK to make an engineering decision to postpone something but that fact should be out of the code. OTOH you could read the presence of lots of TODO comments in code as a judgement on the quality of the issue tracking system!
Andy Dent
A: 

Methods with 30 arguments. On a web service. That is all.

kprobst