Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you add information to the top of each .hpp/.cpp file? [closed]

When creating a new C++ header/source file, what information do you add to the top? For example, do you add the date, your name, a description of the file, etc.? Do you use a structured format for this information?

e.g.

// Foo.cpp - Implementation of the Foo class
// Date: 2008-25-11
// Created by: John Smith

One team I know embeds CVS commit messages to the foot of each file, but I'm not sure I want to go this far...

like image 591
Rob Avatar asked Nov 25 '08 20:11

Rob


People also ask

What should I put in my hpp vs cpp?

h is used as header, which means that you should only declare all functions and classes in it, so the compiler can link them correctly in case you want to access the document from your main program file: // print. hpp void print(int); . cpp files now define the functions and classes you previously declared in the .

Does every cpp file need a header?

Cpp files don't always have to have a header file associated with it but it usually does as the header file acts like a bridge between cpp files so each cpp file can use code from another cpp file. One thing that should be strongly enforced is the no use of code within a header file!

Should I include in header or cpp?

In general, you should only include headers in . h files that are needed by those headers. In other words, if types are used in a header and declared elsewhere, those headers should be included. Otherwise, always include headers only in .

What is the difference between cpp and hpp files?

cpp files have distinct purposes. . hpp files contain declarations, while . cpp files contain implementation of what is declared in the . hpp file.


10 Answers

Information about who created a file and when and who edited it and when is all in source control. If your team has good practices around check-in comments, then you'll know the reasons for each change too. No need for comments for that stuff.

I think it's 100% legit - wise, even - to put a comment block, as long as is necessary, explaining the purpose of the class/module. When the next person goes to change it, they'll have a better idea of the overall vision and whether this file is the appropriate place for their change.

Some shops put copyright notices and other legal folderol in source file comments. This strikes me as just silly - if your (non-OSS) source code has made it onto someone else's servers without your knowledge or permission, a copyright notice is probably not going to deter them from doing anything with it. IANAL, YMMV.

like image 153
bradheintz Avatar answered Sep 29 '22 09:09

bradheintz


Don't. Most of the stuff can be retrieved from the versioning system when needed so it's redundant to add. That would leave you with the description of the content of the file but that's part of the class documentation most of the time (or at least the documentation of the specific type).

I don't do any of those, but then again, I don't like the cruft.

like image 33
Jasper Bekkers Avatar answered Sep 29 '22 10:09

Jasper Bekkers


We're required to put copyright information at the top of each file. I think dates, authors, and the name of the file is a waste of time.

We also have our source control system append check-in comments at the bottom of each file. I initially hated the change log, but over time I learned to like it. It really helps when merging changes.

like image 30
Aardvark Avatar answered Sep 29 '22 11:09

Aardvark


I include the file name, a brief description of the file's purpose, and a $Id$ tag for CVS or Subversion purposes. File creator and date of creation can be found by checking the repository, so it's not needed.

File name is included because depending on what you're using to edit the file, that might not be entirely apparent when you're editing it. The description can be used to determine if a bit of code belongs in the file, or if it should be moved to another. And of course, $Id$ gives you last change time, and last editor.

Embedding check-in messages is only useful when the message is useful, and only if the file is updated once and a while. Including every message will simply bloat the file to the point where there's more comments describing changes than there is actual code. Best to leave that to the repository as well; often it's only a short command line to get the file's check-in log.

If you're stuck with a revision control system that can't keep history for moves and copies, in that case just reference the original file and its version number. Of course, if you're using a system that was created sometime in this century and not the last, that shouldn't be an issue.

like image 44
Chris Charabaruk Avatar answered Sep 29 '22 10:09

Chris Charabaruk


I used to like putting version control keywords in the header of file. But recovered from that affliction. :) For two reasons:

  1. Nobody put comments in that are of any use. You always wind up looking at the diffs reported by the version control system.
  2. It creates a nightmare in trying to diff large filesets because the keywords create differences that are the only difference in the file.
like image 31
Steve Fallows Avatar answered Sep 29 '22 10:09

Steve Fallows


Originally answered here, but since deleted: 134249

I would only put two things:

  • licensing/copyright information
  • comments required by documentation-generating tools (i.e., the comments have to be in the header to work - otherwise, they should go in the definition files)

Anything else is unnecessary fluff that won't be maintained, and will eventually become worse than nothing at all.

At the time I worked for a large defense company, and we had draconian coding standards. If you followed them to the letter (and most people don't), most of your headers would be composed mostly of that meaningless fluff. Doubly worse is that the exact same fluff is required to be placed in source files as well, which means two copies of the fluff gets out of date and becomes misleading.

like image 35
Ben Collins Avatar answered Sep 29 '22 10:09

Ben Collins


I don't embed the date because it's redundant. If someone wants to know the date a file was created don't trust the author, trust your source control system. It should be the defintive answer for the date of creation.

I'm definately not against embedding check in messages though. Those are pretty useful.

like image 26
JaredPar Avatar answered Sep 29 '22 09:09

JaredPar


We use our RCS to automatically stamp the following on the file:

Copyright,

RCS file name,

Date modified,

Author of last change,

RCS revision number

I think this is very convenient. I really like having the file name automatically populated in each file, because it makes searching the solution for files very quick.

like image 25
Marcin Avatar answered Sep 29 '22 11:09

Marcin


This is what i normaly put at the top of files:

///////////// Copyright © 2008 DesuraNET. All rights reserved. /////////////
//
//   Project     : [project name]
//   File        : [file name]
//   Description :
//      [TODO: Write the purpose of ... ]
//
//   Created On: 11/12/2008 2:24:07 PM
//   Created By: [name] <mailto:[email]>
////////////////////////////////////////////////////////////////////////////

and i set up a macro in vis to do add it in and fill in default info when i make a new file

like image 1
Lodle Avatar answered Sep 29 '22 10:09

Lodle


If you're using CVS, check out it's keyword substitutions. They will help automate embedding that information.

Personally I stick this at the top of all of my source files:

// $Id$

Other informative comments I embed to be parsed with doxygen, if they relate to something specific (the file, the class, a type, etc).

like image 1
luke Avatar answered Sep 29 '22 09:09

luke