Question

Although here I will refer specifically to C++ and Bjarne Stroustrup's naming conventions, in principle, I've seen that people use somewhat similar rules for other languages here and there.

So, the basic idea is that one should be able to distinguish standard types from user-defined types while reading the code. For instance, Bjarne Stroustrup suggests that one uses

an initial capital letter for types (e.g., Square and Graph)

which, taking into account that

The C++ language and standard library don't use capital letters

allows achieving the goal mentioned above.

But why do we need to do so? What can be the purpose of distinguishing standard and user-defined types?

I could not find any Bjarne Stroustrup's reasoning on that matter, and besides, I myself think in diametrically opposite way. :D I know, I know, "Who am I to dispute Stroustrup?" But, listen, a bunch of the C++ language features, e.g. operator overloading, serve the purpose to allow user-defined types a similar level of syntactic support as standard types. And then all this is baffled by a different naming discipline...

P.S. Not to mention that often one word is not enough to name a class and an underscore-separated word that starts with a capital letter looks so foreign.

Was it helpful?

Solution

There is absolutely no purpose or benefit in such. One of the goals of C++ was to treat UDTs and primitives interchangably, and whilst they haven't entirely succeeded, this is one area where you don't have to differentiate.

When it comes to naming, Stroustrup is nuts, and this is a scientifically-proven fact.

OTHER TIPS

Naming conventions are about supporting human (i.e. programmer and maintainer) comprehension of the code.

UDTs can be specified so that declaration of variables, initialisation, expressions, and statements work on them differently than is the case for standard types. For problem finding, it is useful for the maintainer to have some cue that some section of code might do funky things (e.g. the implementation of a user-defined integral type used within some function might have a flaw in how it does addition).

There are many ways of providing such cues (comments, design specifications, etc). The advantage of naming conventions is that they are present in the code, whereas comments can be omitted, out of date, etc.

One reason I use capitalized words for types, is to distinguish variables from types. This allows to declare a variable with, apart from the capitalization, the same name:

Foo foo;
Graph graph;

This can be useful for classes of which only one instance is used in each context, e.g. configuration settings.

Licensed under: CC-BY-SA with attribution
scroll top