views:

249

answers:

9

Hi!

I'm interested in hearing what routines you have for cleaning up public header files you distribute to customers.

Some things I'd like to hear your opinions on are:

Comments not meant for external consumption. Generally I like keeping documentation close to the code and comments like this might not be a good idea to share:

/**
 * @todo Should we change the signature of this function to
 * make it obvious that xxx is really yyy?
 */

or perhaps:

/**
 * @todo Add support for feature X
 */

Inconsistent tab styles:

void functionA(int a, 
     int b,
     int c,
     int d);

void functionB(int a,
               int b,
               int c);

Are there any tools for preparing headers or code in general for release?

+14  A: 

You should ALWAYS, on any project involving multiple developers for any extended period of time and the subsequent release of that source code, SCAN FOR OBSCENITIES (and other things you shouldn't have said, e.g., "My boss made me do this", "This code is terrible", etc). Also, spell-checking the comments can be helpful, as people incorrectly spelling words saps from your credibility.

GWLlosa
+1 for fixing spelling and obscenities. I'm sure I'm not allowing in (sub)consciously assuming people who can't spell can't be trusted to write solid code. If you're not great at spelling, get someone else who is to look things over.
Dominic Rodger
Look over ALL comments to make sure they look professional. Do not rely on any sort of automated scan. You don't want your customers reading things like "My boss made me do it this way" or "I hope this works" any more than obscenities.
David Thornley
Good point. Obscenities are the big red flag, but there are other things that can be equally bad. Editing answer to reflect that.
GWLlosa
+1 makes code look very unprofessional otherwise
Dan
Well, I am not sure I would trust code written by people who are most concerned about obscenities in comments than about clear and well-written comments. -1And why "My boss made me do it this way" is obscene?
bortzmeyer
"My boss made me do this" would be an example of "other things you shouldn't have said", hence its inclusion in the parenthetical phrasing.
GWLlosa
bortzmeyer: I expect clear code and comments too, but obscene or comments saying things about your boss or coworker make the code look unprofessional. If I see a comment like "my boss made me do this" in code, then I don't trust that code because a clash between the boss and coder is never good. If I see obscene comments, I don't feel like a qualified professional wrote the code and I don't trust it. etc etc
Dan
+11  A: 

Please make sure that your headers don't generate any compiler warnings.

Paul Mitchell
+2  A: 

It'd generally be better if you had coding standards/formats for documents customers will see that the developers themselves follow when they first create the code, so you're not spending time cleaning up code before release, such as now.

Also, Visual Studio and several other IDE's have an "Auto Formatting" option where you can set up a style and it is applied to your code (tabs, spaces, that sort of thing). I think that's mostly what you're asking for here.

samoz
Yes, but NEVER rely on automated tools when you're going to release source code.
David Thornley
+1  A: 

Have a look at GNU Indent, it can automatically reformat C header files.

Miroslav Bajtoš
+2  A: 

I'm not very familiar with the subject, but for open source projects you often have the licence and copyright statement at the top of the header. This can avoid several juridic issues.

Luc Touraille
In any case, put the copyright statement there.
David Thornley
+1  A: 

G'day,

In C++ I like to use the Handle-Body idiom to decouple the implementation as much as possible from the public interface.

You should also make sure that any boilerplate, e.g. copyright notices, is consistent and up to date, e.g. copyright doesn't expire in 2008 for code released today.

Be consistent across all public header files for naming conventions, formatting, layout and class design otherwise it leaves an unprofesional impression on customers.

Make sure that there are no "using" declarations in your header files. Misuse of "using" dec's can seriously screw things up with inadvertent side effects.

As mentioned previously, make sure that your headers don't generate any warnings.

Finally. make sure you've got some good API documentaion to go with your header files.

Don't be like a company who provides a well known postcode lookup product. First version of the C API came with minimal documentation that was heavily based on the Windows GUI version. The header files simply consisted of functions whose parameters only had types and no names. And no comments at all.

Only way to work out what the functions actually did was to reverse engineer a simple lookup example program provided and reverse engineer it.

Still, after managing to do that I saved BBC's Children in Need tens of thousands of pounds per year because the addresses provided for fund-raising packs were more likely to be correct than in previous years!

HTH

cheers, Rob

Rob Wells
+2  A: 

In my experience having an internally used header routinely and automatically cleaned up for public consumption is a hard task, and definitely error prone. Eventually the inconsistent format or the inappropriate comment will inevitable creep through.

In many cases you are probably better off wrapping everything into a small and clean interface, whose header is always maintained as clean and as commented as possible; modifications to that file should undergo, for instance, a particularly careful review process.

UncleZeiv
+1  A: 

Just as important as removing crufty comments is adding necessary ones. Things you might need to add:

  • copyright/terms-of-use header
  • contact information for support
  • links to documentation if it is being made available online
  • documentation of public interfaces (return values, parameters, pre- and post-conditions, etc)
  • warnings on functions/methods that are exposed but not intended for production use

As far as formatting tools go, check out the recent stackoverflow discussion about Best C++ Code Formatter/Beautifer, wherein AStyle is currently the top-ranked.

Ken Dyck
+3  A: 

Always have somebody (preferably more than one) go through the header to look for anything that looks unprofessional. You can use code formatters and other automatic tools first.

For comments, have them look for anything unprofessional or tentative. Correct misspellings. Make sure they're accurate. Have a standard way to format them, and stick to it.

Check all identifier names. They should conform to a style guide and be professionally named.

Make sure all necessary comments are there. This includes copyright and contact information at the top. Come up with a standard method of documenting classes and such, and enforce it.

Basically, from my point of view, you want your headers to look like they were produced by drones without creativity or a sense of humor, but who are perfectly consistent (sort of like CPA stereotypes). (It's sort of like asking your developers to wear suits while customers are visiting the office - the customers will be happier if they don't see what your developers are really like.)

David Thornley