473,800 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Commenting style?

Can someone recommend a good source of C/C++ coding style.
Specifically, I am interested in commenting style and in
particular how to indent comments and the commented code, rather
than when to use comments. Thanks in advance!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 11 '07
100 4769
In article <45************ ***********@new s.sunsite.dk>, Angel Tsankov
<fn*****@fmi.un i-sofia.bgwrites
>And what about:

int z; // Comment.
int y; // Another comment.
std::vector<do ublex_intersect ions; // Yet another comment.
IMHO, madness. Allowing the longest typename control the position of the
names being declared (and do you propose to do the same for function
declarations? Or perhaps we should make sure all the assignment
operators are vertically aligned? :-)

The rules for code layout should be about readability. People will
disagree about the details. In ordinary text I have been taught to try
to avoid vertical alignment, particularly where the words are similar as
that inhibits easy reading by trapping the eye into skipping lines.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 24 '07 #81
Francis Glassborow wrote:
In article <xn************ ***@nermal.unix consult.co.uk>, Timo Geusch
<tn***@unixcons ult.co.ukwrites
For me, aesthetics are mainly about code readbility - if it's easy
to follow then chances are it's good quality code because mistakes
would be more obvious compared to code that needs a pressure washer
to clean up.

But for me, at least, code that reads cleanly with a minimum (zero
being the ideal) of comments is better than code which is laden with
comments. If the programmer really cares s/he will choose names etc.
that make most comments superfluous.
Oh, I agree - while it'll be hard to do Knuth-type literate programming
in C++, a decent programmer should make the code both readable and
comprehensible without the aid of too many comments such that the
comments are really reserved for the trickier bits.

--
The lone C++ coder's blog: http://www.bsdninjas.co.uk/codeblog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 24 '07 #82
Francis Glassborow wrote:
In article <45************ ***********@new s.sunsite.dk>, Angel Tsankov
<fn*****@fmi.un i-sofia.bgwrites
And what about:
int z; // Comment.
int y; // Another comment.
std::vector<dou blex_intersecti ons; // Yet another comment.
IMHO, madness. Allowing the longest typename control the position of the
names being declared (and do you propose to do the same for function
declarations? Or perhaps we should make sure all the assignment
operators are vertically aligned? :-)
The rules for code layout should be about readability. People will
disagree about the details. In ordinary text I have been taught to try
to avoid vertical alignment, particularly where the words are similar as
that inhibits easy reading by trapping the eye into skipping lines.
But code isn't ordinary text, and there are many cases where
using two dimensions can add to understandabili ty. The most
obvious is initialization lists; given something like:

struct MapInit
{
char const* key ;
int value ;
} ;
MapInit const initTable[] =
{
{ "one" , 1 },
{ "two" , 2 },
{ "three", 3 },
} ;

I cant' imagine not aligning. It's very much like a table in a
book or article (which would also be aligned). Because in C and
C++, declarations and expression statements are so
indistinguishab le, I've also adopted the policy of using a
special formatting for declarations: what is being declared is
always indented 20 spaces. It's arbitrary, but it does mean
that a declaration doesn't look like just another expression,
and draws attention to what is being declared. Other than that,
I do use a lot of vertical alignment, when I want to draw
attention to parallels in the code: if I'm assigning a sequence
of values, for example, I'll align the = signs, so that the
reader will immediately see the code for what it is: a sequence
of the same operations. Code is, on the whole, a lot less
verbose than normal text, and anything I can do to add semantic
content for the reader, I do.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 25 '07 #83
On Tue, 23 Jan 2007 10:40:55 CST, Francis Glassborow wrote:
>But for me, at least, code that reads cleanly with a minimum (zero being
the ideal) of comments is better than code which is laden with comments.
If the programmer really cares s/he will choose names etc. that make
most comments superfluous.
This is something that I hear often. I think it only addresses a
specific category of comments (those that try compensating for bad
naming and bad coding practices in general).
>In fact when I see code that has more than about 10% inline comments I
begin to expect the worst (and if every line has a comment, I am pretty
certain that the comments will include serious mistakes)
Well, for one thing, code meant for automatic documentation extraction
(e.g. for doxygen) easily exceeds that number (or is "inline comment"
meant to exclude comments to be extracted?).

Apart from that, I'd be glad to know if your expectation-of-the-worst
is met with my code, which I guess has a relatively high number of
comments:

<http://breeze.svn.sour ceforge.net/viewvc/breeze/trunk/breeze/>

