Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you style major and minor comments?

Sometimes I want to write a "major" comment to describe a large block of code and then write "minor" comments to describe a few of the lines within that block of code:

// Major comment

// Minor comment
...

// Minor comment 2
...

The major comment looks strange without code directly beneath it, and you can't visually tell how much code it is describing below.

How do you style these comments?

(I remember reading about this in Code Complete a while ago but I don't own the book.)

like image 529
Paige Ruten Avatar asked Jan 01 '09 05:01

Paige Ruten


3 Answers

I use multi-line comments for 'major' comments:

/*
 * yada yada yada
 */

And use the single line comments for the minor ones.

I know some people don't like using /* */ style comments because it makes it harder to automatically comment and uncomment blocks ... but I like how they look, and I'm a strong believer that code should be aesthetically pleasing to read.

That also means that you should be able to parse the structure of the code largely without reading the details, which means I tend to use quite a lot of whitespace and also seperator comment lines to help break things up. So for a block I want to comment I might write:

/**
 * detailed description ...
 */

code

// -- minor comment
code

code
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
like image 178
Rob Walker Avatar answered Nov 07 '22 04:11

Rob Walker


// Major comment
// -------------
...

// Minor comment
...

... // Minor comment (end of line)
like image 41
cschol Avatar answered Nov 07 '22 02:11

cschol


I use something like this, to make it look more like a "heading" or separator for the following block of code:

// ***** Major comment *****

// Minor comment
...

// Minor comment 2
...

Of course that assumes that the "major comment" is only a few words

like image 26
Marc Novakowski Avatar answered Nov 07 '22 02:11

Marc Novakowski