473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Documenting C Code

What sort of documentation do you do on your code? I've never done any code
development on a big project and I want to start documenting my code in a way
that makes sense, is easy to understand, informative, etc.

How would you document:
*.c file headers
*.h file headers
Functions
Variables?

Jeff

Maybe a little OT, but I want a C specific viewpoint. Also, are there tools
similar to JavaDoc for C?

Nov 13 '05 #1
13 7060
On Mon, 17 Nov 2003 20:34:52 -0700, Jeff Rodriguez
<ne********@gurugeek.EXAMPLENOSPAM.com> wrote in comp.lang.c:
What sort of documentation do you do on your code? I've never done any code
development on a big project and I want to start documenting my code in a way
that makes sense, is easy to understand, informative, etc.

How would you document:
*.c file headers
*.h file headers
Functions
Variables?

Jeff

Maybe a little OT, but I want a C specific viewpoint. Also, are there tools
similar to JavaDoc for C?


www.doxygen.org

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2
Jeff Rodriguez wrote:
What sort of documentation do you do on your code? I've never done any
code development on a big project and I want to start documenting my code
in a way that makes sense, is easy to understand, informative, etc.

How would you document:
*.c file headers
*.h file headers
Functions
Variables?

Jeff

Maybe a little OT, but I want a C specific viewpoint. Also, are there
tools similar to JavaDoc for C?


Doxygen makes a great starting point for documenting C code, but it's not
the be all and end all. What you really need to document is not so much the
data types and functions themselves (although that is important, and
Doxygen can certainly help you with that) but the relationships between
them *and how to use those relationships*. This is key; this is critical;
and this is where most documentation fails.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #3
In article <Zqgub.19692$Ro5.18776@fed1read07>,
Jeff Rodriguez <ne********@gurugeek.EXAMPLENOSPAM.com> wrote:
What sort of documentation do you do on your code? I've never done any code
development on a big project and I want to start documenting my code in a way
that makes sense, is easy to understand, informative, etc.

How would you document:
*.c file headers
*.h file headers
Functions
Variables?

Jeff

Maybe a little OT, but I want a C specific viewpoint. Also, are there tools
similar to JavaDoc for C?


For every extern function, write a comment describing what the function
does. The comment must be precise enough so that if you threw away the
source code of the function, I could rewrite it using the comments
alone. Then unless it is obvious, add a description why I would want to
call that function.
Nov 13 '05 #4
Jeff Rodriguez <ne********@gurugeek.EXAMPLENOSPAM.com> wrote in message news:<Zqgub.19692$Ro5.18776@fed1read07>...
What sort of documentation do you do on your code? I've never done any code
development on a big project and I want to start documenting my code in a way
that makes sense, is easy to understand, informative, etc.

How would you document:
*.c file headers
*.h file headers
Functions
Variables?

Here are my general rules:

1. Don't document the obvious. We know how ++, --, and other
operators work.
2. Keep comments high-level, describing the general intent of
the code. Don't just re-express the algorithm in English; tell
us *why* you're doing something.

Right: For each name in the array, extract the surname.

Wrong: Set i to 0. Loop from 0 to 10. Call strchr() on a[i],
looking for the first ' ' character. Return the pointer
the character immediately following the ' '.

3. Use inline comments to refer to standards and practices,
explain non-obvious code, or to justify a hack.
4. Have a doc block at the head of each file describing the
purpose of that module, again keeping everything high-level.
5. Have a doc block for each function that lists the function name,
purpose (high-level), inputs, outputs, return values (and for C++
and Java, exceptions thrown).
6. Use descriptive and meaningful names for variables, constants,
and functions.
Jeff

Maybe a little OT, but I want a C specific viewpoint. Also, are there tools
similar to JavaDoc for C?


If there are, I've never seen one.
Nov 13 '05 #5
John Bode <jo*******@my-deja.com> scribbled the following:
3. Use inline comments to refer to standards and practices,
explain non-obvious code, or to justify a hack.


Also, if you're working on a multiple-person project, you can write
inline comments like "Fred: Please add real implementation here" or
"Are you sure THIS is what you want to do? Left unchanged just in case."
In our company, we write comments like these every now and then.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 13 '05 #6

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bp**********@oravannahka.helsinki.fi...
John Bode <jo*******@my-deja.com> scribbled the following:
3. Use inline comments to refer to standards and practices,
explain non-obvious code, or to justify a hack.


