473,788 Members | 2,811 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 4762
ke*********@gma il.com (kevin cline) wrote (abridged):
Sometimes splitting code over separate functions help, and sometimes
it doesn't. Sometimes in order to understand the code I need to see
it all in one place; get it all in my head at once. For example,
a variable may be set up in one block and used in another, and
these may be coupled such that the blocks are hard to understand
in isolation.

Convert the first block into a function returning the value, then pass
it into a second function. If the first block computes multiple
values, then that data should probably be collected into a class.
I'm well aware of the "method object" pattern. I use it when it is
appropriate. It's not always appropriate. I think you missed the point
about how separating production from consumption can make the code harder
to understand.

Empty is rather a poor name
I agree, but it is the name used by the standard library.

Better naming works wonders, but first one has to see a program with
consistently good naming. In my experience, few programmers have seen
such a program.
I have seen such. I've even written such. And if your naming is going to
be consistent, and it is also going to use std::vector, then it has to use
"empty".

-- 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 16 '07 #51
Seungbeom Kim wrote:
Gerhard Menzl wrote:
Dave Harris wrote:
"IsEmpty" is hardly better than "empty" in that both assume the reader
knows what "empty" means. As I explained, projects often develop
specialised vocabularies that need to be documented.
Knowing what "empty" means does not resolve the ambiguity, which is
caused by the nature of the English language: little inflectional and
conjugational variation, usage of words both as verbs and adjectives.
Thus the word "empty" by itself could either be interpreted as an
imperative ("empty!"), or as an abbreviated question ("empty?").
I prefer to prefix boolean queries with "is" or "has" because it avoids
unnecessary mental effort on the part of the reader.
Then how would you name a function that does the emptying action?
make_empty()?
The general rule is that function names are verbs or verbal
phrases. Given empty(), in this context, "empty" must be a
verb, and says what is done to the object.
(What's wrong with "isEmpty" specifically is that it's inconsistent
with std::vector; but we're surely talking general principles here and
don't want to nit-pick details of the examples.)
The C++ Standard Library naming convention is not very helpful in that
empty() is used as an adjective (query), but clear() as a verb
(command). Obviously, terseness was valued higher than intuitive
understanding.
I think the C++ Standard Library established the convention anyway,
no matter how bad some people may argue it is (because it is "the"
standard). Then following the already-established convention certainly
has its merits, because people already know it.
The problem is that it is in contradiction with every other
existing standard (including common English usage). The basic
first rule of naming is that types are unqualified nouns,
objects, qualified nouns and functions, verbs. There are
exceptions, of course: many people prefer using qualified nouns
for attributes, even if in C++ what the client uses are actually
accessor functions (getters and setters). But the basic
principle is there, and as a function, empty() can ONLY mean
empty the collection.

--
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 17 '07 #52
Seungbeom Kim wrote:
Then how would you name a function that does the emptying action?
make_empty()?
My convention follows the pattern "is/has" + adjective/noun for boolean
queries and verb [+ object] for commands. I would therefore have no
problems with is_empty() and empty(). Since the latter stands alone, it
must be a verb.

--
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".

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

Jan 17 '07 #53
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.

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

Jan 17 '07 #54

Alf P. Steinbach wrote:
* mlimber:

On a personal note, I prefer not to use the preprocessor to "comment
out" code like Satish does above. Instead I use // comments throughout
and use /**/ comments to get rid of blocks and/or regular expression
search-and-replace to add // to the beginning of each line I want to
comment out.

Agreed that // comments are less visually imposing. And it's a shame
that /*...*/ don't nest, necessitating using condititional compilation
as a commenting-out device.
It's also that several code editors that use colour for syntaxing will
not mark the
commented out code as a comment in pre-processors so it is an effort to
see that
it is actually commented out.

Jan 17 '07 #55

Seungbeom Kim wrote:
Gerhard Menzl wrote:
Dave Harris wrote:
"IsEmpty" is hardly better than "empty" in that both assume the reader
knows what "empty" means. As I explained, projects often develop
specialised vocabularies that need to be documented.
Knowing what "empty" means does not resolve the ambiguity, which is
caused by the nature of the English language: little inflectional and
conjugational variation, usage of words both as verbs and adjectives.
Thus the word "empty" by itself could either be interpreted as an
imperative ("empty!"), or as an abbreviated question ("empty?").

I prefer to prefix boolean queries with "is" or "has" because it avoids
unnecessary mental effort on the part of the reader.

Then how would you name a function that does the emptying action?
make_empty()?
(What's wrong with "isEmpty" specifically is that it's inconsistent
with std::vector; but we're surely talking general principles here and
don't want to nit-pick details of the examples.)
The C++ Standard Library naming convention is not very helpful in that
empty() is used as an adjective (query), but clear() as a verb
(command). Obviously, terseness was valued higher than intuitive
understanding.

I think the C++ Standard Library established the convention anyway,
no matter how bad some people may argue it is (because it is "the"
standard). Then following the already-established convention certainly
has its merits, because people already know it.
bool myclass::empty( ) const; will be always a query. I can't see any
ambiguity with that

I believe in shorter names and self documentation coming from a
(member-)function interface; although I agree on additional comments
like // O(1)

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

Jan 17 '07 #56
Stephan Kuhagen wrote:
Keith H Duggar wrote:
/** Second Paragraph */
Books are not ASCII source code and are not bound by it's
limitations. Authors are free to use a variety of expository
tools (tables, line numbers, weights, styles, colors, etc)
and I have seen all these alternatives used in programming
textbooks. So don't we set a bad example by choosing the
useless redundant comment tool? // End Second Paragraph
Sometimes the "book" and the source code are the same thing, as in literate
programming. For a real world example, have a look at "Physically Based
Rendering" from Matt Pharr and Greg Humphreys. The layout of the book is
amazing, the source code (c++) is perfectly readable and has perfectly
helpful documentation.
So it is possible to achieve good documentation, readable layout with lots
of additional information and maintainable source at once. But I can't
imagine, how much discipline it takes to consequently follow the rules of
such documentation style. And the worst is, currently there are very few
tools that help, to do this kind of intermixed coding/documenting.
What about CWeb? If I were writing a book, I'm pretty sure that
that's what I'd use (but maybe only because I don't know of
anything else).

--
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

Jan 17 '07 #57
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.)

--
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 18 '07 #58
James Kanze wrote:
What about CWeb? If I were writing a book, I'm pretty sure that
that's what I'd use (but maybe only because I don't know of
anything else).
Yes, CWeb would be also the first thing I'd look at. But there are very few
tools out there, that does something like it (and even fewer people know
about or consider using them). For the book, I mentioned, the authors
implemented their own tool... I think, the idea of literate programming is
a very good approach towards readable documentation, but the
usability/availability of the tools is "improvable ". That is a pity,
because literate programming has (or had, maybe the chance is gone?) IMHO
the potential for establishing some techniques for better documentation.

Regards
Stephan
Jan 18 '07 #59
Diego Martins wrote:
bool myclass::empty( ) const; will be always a query. I can't see any
ambiguity with that
Providing you are looking at the prototype or function definition. The
meaning of the function is much harder to figure out when all you are
looking at is a call to it, especially when it is part of a complicated
expression. A verb-based naming convention (which would suggest is_empty
for a query) can improve readability.

--
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".

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

Jan 18 '07 #60

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
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
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...
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
5398
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?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
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.