When starting to learn one new language, we have to choose perfect code style su
ID: 655010 • Letter: W
Question
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.
Explanation / Answer
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.