什么是C ++中的vars,方法等的一个好的命名约定? [英] What is a good naming convention for vars, methods, etc in C++?

查看:120
本文介绍了什么是C ++中的vars,方法等的一个好的命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个Objective-C和Cocoa世界,有很多约定,很多人会说它使你的代码漂亮!
现在在C ++中编程我找不到一个像C ++这样的好文档。



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

标准C ++可能没有像上面的东西,但我希望我可以坚持一些其他的SDK或API(如微软(?)等)约定。



我希望您能为我提供一些链接。

解决方案

做任何你想要的,只要它的最小,一致,不违反任何规则



我个人认为Boost风格最简单;它匹配标准库(给出统一的代码),很简单。我分别在 m p 上分别加上成员和参数的前缀:



<$ c $ p> #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>

#includethen / any / detail / headers
#includein / alphabetical / order
#includethen / any / remaining / headers / in
//(你永远不会猜到)
#includealphabetical / order / duh

#define NAMESPACE_NAMES_THEN_MACRO_NAME(pMacroNames)ARE_ALL_CAPS

命名空间lowercase_identifers
{
class separated_by_underscores
{
public:
void for_underscores_are()const
{
volatile int mostLikeSpaces = 0; //但是本地名称被压缩

while(!mostLikeSpaces)
single_statements(); //不需要大括号

for(size_t i = 0; i <100; ++ i)
{
but_multiple(i);
statements_do();
}
}

const complex_type& value()const
{
return mValue; //与这里的值没有冲突
}

void value(const complex_type& pValue)
{
mValue = pValue; //或here
}

protected:
//更多的是public,更重要的是
//所以顺序:public on top,然后保护然后私人

模板< typename模板,typename参数>
void are_upper_camel_case()
{
// gman在这里
}

private:
complex_type mValue;
};
}

#endif


(和我在评论中说的一样,不要为您的代码采用Google风格指南,除非它是作为无关紧要的命名约定。)


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.

解决方案

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.)

这篇关于什么是C ++中的vars,方法等的一个好的命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