Coding conventions in C++

Asked

Viewed 80 times

-3

Hello, I’m learning C++, and I’m looking for best practices for creating variables, functions, classes and so on. I’ve researched the subject on the Internet, but I couldn’t find anything to talk about directly, or when I talked about it was a subject of a certain old point, which was probably obsolete (I believe), however I went to read codes in C++ to try to understand how the style of the language is, but I realized that this is very varied, as for example the variable int windows_size can also be written as int windowsSize, or the function int get_windows_size() (excessive use of "_" to give "space" makes me think that this style of programming comes from C) to be written as int GetWindowsSize() or even int getWindowsSize() and this further increased my uncertainty about how to program (or try) within the language patterns, so I come here to ask what the correct C++ coding pattern is, from now on, I thank everyone who answers

  • Depending on who program, the default library tends to follow snake_case.

  • I see snake_case a lot in various codes, I believe it is the most adopted by C programmers++

  • I’ve been researching in English and found a document from Google that says the style of code she uses in her projects, I believe I will follow the style too, I found it very useful and concise in my opinion, the link from it

  • I like to use the Camelcase java.

  • If there was a pattern, tools like Clang-Tidy would be obsolete. Ideally, it is constant, the rules themselves are not important.

1 answer

0


I don’t think it has any strictly defined standard for C++, but as pointed out by Maniero in comment to his question, the standard library tends to follow snake_case, and even if there are, several libraries and frameworks adopt different standards which may end up mixingin your project.

If you are working on a project for some organization, it is very beneficial that all involved follow the pattern set for that specific project, to maintain readability and cohesion.

Now, if you are working on a personal project, this is your choice, as pointed out by Adriano Siqueira also in comment to your question, it may be a matter of taste.

The adoption of a style guide (as with projects made in Angular, also from Google) preset as you yourself mentioned can also be of great help.

Although it’s not a style guide, these are the best C coding guidelines++ that you can follow, since they were elaborated (also) by the language creator himself and show how to produce conformant code with the latest standards released as C++11, C++14, C++17, the current C++20 and the future C++23.

  • Soon after I found the style guide of Google in my searches, I found the one of the language creator himself, and really the two help to maintain the cohesion of the code, I will try to follow a little of the two, because they are great guides, anyway, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.