Naming conventions

J

Joe Greer

::
:: - When naming pointers should I append a prefix (pName, or
:: p_name), add a suffix (name_p) or nothing at all?

Think about what that will buy you. Is it important to always know
that something is a pointer? When you later realize that it should
have been a reference, will you change the name?

Pointers are easily recognized as they are used in expressions like
*Name, or Name->Something.

::

Well, personally, I think that pointerness is part of the name. :) I
always use pCar rather than car, because the variable
isn't a car it's a pointer to a car and a p is shorter that suffixing it
with ptr.

I developed my convention prior to having clever IDEs, so some of the
guidelines are there to help me find the variables. I still find it
convenient to know at a glance where things are coming from during the
maintenance phase of development, so I haven't adjusted it much.

I generally use camel case for naming items.

Variables start with a lower case letter (This rule includes any
prefixes as part of the variable name, thus car, m_Car, m_pCar, pCar etc
are all variables).

Members get prefixed with m_.

Statics are prefixed with an s_

Globals (if any) get prefixed with a g_.

Locals don't get a prefix because they should be declared nearby and are
easily found.

Functions, class names and constants start with a capital letter
(MaxSize, SomeClass).

I haven't really formed a strong preference for
namespaces. So far, I have kept them lowercase and sometimes with '_'s
instead of camel case. I suppose for consistency, I should prefer camel
case starting with a capital (like classes), but I create far fewer
namespaces than classes, so I haven't developed a strong preference.

Now for some anti patterns:

I really really don't like trailing '_'s or even prefixes because they
look horrible in actual code. Things like carptr_->engine_ just look
weird to me and so does _carptr->_engine. Just too many straight lines
in a row. But, that's just my opinion. :)

Hope that helps more than adds to the confusion.

joe
 
J

Jorgen Grahn

* Miguel Guedes:
So, I take it the moral of the story is: make up your own naming
convention and stick to them?

To some degree.

However, Java and C# have established naming conventions, and to keep
things as simple as possible it's a good idea to try to use as much as
practical of the same conventions in all three languages, e.g. [...]

Uh, some of us would hate having to conform to conventions from other,
inferior languages. Especially when we have no contact with them
whatsoever.

I feel that for C++, there may not be *one* convention, but there is
probably one in the part of the community where you live.

The Ellemtel rules used to be popular ten years ago, for example. I
cannot really recommend them today, but here they are:
http://www.doc.ic.ac.uk/lab/cplus/c++.rules/

Using myself as another example:

I like Stroustrup's books a lot (although TC++PL doesn't pretend to be
a manual of style). Also, Stroustrup comes from a Unix/C environment
and so do I. So I managed to assemble my style using bits & pieces
from TC++PL, K&R, the Unix APIs, and various bits of code I have read
and liked in the past.

This means, among other things, relatively little CamelCase and few
funny prefixes. And pragmatism.

/Jorgen
 
J

Jorgen Grahn

Personally I like camel-case better but
Bjarne Stroustrup claims that underscores have been proven to be more
readable.

Where? As I recall it, TC++PL tries to stay away from those issues; I
think he mentions somewhere that the code snippets look like they do
mostly to make the text around them readable.

That said, Stroustrup spent a lot of time at Bell Labs -- the world
capital of lower-case letters ;-)

/Jorgen
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Where? As I recall it, TC++PL tries to stay away from those issues; I
think he mentions somewhere that the code snippets look like they do
mostly to make the text around them readable.

That said, Stroustrup spent a lot of time at Bell Labs -- the world
capital of lower-case letters ;-)

It was in an interview I downloaded from the net. I think the link was
posted in here some time ago.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top