1) does anyone have links to some code out there which you consider bad ? C focused please.
2) can you explain what makes them bad in your opinion ? specifics.
I'm looking at this as a learning experience. I didn't know I was writing bad code until someone pointed out why .. maybe someone else can learn 'why' from some examples posted in this thread. I'm not looking for good code at the moment, I may start another 'good' thread later.
My motivation ? I'm told by the 'experts' to seek out examples of good code to read and learn from .. I may have been picking out the wrong things if I don't know that it's bad. By quickly browsing a codebase and getting the gut feel of 'this is good' .. I'll then dedicate some deeper time to see what I can learn from it. Kernel sources and Python codebase come to mind - but I'm always on the lookout for something different.
I don't want to get drowned out in the minutiae of spec's (if someone is really interested they can dig into it -- and maybe debunk it!). Just some quick one-liners/guidelines/rules-of-thumb of your opinion why its subpar. I'll start w/something from recent memory that I've seen in my own codebase:
Code: Select all
float f = 1.2;
if( f == 1.2 ) {
doit();
}
1) no 'f' suffix on the declaration - the compiler will most likely treat it as a double which may penalize you in performance later through silent casting
2) direct comparison to a float (well actually a double in this case since there is no 'f' suffix) - look to using <=, <, >, >= instead.
Architecture flaws also work here, but are harder to explain short-n-sweet. I'm off to look for some real world examples ..