13

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.

  • 6
    In the end you could call it a form of Hungarian Notation as well (i.e. embedding info about type/usage in a name), which is usually frowned upon. Hence: good question. – stijn Nov 28 '14 at 19:54
  • 2
    @stijn: Yes, but do you know why it's frowned upon? Back in the day, when everyone used a text editor to write their programs, it was useful to know the type by looking at the identifier. But today, you can just hover over the identifier in any modern IDE and it will tell you the type. – Robert Harvey Nov 28 '14 at 20:42
  • 4
    @RobertHarvey Except that Hungarian notation is also eschewed in communities that don't have powerful IDEs, or rather, mostly use plain text editors. – user7043 Nov 28 '14 at 20:59
  • 1
    @RobertHarvey No, I'm aware of both ("apps Hungarian" and "systems Hungarian"), and in my experience neither is common in, for example, Python and Rust. – user7043 Nov 28 '14 at 21:47
  • 3
    @RobertHarvey Are you saying Hungarian notation only ever existed in C, C++ and VB? Regardless, my point was to question the reason you give for it being frowned up upon (that it's redundant only since we have IDEs that tell you the type) since that reason doesn't apply to languages that don't have such IDes and, by your reasoning, should benefit from Hungarian. That Python is dynamically typed makes no difference, in fact it should enhance the reasoning since typically the programmer knows the types when writing the code, but they can't be inferred automatically. – user7043 Nov 29 '14 at 11:02
5

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.

  • 2
    I'll see your RAII, and raise you an SFINAE. – Robert Harvey Nov 28 '14 at 20:33
  • 5
    I could see how it might be useful to distinguish standard types from UDT's. You shouldn't change the code in a standard library unless you have a really good reason, so seeing the lower case indicates that you don't have to go look at the code because, well, it's in the standard library. – Robert Harvey Nov 28 '14 at 20:40
  • 4
    @delnan: Yes. That's nothing more than an appeal to authority on the part of "Many people". If "Many people" want to get over here and discuss their reasoning, they're free to do so, and until then, they have nothing to add. FTR, I use uppercase for UDTs too... but also for primitives whenever I have cause to need to alias them. – DeadMG Nov 28 '14 at 21:56
  • 2
    @RobertHarvey: That assumes that you can't recognize Standard types on sight. Or by mousing over them in your environment and seeing the "std::". Or by using Go To Definition or whatever. So basically, it doesn't apply. – DeadMG Nov 28 '14 at 21:58
  • 1
    I always thought the purpose was to distinguish from standard types, before the advent of namespaces. Nowadays, it's just a relic of old thinking (a "best practice" that stopped being best some time ago). FWIW, in my personal projects, I use small letters snake_case for my classes and namespaces and have had no issues - or confusion - caused by this yet. – utnapistim Dec 1 '14 at 12:52
3

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.

2

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.

  • IMHO this doesn't answer the question in the heading. Maybe add "in this respect, naming the standard types with small letters was only for backwards compatibility with cstdlib". – Vorac Aug 7 '17 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.