开发者

Variable naming convention according to scope (class, method, global...)

开发者 https://www.devze.com 2022-12-21 21:06 出处:网络
I soon started a small C++ project and elaborated some naming convention rules for variables, according to scope.

I soon started a small C++ project and elaborated some naming convention rules for variables, according to scope.

I made class members _prefixed_with_underscore and methods parameters suffixed_with_underscore_. I soon got neurotic about inventing naming conventions for everything, like global variables (ok, those might be important), inline global functions, to try to improve code readability.

I've read this question and its answers that answered some of my doubts about naming convention, specially about methods paramet开发者_如何学Pythoners names. Maybe having a_rule_like_this_ for parameters names might not be a good idea.

So, my question is, what naming conventions do you use for different "entities" when programming, specially for parameter names? Thanks.


There are many people who have created conventions - google for c++ naming convention

Also there are style guides which also suggest ways of programming.

You will probably not agree with all the points in one but being consistent in your project/team/company is the important one.

Also naming conventions will differ for different languages (and across OS) So don't use PHP suggestions for C++. (the preceding _ gives a problem fo C++) For C++ the most common conventions are that parameters are normal text but instance variables either end in _ or start with m_ (I prefer the former)


Never make any type of identifier that uses a prefixed '_'. The prefixed underscore is reserved for compiler implementers to use for their own neferious purposes. Meaning its perfectly valid for C/C++ compiler implementors to create macros, implicit local variables, namespaces, as long as they have at least one underscore.

If your code conforms to the c++ standard in every other way, the presence of prefixed underscores on identifiers mean that there is no expectation that it will compile on other compilers, or different versions of the compiler you are using right now.


Do not prefix identifiers with an underscore, as some compilers reserve them for their own non-standard purposes.

I like the naming conventions in the Google C++ style guide.


This is little more than a matter of opinion. So, here's mine.

  1. Don't sweat the small stuff
  2. Check out other people's opinions on the matter (which you're doing)
  3. It matters less which convention you choose, and much much more that you apply it consistantly
  4. Here's some of my convention:

    class MyGizmo
    {
    public:
      int DoIt();
    private:
      string myString_;
    };
    
    typedef vector<MyGizmo> MyGizmos;
    
    namespace somewhere
    {
      MyGizmos gizmos;
    };
    
    
    int MyGizmo::DoIt()
    {
      int retVal = 0;
      string strCopy = myString;
      retVal = strCopy.length();
      return retVal;
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号