Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When starting to learn one new language, we have to choose perfect code style such as types or file name's first letter should be uppercase or lowercase, etc. But when I started to learn C++, I did not find any official common code style as I did for Java.

A popular one is Google's, yet someone told me, we should keep the style of our company. But I think that no one will work in one company for a whole life, so it could be better for him to choose a popular common code style if some company's code style is ugly.

How should I choose a code style for C++? Is there any official, common, popular code style that is generally used? Can I write C++ code with the style I use for Java? I think Java's code style is perfect.

share|improve this question

closed as primarily opinion-based by gnat, Joris Timmermans, MichaelT, Dynamic, ratchet freak Nov 29 '13 at 14:17

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

6  
more important the choosing a good style is being consistent in following it –  ratchet freak Nov 29 '13 at 9:48
4  
Coding style is probably one of THE most opinion-based topics in programming, and C++ adds a notch or ten. I'm with @ratchetfreak - the only definitive statement you can make is "be consistent". –  Joris Timmermans Nov 29 '13 at 10:18
    
Why do people think this is opinion based? I would close it as duplicate, if I could find one –  BЈовић Nov 29 '13 at 11:02
    

2 Answers 2

We use a code style adapted from Sun's Style for Java, because it fits well to our style of object-oriented programming (It's crucial that the style fits you as the team!). But there are some elements in C++, that aren't covered, such as initialisation lists.

We use automatic formatting, whereas its configuration is part of the development process, and under discussion when necessary. We started with a very small set of rules and refined them iteratively. Artistic Style does a good job for this growing style: it keeps things unchanged if there is no rule yet.

I personally think it's important to know the reasons behind the rules. For example

if (aCondition) {
    some;
    statements;
} else {
    anotherStatement;
}
  • space around keywords. Keywords are the base of structure and therefore need enough space
  • The indentation is 4 chars, because we don't nest deeply
  • the opening braces are attached partly for space reasons, partly because
  • else is attached, so it gets indented 1/2 column to its if, this makes structure obvious (the same goes for try and catch)

Another important thing is naming, because you write for reading. Names are well chosen and mostly not abbreviated, the time required for this is a good investment for the future. Relatively long names (10-20 chars) like (volume, soundFile, position) are fine for us, since we don't like long expressions, functions, or classes, and, as already stated, we avoid deep nesting.

share|improve this answer

So how to choose code style for C++? is there any official common popular code style existed?

Assign someone a task to write a document. Then review and modify it until whole team (or at least people who decide) agree that the document is fine.

If you are lazy, you can find a code style online, and blindly follow it.

share|improve this answer
    
There are official Java Coding Conventions (see Wolf's answer for a link). In practice, these are followed quite closely, and other Java styles are just minute variations such as uncuddling the elses, or adding rules for topics which the JCC does not cover. –  amon Nov 29 '13 at 10:24
    
@amon Ok, I don't code in java, so I had no idea there were official coding style. Removing the nonsense –  BЈовић Nov 29 '13 at 10:27
3  
Most languages actually have an official style guide (I know them for Java, Perl, Python, Go, Scala), but not all of them are enforced by the community. Other languages have very strong informal conventions (e.g. indentation in Lisps). C++ is different here, also because different people will use different subsets of the language. A “one size fits all”-style guide does match C++'s philosophy. –  amon Nov 29 '13 at 10:40

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