0

I normally use C#, not C++, which is the reason I asked this question.

I've seen it as normal in most C++ code to name constants in capitals with underscores, LIKE_THIS.

However, when I see C++ code (not that I've seen much of it), it looks immediately different from C#. Aside from the generic abundance of pointers and less formally-named/abbreviated stuff like WndProc and the Hungarian notation, half of the stuff is in all-capitals.

Even excluding constants, stuff like method names, physical structures/C++ things themselves (HRESULT), or sometimes even physical data types (BOOL) are in all-capitals with underscores.

This may/may not be common in C++ style (although I can't find it anywhere on google), why is this abundance of capitalization common in so many places? Or, where did it come from?

  • See e.g. http://stackoverflow.com/questions/1776291/function-names-in-c-capitalize-or-not – Fredrik Pihl Nov 27 '14 at 21:44
  • 6
    In C++, it's generally only macros that are written in `ALL_CAPS_WITH_INTERLEAVED_UNDERSCORES`. That's a C heritage: the preprocessor is error-prone and easy to abuse, so from the beginning, C programmers tend to use outstanding names for preprocessor macros. Apart from that, Hungarian notation et al. are not exactly "common C++ style". What you are talking about is the style of the WinAPI, which is "particular", at best. (I'd say "idiotic", but I suspect some Windows fans sneaking around here.) – The Paramagnetic Croissant Nov 27 '14 at 21:45
  • and http://programmers.stackexchange.com/questions/113556/is-there-a-common-capitalization-convention-in-c – Fredrik Pihl Nov 27 '14 at 21:45
  • 2
    Developers use whatever naming convention suits their fancy. Nobody cares, accept it and move on. – Captain Obvlious Nov 27 '14 at 21:46
  • 2
    The C++ bits from WinAPI don't use all-caps either (`IUnkown`). C style. – MSalters Nov 27 '14 at 21:48
  • 1
    This is not opinion-based. Why it is done is a well-established fact. Whether it's a good idea is opinion. Voting to reopen. – user207421 Nov 27 '14 at 22:24

4 Answers4

1

A widely used convention in C and C++ is to give all-caps names to macros and not to variables or functions. Such a style makes it easy to tell at a glance which identifiers are macros and which are not. This style doesn't appear in K&R, so I don't know how it originated, but it is fairly common today.

In C, macros are used to define constants. In C++, on the other hand, constants are usually const variables. Declaring constants as macros is C style, but many people write C++ code in C style.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • C++ inherited unscoped `enum`s from C, which is another often used method for defining constants if the preprocessor need not see them. – Deduplicator Nov 27 '14 at 22:02
1

ALL of your examples are style conventions specific to WinAPI and not C++, also the WInAPI is a C API not a C++ Api.

rafeek
  • 151
  • 4
1

I've seen it as normal in most C++ code to name constants in capitals with underscores, LIKE_THIS.

Usually, all-caps identifiers are used for macros, to avoid confusion with "real" compiler names. Since in earlier C macros were the only way to have constants usable e.g. in array sizes, some people adopt this style for all constants (or at least for enum values). Personally, I just leave them for macros, where it's actually useful to make a distinction.

However, when I see C++ code (not that I've seen much of it), it looks immediately different from C#. Aside from the generic abundance of pointers and less formally-named/abbreviated stuff like WndProc and the Hungarian notation, half of the stuff is in all-capitals.

That would be the Windows API style, which comes from the late eighties, so it's normal that it looks nothing like .NET.

Windows-related types are typically all-caps, I suspect that comes from the fact that they too were macros (although currently they are all typedefs) actually, I just checked the windows.h from the Windows 1 SDK, and they were typedefs even then, so probably it was some kind of stylistic choice; constants are still macros, though.

Hungarian notation was invented in Microsoft by Charles Simonyi, and was widely adopted (and misunderstood) internally in the early years of Windows.

As for the abbreviations, that's typical of older APIs (look at the C standard library or the POSIX APIs, they are even terser), and I think that it can be justified both by technical and practical arguments:

  • older compilers and linkers had length limits for identifiers, which means that they wouldn't distinguish identifiers differing only from - say - the sixth letter onwards; so, you want to keep the meaningful letters at the beginning (incidentally, this seems to have influenced the C standard library - I heard that strncmp, strncat & co. have the n in the middle because of this);
  • when you work on an 80x25 terminal, you don't want to continue to scroll to read a single line, so names like, say, AttachedPropertyBrowsableWhenAttributePresentAttribute don't strike you as a stellar idea;
  • also, people worked in C++ way before Visual Studio and IntelliSense. I'm sure that some editors had autocompletion even way back then, but in simpler editors you don't want to spend the day writing ListViewVirtualItemsSelectionRangeChangedEventHandler by hand.
Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 1
    Link for the second jumbo: http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewvirtualitemsselectionrangechangedeventhandler(v=vs.110).aspx – Deduplicator Nov 27 '14 at 22:09
0

Because somebody decided in about 1975 that identifiers declared in the preprocessor needed to be distinguished from identifiers declared in the C language proper.

I have never agreed, and I've ben using C since 1982.

user207421
  • 305,947
  • 44
  • 307
  • 483