Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good naming convention for vars, methods, etc in C++? [closed]

I come from a the Objective-C and Cocoa world where there are lots of conventions and many people will say it makes your code beautiful! Now programming in C++ I cannot find a good document like this one for C++.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

Standard C++ probably does not have something like above but I hope I can stick to some other SDK or APIs (like Microsoft's(?),etc) conventions.

I hope you can provide me with some links.

like image 287
nacho4d Avatar asked Sep 14 '10 05:09

nacho4d


People also ask

What is the naming convention for variables in C?

Rules for naming a variableA variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be.

What should be the naming convention for methods?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

Which of the following is correct naming convention in C?

Naming Conventions rules for Variables and Methods (Functions) are: It should begin with an alphabet. There may be more than one alphabet, but without any spaces between them. Digits may be used but only after alphabet.

What are the 3 rules that need to be considered while naming a variable?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)


1 Answers

Do whatever you want as long as its minimal, consistent, and doesn't break any rules.

Personally, I find the Boost style easiest; it matches the standard library (giving a uniform look to code) and is simple. I personally tack on m and p prefixes to members and parameters, respectively, giving:

#ifndef NAMESPACE_NAMES_THEN_PRIMARY_CLASS_OR_FUNCTION_THEN_HPP #define NAMESPACE_NAMES_THEN_PRIMARY_CLASS_OR_FUNCTION_THEN_HPP  #include <boost/headers/go/first> #include <boost/in_alphabetical/order> #include <then_standard_headers> #include <in_alphabetical_order>  #include "then/any/detail/headers" #include "in/alphabetical/order" #include "then/any/remaining/headers/in" // (you'll never guess) #include "alphabetical/order/duh"  #define NAMESPACE_NAMES_THEN_MACRO_NAME(pMacroNames) ARE_ALL_CAPS  namespace lowercase_identifers {     class separated_by_underscores     {     public:         void because_underscores_are() const         {             volatile int mostLikeSpaces = 0; // but local names are condensed              while (!mostLikeSpaces)                 single_statements(); // don't need braces              for (size_t i = 0; i < 100; ++i)             {                 but_multiple(i);                 statements_do();             }                      }          const complex_type& value() const         {             return mValue; // no conflict with value here         }          void value(const complex_type& pValue)         {             mValue = pValue ; // or here         }      protected:         // the more public it is, the more important it is,         // so order: public on top, then protected then private          template <typename Template, typename Parameters>         void are_upper_camel_case()         {             // gman was here                         }      private:         complex_type mValue;     }; }  #endif 

That. (And like I've said in comments, do not adopt the Google Style Guide for your code, unless it's for something as inconsequential as naming convention.)

like image 146
GManNickG Avatar answered Sep 22 '22 07:09

GManNickG