473,788 Members | 2,820 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 4763

cl***********@t his.is.invalid wrote:
On 13 Jan 2007 15:59:57 -0500, Dave Harris wrote:
I think trying to line up comments with tabs or spaces is a mugs game.
Including things like:

int x; // Some value.
std::vector<int table; // Some other value.

It's just too much trouble to maintain. [...] And although it looks
prettier
I don't believe it is any easier to read.

I hope this won't create an uproar but one shouldn't underestimate the
good effects of pleasure (I mean of the spirit :-)). Our mood does
affect our activities, especially the mental ones.
I agree with you fully on this one. I just love to work with nice and
well structured code (and hate the pasta-like alternatives). It is also
easier to find bugs and/or enhance such code.

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

Jan 18 '07 #61
In article <11************ **********@11g2 000cwr.googlegr oups.com>, James
Kanze <ja*********@gm ail.comwrites
>I think it also passes an important message: that the programmer
cared about the code he was writing. (But not aligning things
doesn't mean that the programmer doesn't care. If it's aligned,
he probably cared; if it's not, you don't know, unless there are
other signs---and there almost always are, if he did care.)

And if it isn't aligned it may well be that that is an artefact of the
editor he used and the one you are using to read his code (or even the
configuration details of the respective editors)
--
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 18 '07 #62
James Kanze wrote:
cl***********@t his.is.invalid wrote:
On 13 Jan 2007 15:59:57 -0500, Dave Harris wrote:
I think trying to line up comments with tabs or spaces is a mugs
game. Including things like:
int x; // Some value.
std::vector<int table; // Some other value.
It's just too much trouble to maintain. [...] And although it
looks prettier I don't believe it is any easier to read.
I hope this won't create an uproar but one shouldn't underestimate
the good effects of pleasure (I mean of the spirit :-)). Our mood
does affect our activities, especially the mental ones.

Agreed. I'd rather maintain esthetically pleasing code.

I think it also passes an important message: that the programmer
cared about the code he was writing. (But not aligning things
doesn't mean that the programmer doesn't care. If it's aligned,
he probably cared; if it's not, you don't know, unless there are
other signs---and there almost always are, if he did care.)
How often do you see someone doing something they really care about in
a sloppy fashion?

While code that looks estetically pleasing to me may not look the same
to you - after all, countless flamewars have been fought about the most
important question ever in the proramming universe, namely where the
curly braces go - at least a consistent layout with a minimum of
formatting and well tought-out variable names would suggest that the
original author of the code cared about what e was doing.

Once you find three to four different styles of indentation in a single
file then I would put the probability of the file containing bugs at
80% plus...

Guess who's currently working on software that has dark corners with
strangely formatted files and long functions in said files that tend to
contain multiple declarations of "int i" within said long functions?

--
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 18 '07 #63
Timo Geusch wrote:
James Kanze wrote:
cl***********@t his.is.invalid wrote:
On 13 Jan 2007 15:59:57 -0500, Dave Harris wrote:
I think trying to line up comments with tabs or spaces is a mugs
game. Including things like:
int x; // Some value.
std::vector<int table; // Some other value.
It's just too much trouble to maintain. [...] And although it
looks prettier I don't believe it is any easier to read.
I hope this won't create an uproar but one shouldn't underestimate
the good effects of pleasure (I mean of the spirit :-)). Our mood
does affect our activities, especially the mental ones.
Agreed. I'd rather maintain esthetically pleasing code.
I think it also passes an important message: that the programmer
cared about the code he was writing. (But not aligning things
doesn't mean that the programmer doesn't care. If it's aligned,
he probably cared; if it's not, you don't know, unless there are
other signs---and there almost always are, if he did care.)
How often do you see someone doing something they really care about in
a sloppy fashion?
Never.
While code that looks estetically pleasing to me may not look the same
to you - after all, countless flamewars have been fought about the most
important question ever in the proramming universe, namely where the
curly braces go - at least a consistent layout with a minimum of
formatting and well tought-out variable names would suggest that the
original author of the code cared about what e was doing.
Certainly. And there are two philosophies regarding the
alignment of opening comments. I like them aligned; Dave Harris
doesn't think it matters. Both are valid viewpoints, just like
the position of the opening brace. If you see them aligned, you
know that the programmer cared. You also know that he thought
it mattered. If you don't see them aligned, you don't know
whether it was because he didn't care, or because he simply
didn't feel that it mattered.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Simard, 78210 St.-Cyr-l'Icole, 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 19 '07 #64
James Kanze wrote:
Agreed. I'd rather maintain esthetically pleasing code.

I think it also passes an important message: that the programmer
cared about the code he was writing. (But not aligning things
doesn't mean that the programmer doesn't care. If it's aligned,
he probably cared; if it's not, you don't know, unless there are
other signs---and there almost always are, if he did care.)
The message it brings is that the programmer cared about the aesthetics
of the code, not necessarily the working quality of the code. I've seen
lots of (novice) programmers who write poor quality code but care a lot
about its decoration, spending time on drawing big boxes of asterisks
around only a few lines of the actual comments.

--
Seungbeom Kim

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