--
HELP: many of this group's participants might know that I'm the legit
~~~~ owner of the yahoo account with id 'gennaro_prota' ; I would be
immensely grateful to anyone who might help me gaining access
to it again (note that I'm now using the id 'gennaro.prota' ).
Thanks!

Genny.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 28 '07 #84
[comp.lang.c++.m oderated removed - nothing ever came of previous attempt
to reply]

Gennaro Prota wrote:
Apart from that, I'd be glad to know if your expectation-of-the-worst
is met with my code, which I guess has a relatively high number of
comments:

<http://breeze.svn.sour ceforge.net/viewvc/breeze/trunk/breeze/>
I think you could cut down the size of your "header" comment a great
deal (where you explain the license and such. There are also many files
that have several lines of comment (like 100+) and only 5 lines of code
or so. This is a little excessive. Also this file in particular:

http://breeze.svn.sourceforge.net/vi...16&view=markup

There is more comment than code, as before, but worse...the code is
intermixed into the comments so you have like 100 lines of comment, a
line of code...20 lines of comment, a line of code, 10 lines of comment,
2 lines of code...etc...no t good. Makes the code pretty much
unreadable. What this illustrates is that in your projects the code has
less importance than the comment and it should be the other way around.

If you want to document the research you did it should be in a separate
file ... maybe even a paper. Your code shouldn't need a
bibliography... and yours has one. Yeah, credit where credit is
due...but put that somewhere else.

You asked...
Jan 28 '07 #85
In article <bv************ *************** *****@4ax.com>, clcppm-
po****@this.is. invalid says...

[ ...]
Well, for one thing, code meant for automatic documentation extraction
(e.g. for doxygen) easily exceeds that number (or is "inline comment"
meant to exclude comments to be extracted?).
Comments to be extracted are normally not inline, but the extraction
isn't really the primary point. Inline comments are comments inline with
the code, intended to comment on a specific line of code. Header
comments that tell about the purpose of a routine (for example) aren't
inline comments. If you're using something like Doxygen, then yes, those
are (typically) the comments you format for extraction. Even if you
don't make any attempt at formatting them specifically for automatic
extraction, the same general kind of comment still isn't inline though.
Apart from that, I'd be glad to know if your expectation-of-the-worst
is met with my code, which I guess has a relatively high number of
comments:

<http://breeze.svn.sour ceforge.net/viewvc/breeze/trunk/breeze/>
Looking at a little of your code:

//
//! Generalized version of std::accumulate ().
//!
//! breeze::accumul ate_traits<is a customization
//! point: you are allowed to specialized it in
//! namespace breeze for your own type.
//

Most people would call this a block comment (or something similar). The
fact that it's formatted for automatic extraction is more or less beside
the point -- if you weren't using an automatic extraction tool, and
didn't do any special formatting on it for extraction, it'd still be a
block comment.

OTOH, it is more or less a one-way street: if you looked at some code
and decided something looked like a good candidate for automatic
extraction, that would probably be a fair indication that you were
looking at a block comment, not an inline comment.

template< typename InputIterator, typename T >
inline typename accumulate_trai ts< T >::result_typ e
accumulate( InputIterator begin,
InputIterator end,
T v = accumulate_trai ts< T >::first() )
{
return breeze::accumul ate_traits<
T >::compute( begin, end, v ); // gps qualify?

This one is an inline comment. It looks to me like an indication of
unfinished code -- it should really be replaced with a unit test that
proves one way or the other. It might well be appropriate to add a block
comment to that unit test to explain why it matters (assuming it's not
obvious to anybody who's familiar enough with your code to really care).

--
Later,
Jerry.

The universe is a figment of its own imagination.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 29 '07 #86
Gennaro Prota wrote:
Apart from that, I'd be glad to know if your expectation-of-the-worst
is met with my code, which I guess has a relatively high number of
comments:

<http://breeze.svn.sour ceforge.net/viewvc/breeze/trunk/breeze/>
I think you could cut down the size of your "header" comment a great
deal (where you explain the license and such. There are also many files
that have several lines of comment (like 100+) and only 5 lines of code
or so. This is a little excessive. Also this file in particular:

http://breeze.svn.sourceforge.net/vi...16&view=markup

There is more comment than code, as before, but worse...the code is
intermixed into the comments so you have like 100 lines of comment, a
line of code...20 lines of comment, a line of code, 10 lines of comment,
2 lines of code...etc...no t good. Makes the code pretty much
unreadable. What this illustrates is that in your projects the code has
less importance than the comment and it should be the other way around.

If you want to document the research you did it should be in a separate
file ... maybe even a paper. Your code shouldn't need a
bibliography... and yours has one. Yeah, credit where credit is
due...but put that somewhere else.

You asked...

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 29 '07 #87
Noah Roberts <nr******@examp le.netwrites:
Gennaro Prota wrote:
Apart from that, I'd be glad to know if your expectation-of-the-worst
is met with my code, which I guess has a relatively high number of
comments:

<http://breeze.svn.sour ceforge.net/viewvc/breeze/trunk/breeze/>

I think you could cut down the size of your "header" comment a great
deal (where you explain the license and such. There are also many files
that have several lines of comment (like 100+) and only 5 lines of code
or so. This is a little excessive. Also this file in particular:

http://breeze.svn.sourceforge.net/vi...16&view=markup

There is more comment than code, as before, but worse...the code is
intermixed into the comments so you have like 100 lines of comment, a
line of code...20 lines of comment, a line of code, 10 lines of comment,
2 lines of code...etc...no t good. Makes the code pretty much
unreadable. What this illustrates is that in your projects the code has
less importance than the comment and it should be the other way around.

If you want to document the research you did it should be in a separate
file ... maybe even a paper. Your code shouldn't need a
bibliography... and yours has one. Yeah, credit where credit is
due...but put that somewhere else.
I agree. This style of commenting is better suited to Web than C++.

Ryan -

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 29 '07 #88
Noah Roberts wrote:
Gennaro Prota wrote:
Apart from that, I'd be glad to know if your expectation-of-the-worst
is met with my code, which I guess has a relatively high number of
comments:
<http://breeze.svn.sour ceforge.net/viewvc/breeze/trunk/breeze/>
I think you could cut down the size of your "header" comment a great
deal (where you explain the license and such. There are also many files
that have several lines of comment (like 100+) and only 5 lines of code
or so.
It all depends. I know that I once had a header with close to
2000 lines of comments, for about 300 lines of code. If the
header is being used as the external documentation of the class,
the case can occur. (To be fair, in my case, the header
contained the external documentation in two languages. But even
if I delete the French part, there will be close to 1000 lines
of comment.)

He's using the GNU licence, and I think his license text is
about right for this: copyright, a statement that the code is
under GPL, and an indication where you can get the full text.
I'd reorder it slightly, but that's about it.
This is a little excessive. Also this file in particular:
http://breeze.svn.sourceforge.net/vi...16&view=markup
There is more comment than code, as before, but worse...the code is
intermixed into the comments so you have like 100 lines of comment, a
line of code...20 lines of comment, a line of code, 10 lines of comment,
2 lines of code...etc...no t good. Makes the code pretty much
unreadable. What this illustrates is that in your projects the code has
less importance than the comment and it should be the other way around.
The file in question is a header, which contains (in this case)
the external documentation. Which means that the documentation
is more important than the code, since it determines what the
code should do. And I don't see any problem reading it: to
begin with, of course, syntax highlighting means that the
comments are of a different color. (Historically, I almost
always terminate a block comment with an underline, to separate
it from the code. I developed this habit in the days before
syntax highlighting, however; I doubt that it is that necessary
today.)
If you want to document the research you did it should be in a separate
file ... maybe even a paper. Your code shouldn't need a
bibliography... and yours has one. Yeah, credit where credit is
due...but put that somewhere else.
Why? So that it won't be updated when the file changes.
Anything which concerns just the contents of a single file
should be documented in that file, not elsewhere. (You often
need separate documentation as well, to give a larger, more
global view, with the relationships between classes.) On the
whole, I liked his documentation style (but that's probably
because it looks very much like my own).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 29 '07 #89
In article <11************ *********@a75g2 000cwd.googlegr oups.com>, James
Kanze <ja*********@gm ail.comwrites
>Why? So that it won't be updated when the file changes.
Anything which concerns just the contents of a single file
should be documented in that file, not elsewhere. (You often
need separate documentation as well, to give a larger, more
global view, with the relationships between classes.) On the
whole, I liked his documentation style (but that's probably
because it looks very much like my own).
However I think such documentation should not make it hard to find the
code and scrolling down through several screens isn't exactly making
code accessible and readable. It would, I think, be helpful if the tools
that extract documentation also provided for the exclusion of the
documentation.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 29 '07 #90

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

Similar topics

46
2501
by: Profetas | last post by:
Hi, I know that this is off topic. but I didn't know where to post. Do you comment your source code while coding or after coding. for example: you write a procedure and after it is working, you'll comment it.
9
1879
by: | last post by:
Does C++/CLI have XML style commenting like C#?? If i do /// I dont get the completion like C#
8
1866
by: lallous | last post by:
Hello I've been programming for a number of years, however my commenting style is always different. Sometimes I use something like: /************************ * method .... * comments... *************************/
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10250
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.