Also, if you're working on a multiple-person project, you can write
inline comments like "Fred: Please add real implementation here" or
"Are you sure THIS is what you want to do? Left unchanged just in case."
In our company, we write comments like these every now and then.


I've also worked at places where this was done, and sometimes
it escalated into arguments. Sort of like a newsgroup in the
code. :-)

-Mike
Nov 13 '05 #7
Mike Wahler <mk******@mkwahler.net> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bp**********@oravannahka.helsinki.fi...
John Bode <jo*******@my-deja.com> scribbled the following:
> 3. Use inline comments to refer to standards and practices,
> explain non-obvious code, or to justify a hack.
Also, if you're working on a multiple-person project, you can write
inline comments like "Fred: Please add real implementation here" or
"Are you sure THIS is what you want to do? Left unchanged just in case."
In our company, we write comments like these every now and then.

I've also worked at places where this was done, and sometimes
it escalated into arguments. Sort of like a newsgroup in the
code. :-)


I've not had arguments in code comments, but there have been two
occassions where I have had the chance to write a humorous comment.

One:
A colleague had Java code something like this:
try {
/* ... */
} catch (Exception e) {
System.err.println("Can't make hash from the password");
}
I added a comment:
// JOONAP: If you can't make hash, serve soup instead.

Two:
A colleague wrote a Javadoc comment for a method something like this:
/**
* Parses the result set returned by the JDBC API to give us some
* sensible data. Digs: row id, field type, other fields.
*/
(where "other fields" was really more names).
I added ", man." after the sentence starting with "Digs:".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Nov 13 '05 #8

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bp**********@oravannahka.helsinki.fi...
Mike Wahler <mk******@mkwahler.net> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bp**********@oravannahka.helsinki.fi...
John Bode <jo*******@my-deja.com> scribbled the following:
> 3. Use inline comments to refer to standards and practices,
> explain non-obvious code, or to justify a hack.

Also, if you're working on a multiple-person project, you can write
inline comments like "Fred: Please add real implementation here" or
"Are you sure THIS is what you want to do? Left unchanged just in case." In our company, we write comments like these every now and then.

I've also worked at places where this was done, and sometimes
it escalated into arguments. Sort of like a newsgroup in the
code. :-)


I've not had arguments in code comments, but there have been two
occassions where I have had the chance to write a humorous comment.

One:
A colleague had Java code something like this:
try {
/* ... */
} catch (Exception e) {
System.err.println("Can't make hash from the password");
}
I added a comment:
// JOONAP: If you can't make hash, serve soup instead.

Two:
A colleague wrote a Javadoc comment for a method something like this:
/**
* Parses the result set returned by the JDBC API to give us some
* sensible data. Digs: row id, field type, other fields.
*/
(where "other fields" was really more names).
I added ", man." after the sentence starting with "Digs:".


We think much alike. :-)

But I'd have probably jumped on the obvious agricultural
theme consistent in 'digs', 'row' and 'field', e.g. "Let's
farm this part out to somebody else." :-)

-Mike
Nov 13 '05 #9
On Tue, 18 Nov 2003 22:03:45 GMT, in comp.lang.c , "Mike Wahler"
<mk******@mkwahler.net> wrote:

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bp**********@oravannahka.helsinki.fi...
John Bode <jo*******@my-deja.com> scribbled the following:
> 3. Use inline comments to refer to standards and practices,
> explain non-obvious code, or to justify a hack.


Also, if you're working on a multiple-person project, you can write
inline comments like "Fred: Please add real implementation here" or
"Are you sure THIS is what you want to do? Left unchanged just in case."
In our company, we write comments like these every now and then.


I've also worked at places where this was done, and sometimes
it escalated into arguments. Sort of like a newsgroup in the
code. :-)


I had a guy come for an interview, armed with some printouts of some
of my comments from a project I'd worked on some years earlier.
Comments like "This is a complete hack, but it works for all cases I
tested, and for the rest the punters won't be able to tell the
difference" .... :-)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #10
Mark McIntyre <ma**********@spamcop.net> wrote:
I've also worked at places where this was done, and sometimes
it escalated into arguments. Sort of like a newsgroup in the
code. :-)


I had a guy come for an interview, armed with some printouts of some
of my comments from a project I'd worked on some years earlier.
Comments like "This is a complete hack, but it works for all cases I
tested, and for the rest the punters won't be able to tell the
difference" .... :-)


So... did he get the job? :)

Stig