Jan 20 '07 #65
ja*********@gma il.com (James Kanze) wrote (abridged):
Certainly. And there are two philosophies regarding the
alignment of opening comments. I like them aligned; Dave Harris
doesn't think it matters.
Actually my position is more anti than my earlier remark suggested. I
think end-comments are often easier to read if they are not aligned. If
you have some very short declarations, and one or more long ones, then the
long one pushes the comment so far to the right, and so far from the the
short ones, that it can be hard to see which comment goes with which
declaration.

My editor can't align end-of-line comments automatically, but if it could
I'd switch the feature off.

There are other cases where prettiness is counter-productive. For example,
justified text looks cleaner but is harder to read than ragged-right
because irregularity helps the eye keep track of where it is.

(There are also cases where it helps, of course. It can highlight symmetry
and make column-selections more useful.)

-- Dave Harris, Nottingham, UK.

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

Jan 20 '07 #66
On Fri, 12 Jan 2007 19:21:36 -0500, noone <no***@all.comw rote:
....
I have no preference regarding whitespace in
expressions and deviate in style there myself.
I think and hope you mean something like "... and use different styles
depending on the circumstances, in a way I cannot express in the form of
rules".

I can accept styles that don't look like mine, and I think style should
change depending on the thing you try to implement -- but I cannot tolerate
when people have /no/ style, and write more or less at random.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Jan 20 '07 #67
[c.l.c++.m trimmed]

On 13 Jan 2007 00:46:54 -0500, kevin cline <ke*********@gm ail.comwrote:
>
Satish wrote:
>Basically attached is the class layout and source code layout... You
can see the comments section. Also if you want to comment a large
piece of code but at the same time you don't want to delete the code
use #ifdef like

#ifdef JUST_FOR_COMMEN TING_OUT
code here which you dont want to execute
#endif
"#if 0" is mush more obvious, IMHO. It cannot mean anything else.
(But if you use this in code someone else ever sees, you really need a system
for revision control instead.)
>>
############## CLASS LAYOUT######### ############### ##
/**
* A one line description of the class.
*
* #include "XX.h" <BR>
* -llib
*
* A longer description.
*
* @see something
*/
Here he's are assuming that the header file encapsulates a class and nothing
else. That's pretty common I guess, but you might equally well have
namespaces, templates, functions, more related classes ...
All the comments that follow are completely useless. They consume
screen space while adding no information, merely stating what is
obvious to a C++ programmer with more than a month's experience. Code
is much easier to read and understand when it is uncluttered.
Yes. I used that commenting style in my very first C++ programs way back,
and it just looks silly today.

IMHO, for a class, the most important part is the documented purpose and
limitations of the class as a whole. Surprisingly often, the rest is taken
care of with good function and parameter names.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Jan 20 '07 #68
Jorgen Grahn wrote:
[c.l.c++.m trimmed]

On 13 Jan 2007 00:46:54 -0500, kevin cline <ke*********@gm ail.comwrote:
>>Satish wrote:
>>>Basically attached is the class layout and source code layout... You
can see the comments section. Also if you want to comment a large
piece of code but at the same time you don't want to delete the code
use #ifdef like

#ifdef JUST_FOR_COMMEN TING_OUT
code here which you dont want to execute
#endif


"#if 0" is mush more obvious, IMHO. It cannot mean anything else.
(But if you use this in code someone else ever sees, you really need a system
for revision control instead.)
Why bother? Just delete the code and recover it from version control if
you want it back.

--
Ian Collins.
Jan 20 '07 #69
Seungbeom Kim wrote:
James Kanze wrote:
Agreed. I'd rather maintain esthetically pleasing code.
I think it also passes an important message: that the programmer
cared about the code he was writing. (But not aligning things
doesn't mean that the programmer doesn't care. If it's aligned,
he probably cared; if it's not, you don't know, unless there are
other signs---and there almost always are, if he did care.)
The message it brings is that the programmer cared about the aesthetics
of the code, not necessarily the working quality of the code. I've seen
lots of (novice) programmers who write poor quality code but care a lot
about its decoration, spending time on drawing big boxes of asterisks
around only a few lines of the actual comments.
There's esthetics, and esthetices. Drawing big boxes of
asterisks is ugly (but not much extra effort if you have the
right tools); the only place I'd use it is at the top (the
copyright header). Caring about *superficial* decoration is not
caring about the code you are writing.

With regards to aligning end of line comments, to be frank, it's
rarely an issue, since I don't use them much. If I have a
series of anything parallel, however (assignments, end of line
comments, etc.), I rather like to align them to stress the
parallelism. I'll also line up initializers in an agglomerate
initialization, if possible. (And as Dave Harris pointed out,
this causes problems if one individual right-hand side element
is much longer than the others. As with any rule, it's not
perfect, and you must weigh the pro's and the contra's in each
case.)

FWIW: I don't do this by hand. I have various programs which to
it for me. (At least with emacs and vim, it's easy to pass a
block of code through an external filter, so you have all the
power of C++ at your disposition for special formatting.)

--
James Kanze (Gabi Software) email: ja*********@gma il.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 21 '07 #70

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

Similar topics

46
2496
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
9656
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
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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
8993
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
7517
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
5399
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.