So, we saw the post on Code Smells and how to fix them...but what about for us NON object oriented folk? I use embedded C a LOT and while all the responses to the said post were great, I wondered if anyone had some input about the straight-C world? Some of the things mentioned in the other post do not apply (and some were even completely opposite) to my world... What do you guys think?
High Cyclomatic Complexity still applies. A function with a high cyclomatic complexity is a sign that the code is too complex and likely to have bugs and that bug fixes are likely to have bugs!
http://en.wikipedia.org/wiki/Cyclomatic_complexity
Edit: Source Monitor is an excellent tool for gathering Cyclomatic Complexity. http://www.campwoodsw.com/sourcemonitor.html
goto
using non secure versions of functions
code files with over 2000 lines of code
How about Tail Call Optimization:
int bar(int a)
{
printf("bar called with arg %d\n", a);
return a * a;
}
int foo(int b)
{
return bar(b * b);
}
Deeply nested control structures (ifs inside switches inside loops and so on) usually mean it's time for a good refactoring session.
functions that take a gazillion of arguments always smell..
large functions (with more than 200 lines of code, or more than one screenful, pick something)
function calls using post/prefix increment - foo(i++)/foo(++i).
This can lead to undefined behavior - foo(i++, i)
unhelpful variable names
i,j,k as loop variables are ok, but what does this do:
tt = (a * st) * ct; t = a + tt;
The following should be clear to everyone:
total_tax_amout = (amount * state_tax_rate) * county_tax_rate; total = amount + total_tax_amount;
It's almost self-documenting code! :)
Here's a really bad smell: defining macros to replace the basic syntax of the language, like
#define BEGIN {
#define END }
Don't laugh! I had a programmer at my first job who was very proud of having made his C code look like Modula-2.