Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly comment [closed]

This is a very simple question which I am surprised I haven't found anywhere else on SO. I was wondering which comments should or shall not be in header/source files and even, because some languages don't really use the header/source system, what is the proper way to comment.

So far I have been doing it like that :

main.c or main.cpp

int main()
{
    // Comments to describe what happens in main
}

foo.h

 // Comments for documentation and which gives information about the function itself

/**
* \fn void aFunction(void)
* \brief This function is a function
*/
void aFunction(void);

foo.c or foo.cpp

void aFunction(void)
{
    // Comments to describe and explain what happens within this function
}
  • Not much comments in main, just describing basically what functions are called and why
  • In header, only comments to describe the function itself; parameters, brief, return etc.
  • In source, only comments to describe what's happening within the function; loops, condition, etc.

That is what I know for sure. Are there more comments needed in either main, source or header ? Should I add the comments I usually only put in the header in the source too, like that :

foo.c or foo.cpp

/**
* \fn void aFunction(void)
* \brief This function is a function
*/
void aFunction(void)
{
    // Comments to describe and explain what happens within this function
}

I know this may sound subjective, but it is an obvious fact that some devs are better at commenting than others, and thus that there are good and bad ways to comment.

like image 440
Badda Avatar asked May 16 '17 09:05

Badda


People also ask

How do you end a comment?

End comments usually start out with a positive about the paper. Every paper has a positive; it is good to mention and praise the positive to encourage the writer. Honesty in commentary is important, however; it is not a good idea to write insincere praise, just to write praise.

What is a closed comment?

Closing a comment thread is used to halt the discussion while allowing the current comments to remain visible.

How do you write comments in your code?

Choose filenames (for headers and compilation units) so groupings of functions and declarations is obvious. Then use comments only when needed to explain something that isn't obvious by looking at the code itself. For example, it is better to explain WHY code does something, and allow the HOW to be described by the code itself.

What is the use of comments in C++?

In all programming languages, comments are used to explain something in a normal way, something like post-its you put into lines to remember or to notify other developers. In C and C++, there are two kinds of commenting character series; 1. Single Line Commenting with // Double forward slashes, // is used as a single-line comment.

Should you comment your source code?

There is a not-small subset of developers who believe that commenting your code should be an exceptionally rare occasion. That when you need source code comments, that’s an indication that your code is weak in some way.

Why do we need to write comments in Java?

Not only does it help me keep track of the code as it grows more complicated, but it also makes the code more understandable for anyone that needs to reference the code at a later date. Comments in Java are the statements in the Java code that are not executed when the program is run. Think of them as markers that guide the reader.


1 Answers

C files should contain the usual comments you write anywhere when you write code. What does it do and why. Usually at the end of each line unless there is a need for more extensive comments.

H files can either contain a brief minimum, explaining what a function takes as parameters and what it returns. Or alternatively it could contain a full documentation of how the function should be used. If the full documentation is not provided in the header file, you will have to write it separately. Note: a few lines of Doxygen crap just to generate some manner of useless "documentation" file does not count as complete documentation.

H files document what a function does and how it should be used, without mentioning implementation details. It is important is to realize that a h file should be the full, stand-alone interface for the corresponding c file (or library). All types and dependencies (includes) that the caller needs to be aware of should be present in the h file.

This includes any pre- and post-conditions: What needs to be executed prior to calling a function? What resources did the function use? Did it leave any handles/files/resources open that need to be cleaned up later? Did it alter some global state? Etc etc.

The corresponding c file(s) are not necessarily made available to the user, nor should the user need to read the c file in order to understand how the functions there should be used. Everything should be made obvious from the h file alone.

like image 89
Lundin Avatar answered Nov 14 '22 22:11

Lundin