|
|
13. All names should be written in English. 91. All comments should be written in English. |
37. File content must be kept within 80 columns. 92. Use // for all comments, including multi-line comments. |
I don't like this article. |
|
|
Write constants in all capitals |
Each header file should contain one class (or embedded or very tightly coupled classes) declaration only. |
Do not change a loop variable inside a for loop block. [...]it is highly confusion[...] |
All switch statements should have a default clause. |
methods/functions should not be longer than 5-9 statements. |
gcampton wrote: |
---|
you think it's ok for people to invent their own styles |
I'm not trying to push my ideas, I'm trying to push the ideas of the general populous of C++ programmers on new C++ programmers who need some guidance. |
Each header file should contain one class (or embedded or very tightly coupled classes) declaration only. |
helios wrote: |
---|
Can cause confusion with macros. |
|
|
Even those where the type has no more possible outcomes? Even when it doesn't matter whether any of the cases apply? Even when the variable has already been checked for correctness? |
|
|
That's just insane. And does that count only top-level statements, or the entire control tree? |
gcampton wrote: |
---|
As for Qemu, throw that garbage out and use a virtual box or vmware :) |
I didn't know you were the general populous of C++ programmers. My mistake. |
All switch statements should have a default clause. Even those where the type has no more possible outcomes? Even when it doesn't matter whether any of the cases apply? Even when the variable has already been checked for correctness? |
the comments that are directed specifically as my views when I have simply copied most of these from other sources are just getting a bit annoying |
Yes, the reason behind is maintainability |
rules [...] broken [...] Anarchy |
Suppose you're writing a simple substitution cipher where a letter is replaced by another. How would adding a default case improve maintainability when we don't want characters other than letters to be replaced? Would you suggest that every if must be accompanied by an else? |
All switch statements should have a default clause. Even in cases where the default can never be reached it should in some way notify the programmer that the switch should be changed or an exception has occurred. This allows for future changes. |
INTRODUCTION What is a set of standards and why am I reading this? The purpose of C++ standards is to define a C++ coding standard that should be adhered to when writing code. ISO 9000 and the Capability Maturity Model (CMM) state that coding standards are mandatory for any organization with quality goals. Standards provide indications aimed at helping programmers to meet the following requirements on a program: 1.be free of common types of errors 2.be maintainable by different programmers 3.be easy to read and understand 4.have a consistent style There are no hard and fast rules when it comes to programming style only the restrictions enforced by the compiler and the language, however it is important to be consistent. Below we are going to touch on some of the styles and guidelines that should be followed as much as possible but without trying to enforce any concrete rules. Most large companies have a set of standards that are enforced. If you are working for such a company you are being paid to write code subject based on what they want you to write, and in a style that must match up to their standards. The main reason being maintainability along with the above mentioned points. Please also note that no standard should be followed to the letter, in fact most sets will have clauses that allow you to disregard certain points. While you may be thinking that some of the standards you have seen seem completely silly, they do not always need to be followed, some can be disregarded and you can even create your own rules if the current project calls for such workarounds. This is of course company specific, but really as far as standards go they cannot possibly account for all situations which is why most have a clause, and the rest should have a clause. This will be at most a style guide, used to develop your own style but while still conforming to the needs of the community. It will cover 3 main topics: Naming, Coding, and Style. Naming 1. Meaningful names. Use English names that are self-descriptive. This goes for a number of different things, namely.....local variables, constants, functions, etc.. /// fix this... There are exceptions as there always are, those being: short-lived index variables... etc 2. Names of classes, functions and important variables should be chosen with care and should be meaningful. Abbreviations should be avoided, except in cases such as.... blah blah |
4.have a consistent style |
have a consistent style which leads to: a. being easy to read and understand b. being free of common types of errors c. being maintainable by different programmers. |
NAMING Names are the heart of programming. In the past people believed knowing someone’s true name gave them magical power over that person. If you can think up the true name for something, you give yourself and the people coming after power over the code. Don’t laugh! 1. Naming of files. The names of header and implementation files are generally the same with different suffix file type, they are also commonly named the same as the class. Some operating systems are non-case sensitive while others are case sensitive so for cross compatibility reasons you should stick to one style of case naming. This again falls under having a consistent style. //FIXME ;= other examples? 2. Naming of Classes, Functions, Variables, Constants, and Methods. Meaningful names are important, recall the above statement about power over a name. Usually functions and methods perform some type of action so naming the function in a way that describes what the function is doing is generally the best approach e.g. WriteDataToFile( ) rather than DataFile( ) Classes are often nouns. By making function names verbs and following other naming conventions programs can be read more naturally. Make every variable name descriptive, limit the use of abbreviations or letter-words. It’s worth writing words completely since it makes the code much more readable. Beware however that when trying to find a good name, you don’t end up with with something like ’the_variable_for_the_loop’, use a proper English word for it like ’counter’ or ’iterator’, but with a prefix attatched to be more descriptive like 'appleCounter'. English is a rich language and trying to find a correctly fitting word is important for code readability, cleanness and variation. Whenever in doubt, just use a thesaurus. Suffixes are sometimes useful: • Max - to mean the maximum value something can have. • Cnt - the current count of a running count variable. • Key - key value. For example: RetryMax to mean the maximum number of retries, RetryCnt to mean the current retry count. • Prefixes are sometimes useful: • Is - to ask a question about something. Whenever someone sees Is they will know it’s a question. • Get - get a value. • Set - set a value. For example: GetMaxOrangeCnt( ) Some standard variables are used for often recurring tasks. Below is a list of those that are accepted (for those that follow the first, are used as a backup within the same scope): i , j , k : integer counter (used in for loops) x, y, z : multiplication, or graph points. r , c , d , t : row, column, depth, time (used for array/pointer cells) it : STL-like iterator c : char (temporary) <type>_it : STL-like iterator of a certain type for differentiation amongst types tmp_<type> : eg. tmp_qstring, tmp_int, tmp_float for variables that are solely used for the storage of temporary intermediate values Notice the above I have listed both 'c' for column and 'c' for char, using both of these in the same scope would be a compilation error. C++ is a case sensitive language so sometimes it may be tempting to use the same name for a variable with only a change in a case. This is generally considered poor practise as it becomes difficult to read and debug. e.g.
However another acceptable way of naming global members is with a 'g' or global constants with a 'gc' as a prefix. Additionally Hungarian notation can be used and combined with descriptive English names. For more information see this link: http://en.wikipedia.org/wiki/Hungarian_notation For output of units of information, b for bits, B for bytes, and o for octets. SI prefixes are mostly used capitalized (K instead of k). If the prefix is a power of two, an i is added to it (KiB instead of KB). Otherwise, the prefix is a power of ten. The user should be aware of this difference where appropriate. |