--
brautaset.org
Nov 13 '05 #11
Mark McIntyre <ma**********@spamcop.net> spoke thus:
I had a guy come for an interview, armed with some printouts of some
of my comments from a project I'd worked on some years earlier.
Comments like "This is a complete hack, but it works for all cases I
tested, and for the rest the punters won't be able to tell the
difference" .... :-)


So basically you're renouncing any further ability to mock other
people who post here with code that is no better than a cheap hack?
;)

while( 1 ) {
if( 1 ) {
while( 1 ) {
if( 0 ) {
break;
}
else {
continue;
}
continue;
}
break;
}
else if( 0 ) {
break;
}
goto 2;
}

2: printf( "%s\n", 1?0?1?"continue":"break":"continue":"break" );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #12
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
Mike Wahler <mk******@mkwahler.net> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bp**********@oravannahka.helsinki.fi...
John Bode <jo*******@my-deja.com> scribbled the following:
> 3. Use inline comments to refer to standards and practices,
> explain non-obvious code, or to justify a hack.

Also, if you're working on a multiple-person project, you can write
inline comments like "Fred: Please add real implementation here" or
"Are you sure THIS is what you want to do? Left unchanged just in case."
In our company, we write comments like these every now and then.
I've also worked at places where this was done, and sometimes
it escalated into arguments. Sort of like a newsgroup in the
code. :-)
I've not had arguments in code comments, but there have been two
occassions where I have had the chance to write a humorous comment. One:
A colleague had Java code something like this:
try {
/* ... */
} catch (Exception e) {
System.err.println("Can't make hash from the password");
}
I added a comment:
// JOONAP: If you can't make hash, serve soup instead. Two:
A colleague wrote a Javadoc comment for a method something like this:
/**
* Parses the result set returned by the JDBC API to give us some
* sensible data. Digs: row id, field type, other fields.
*/
(where "other fields" was really more names).
I added ", man." after the sentence starting with "Digs:".


Here's a real-life Javadoc comment I wrote for a method in a class in
our application:

/**
* Returns the value of the HTTP header <code>Content-Disposition</code>.
* This header is needed to make Microsoft Internet Explorer realise that
* what we are giving it really <b>is</b> a PDF file, although it looks like
* a PDF file. Right. If Microsoft would stick to standards, we wouldn't
* have to do this, but then they wouldn't be Microsoft.<br/>
* I <b>hope</b> this works. I don't have Microsoft Internet Explorer to test
* this on.
* @param fileName The name of the file we're giving as a PDF.
* @return The value of the <code>Content-Disposition</code> HTTP header.
* @author Joona Palaste
*/

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush
Nov 13 '05 #13

"Christian Bau"

Maybe a little OT, but I want a C specific viewpoint. Also, are there tools similar to JavaDoc for C?


There is this but I've never used it:

http://www.stack.nl/~dimitri/doxygen/

ML
Nov 13 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rony | last post by:
A question on python source documentation. Does there exist a standard for documenting code ? I've included here an example generated by pydoc of one of my modules. Is this the right way or is it...
5
by: Isaac Rodriguez | last post by:
Hi, Are there any standarized ways of documenting Python code? When I check the __doc__ attribute of the standard modules, the results are kind of plain. Is everyone using this style? Since...
6
by: Jeremy Wallace | last post by:
I have a ton of queries that I need users to be able to view. I'd like to have them viewed in a datasheet-view form instead of directly, so that I can keep the users from futzing with the data. ...
1
by: strauss.sean | last post by:
I have been asked to begin documenting the ongoing development and changes to a database that I maintain. Not the entry of data; this is about the changes to how tables are restructured, and any...
5
by: Anders Borum | last post by:
Hello! I was wondering if there are any guidelines to documenting our C# code? I'm not talking about the actual syntax for the Xml markup we add to the classes, methods etc., but general...
1
by: Brett | last post by:
I'd like to have all of my documentation in one place. I use the following for documenting code: - attributes for certain types of documentation - use of the C# generated inline XML...
8
by: Spleenwort | last post by:
With regard to XML comments in c#. I think that #regions should be self-documenting relative to XML comments or that a <region> tag should be defined and auto-inserted when you type #region...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
52
by: Paul McGuire | last post by:
I'm starting a new thread for this topic, so as not to hijack the one started by Steve Howell's excellent post titled "ten small Python programs". In that thread, there was a suggestion that...
2
by: sajithamol | last post by:
What is a Self Documenting Code? wheather java is a self documenting code?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.