1

I'm moving back from Python to C++ for my next project.
I know why I shouldn't and I know why I should. Never mind that debate.

C++ conventionForVariablesIsCamelCaseAndI'mHavingTroubleAcceptingIt as, at_least_for_my_eyes_it's_less_readable_than_the_lower_case_underscored_convention.

Did any of you encounter an article or information claiming programmers should adopt lower_case_underscored convention and abandon the camelCase, even in C++ projects? Or perhaps research that shows that one is indeed scientifically more readable than the other?

Community
  • 1
  • 1
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

4 Answers4

9

If coding in a team, consistency is probably more important than personal preference; team members should not have to context switch between reading Joe's code and reading Jane's code. Equally if coding academic assignments, course style like team style should be adhered to (for right or wrong), simply because the person awarding the marks will be used to reading that, and you need to extract the best possible mark for your work!

I would suggest otherwise that one convention has little advantage over another. CamelCase does provide a certain efficiency of symbol length.

Clifford
  • 88,407
  • 13
  • 85
  • 165
4

If it's a private project, use whatever naming convention you feel comfortable with and helps you be productive. Just bear in mind that it is generally a good idea to be in keeping with the overall "style" oft he language / usual practise since any samples / examples etc. will usually use that style, making integration etc. easier and less "jarring".

If it's a public project it's probably better to use the conventions since that's easier for other people to work with.

If it's corporate, do whatever your corporate guidelines mandate. If there aren't any, then I'd do the same as for a public project.

One thing I'd personally say about CamelCase, is not to get completely hung up on it, and apply common sense for the sake of readability. For example, I've often seen abbreviations in camel case names written as part upper / part lower, which I think really hurts readability. In cases like this I'd always go for the more readable option. So, for example I'd always write:

private string targetURL;

rather than

private string targetUrl;

But, this is just personal preference.

xan
  • 7,440
  • 8
  • 43
  • 65
2

There is no agreed upon convention for C++ naming among C++ programmers, however lower case with underscores is used in both the C++ standard library and in boost.


As for the coding standards document you linked...

A link to a random company's coding standards that use "Common practice in the C++ development community" as a justification for their standards, yet provide no citation for that statement smells like a false appeal to authority in order to justify the preferences of whoever wrote the document.

JoeG
  • 12,994
  • 1
  • 38
  • 63
2

CamelCase vs underscores: Scientific showdown describes a single scientific study which found:

Considering all four hypotheses together, it becomes evident that the camel case style leads to better all around performance once a subject is trained on this style. Training is required to quickly recognize such an identifier.

But then the page disagrees that their conclusions are valid. :)

It also has two polls of visitors to the site, both of which are 50/50 in favor of each style.

endolith
  • 25,479
  • 34
  • 128
  • 192
  • Don't forget to read the follow-up study which I link to in a comment: http://www.computer.org/portal/web/csdl/doi/10.1109/ICPC.2010.41 ;p It attempts to solve some of the problems with the original study which I blogged about. – Steven Jeuris Feb 21 '12 at 23:28