10-29-2007 10:58 AM
10-29-2007 11:47 AM
So do I.
We wind up having several senior programmers spend days tracking down a mistake that it took some amateur C hack 5 minutes to make. Then the program manager complains about the software cost.
Mishandling pointers is a favorite, but in general the capacity of systems engineers, EE's, and techs to write unbelievably bad code would blow your mind. Most of the amateur hacks are literally too stupid to be embarassed at the junk they write. Many of them also seem to be convinced that writing C code just can't be that hard - pure arrogance.
Menchar
10-29-2007 12:50 PM - edited 10-29-2007 12:50 PM
Message Edited by DanielGreen on 10-29-2007 12:50 PM
10-29-2007 01:14 PM
I'm at raytheon.
Yup, globals are bad - turns out that's easily said but profoundly true. I was going to try and multi-thread a large goofed up app until I looked at global usage - I'll grow old and die before I could work through it all.
I get shouted down, sometimes literally, and laughed at for making suggestions like:
1. Minimize pointer use. Don't de-reference a NULL pointer. Don't use pointers to void as a polytype to defeat the type system, such as it is.
2. One declaration per line, in alphabetical order, using meaningful names.
3. Some form of hungarian notation so variable type is known without hunting all over for the declaration.
4. No "magic numbers".
5. Use the registry, not .ini files
6. Use the
if ((iStatus = function ()) != NO_ERROR) {
// error handling here
}
idiom.
7. Use a consistent brace style, normally the "one true bracket style."
8. Annotate - good code's written once and read many times.
9. Limit nesting depth, function length, number of parameters, module size.
10. Don't code memory leaks.
11. Don't recode stuff that's in the standard library.
Among others.
I use the terms "too stupid to be embarrased" and "conspiracy of fools" quite a bit.
Menchar
10-29-2007 01:23 PM
1. Minimize pointer use. Don't de-reference a NULL pointer. Don't use pointers to void as a polytype to defeat the type system, such as it is.
This is a no brainer, pointers are FINE if they're used as constant pointers from accessors in a modular API that nobody but a really godo idiot can break.
2. One declaration per line, in alphabetical order, using meaningful names.
This is style, but very GOOD style
3. Some form of hungarian notation so variable type is known without hunting all over for the declaration.
For CVI this is a good idea even if it's just throwing val str or ptr onto the variable name somewhere to get an IDEA of what we're working with, in other IDE's it's not such a big deal, in VS, I just mouse over the identifier.
4. No "magic numbers".
unless it's like [BUFFERLEN-1] or other relatively idiot proof ones, even then I tend to comment...
5. Use the registry, not .ini files
This depends on the implementation and distribution method, we use ini's alot because we run alot of our software off remote drives, however I do tend to agree.
6. Use the
if ((iStatus = function ()) != NO_ERROR) {
// error handling here
}
Eww... first row open brackets, bad readability and brackettracing.
7. Use a consistent brace style, normally the "one true bracket style."
Style my friend, it's all good so long as it's condusive to good development and is consistant.
8. Annotate - good code's written once and read many times.
Just dont' say "this is an integer"
9. Limit nesting depth, function length, number of parameters, module size.
functions should do one thing and do it well, if that one thing encompasses other smaller related things, they should each be functions, lather rinse repeat.
10. Don't code memory leaks.
Tell that to Microsoft.
11. Don't recode stuff that's in the standard library.
Unless it's academic in nature.
10-29-2007 03:30 PM
10-29-2007 03:35 PM
10-29-2007 04:21 PM
My comment on the idiom was to capture an error return and deal with it, instead of ignoring returned status as so many of the clowns around here do. tens of thousands of lines of code and it all falls apart whenever there's an error - no way to localize it without going in and hacking it. The bracket style isn't important.
You have to do it with "style" in C 'cause there's no way for the compiler/linker to enforce good practice. Yup, Ada is strictly typed, too bad no one much wants to use it. Some say they ruined the language with Ada 95.
structs are about the best C offers for encapsulation / "data hiding".
CVI toolbox has List type, I use the heck out of this. A collection-like abstraction in C. Lists of structs work pretty well sometimes.
Plus I think NI gives you the sources to List. Now if they only had a map 🙂 Could likely make one from the List data type, could also make queue, set, I think.
Try running this metric tool over some of your C code www.campwoodsw.com.
Some of the code I have to deal with goes off the chart with this tool - I was going to send some of it to the author of the tool to see if he had ever seen metrics as bad.
I may post some of it up here just for laughs. We could have a "bad CVI C code" contest. Then realize that someone actually got paid to write it 🙂
Menchar
10-29-2007 04:48 PM
10-29-2007 05:41 PM