我来自一个在Objective-C和可可的世界里,有很多约定的,很多人会说,它使你的代码漂亮! 现在编程在C ++中,我不能找到一个很好的文件这样一个C ++。
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html
标准C ++可能不会有这样的事情上面,但我希望我能坚持到一些其他的SDK或API(如微软的(?),等等)的约定。
我希望你能为我提供一些链接。
Answer 1:
你想,只要其最小的,一贯的什么人,而且不违反任何规则 。
就个人而言,我发现了Boost样式最简单的; 它的标准库(给人一种统一的外观代码)相匹配,并且很简单。 我个人钉在m
和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>
#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
那。 (就像我在评论中已经说过了, 不采用谷歌风格指南为您的代码,除非它是无关紧要的命名约定的东西。)
Answer 2:
可能有许多命名约定有个人,辩论是滔滔不绝(无菌)哪个振奋风格来使用等等。
所以我有2点建议:
- 是一个项目中一致
- 不使用保留的标识符(以两个下划线任何东西,或开始用下划线后跟一个大写字母)
剩下的就是给你。
Answer 3:
事实上,我经常使用Java风格:PascalCase的类型名称,驼峰的函数和变量,CAPITAL_WORDS的预处理宏。 我宁愿在升压/ STL约定,因为你没有用的后缀类型_type
。 例如
Size size();
代替
size_type size(); // I don't like suffixes
这具有的StackOverflow代码格式化认识到额外的好处Size
的类型名称;-)
Answer 4:
我们按照此页面上列出的准则: ++ C编程风格指南
我也建议你阅读C ++风格的Misfeldt等元素 ,这是对这个话题非常的优秀图书。
Answer 5:
为了应对风格指南我建议拿起一个,并坚持它的更广泛的问题。
在谷歌的风格指南非常详细的/全面的,所以这是一个不错的选择。
只是命名一个共同的约定是:
-
camelCase
的所有名称(除常量) - 以大写开头,如果它是一类-
CamelCase
- 在常量
ALL_CAPS
以下划线分离词语 - 这样写变量函数-
camelCase()
- 成员变量前缀的“M” -
mCamelCase
这真的取决于你或者你也为工作/与人或你在学习大学。
对于以各种风格指南或他们背后的基本原理详细看看,看看由Andrew Hunt和大卫托马斯程序员修炼 。
Answer 6:
对于什么是值得的,Bjarne的Stroustrup的,C ++的原作者有自己喜欢的风格,描述如下: http://www.stroustrup.com/bs_faq2.html
Answer 7:
虽然很多人会建议或多或少严格的匈牙利命名法的变体( 吓人 !),命名建议,我建议你看一看谷歌的C ++编码指南 。 这很可能不是最流行的命名惯例,但至少它是相当完整。 除了完善的命名约定,有一些有用的指导有,但是大部分应该与盐粮采取(除禁令例如,和倾向从现代C保持距离++编码风格)。
虽然我个人喜欢STL和加速的极端低技术含量的常规风格)。
Answer 8:
一致性和可读性(自记录代码)是很重要的。 一些线索(诸如情况下)可以并且应该被用于避免碰撞,并以指示是否需要一个实例。
的我进入最佳做法之一是使用代码格式化器(的astyle和uncrustify是2个的例子)。 代码格式化可以摧毁你的代码格式 - 配置格式化,并让它做它的工作。 严重的是,忘了手动格式,并养成使用它们的做法。 他们将节省大量的时间。
如前所述,很描述与命名。 也,是非常具体与作用域(类类型/数据/命名空间/匿名的命名空间)。 在一般情况下,我真的很喜欢很多Java的共同书面形式 - 这是一个很好的参考和类似于C ++。
至于具体的外观/命名,这是类似于我用一个小样本(变量/参数被lowerCamel,这只是表明了该语言的功能部分):
/** MYC_BEGIN_FILE_ID::FD_Directory_nanotimer_FN_nanotimer_hpp_::MYC_BEGIN_FILE_DIR::Directory/nanotimer::MYC_BEGIN_FILE_FILE::nanotimer.hpp::Copyright... */
#ifndef FD_Directory_nanotimer_FN_nanotimer_hpp_
#define FD_Directory_nanotimer_FN_nanotimer_hpp_
/* typical commentary omitted -- comments detail notations/conventions. also, no defines/macros other than header guards */
namespace NamespaceName {
/* types prefixed with 't_' */
class t_nanotimer : public t_base_timer {
/* private types */
class t_thing {
/*...*/
};
public:
/* public types */
typedef uint64_t t_nanosecond;
/* factory initializers -- UpperCamel */
t_nanotimer* WithFloat(const float& arg);
/* public/protected class interface -- UpperCamel */
static float Uptime();
protected:
/* static class data -- UpperCamel -- accessors, if needed, use Get/Set prefix */
static const t_spoke Spoke;
public:
/* enums in interface are labeled as static class data */
enum { Granularity = 4 };
public:
/* construction/destruction -- always use proper initialization list */
explicit t_nanotimer(t_init);
explicit t_nanotimer(const float& arg);
virtual ~t_nanotimer();
/*
public and protected instance methods -- lowercaseCamel()
- booleans prefer is/has
- accessors use the form: getVariable() setVariable().
const-correctness is important
*/
const void* address() const;
virtual uint64_t hashCode() const;
protected:
/* interfaces/implementation of base pure virtuals (assume this was pure virtual in t_base_timer) */
virtual bool hasExpired() const;
private:
/* private methods and private static data */
void invalidate();
private:
/*
instance variables
- i tend to use underscore suffix, but d_ (for example) is another good alternative
- note redundancy in visibility
*/
t_thing ivar_;
private:
/* prohibited stuff */
explicit t_nanotimer();
explicit t_nanotimer(const int&);
};
} /* << NamespaceName */
/* i often add a multiple include else block here, preferring package-style inclusions */
#endif /* MYC_END_FILE::FD_Directory_nanotimer_FN_nanotimer_hpp_ */
Answer 9:
有许多不同的sytles /约定在编码C ++当人们使用。 例如,有些人喜欢使用分离首都(myVar的或MyVar的)的话,或使用下划线(my_var)。 通常情况下,使用下划线变量在所有小写(从我的经验)。
还有被称为匈牙利的编码风格,我相信这是由Microsoft。 我个人认为,这是在浪费时间,但它可能证明是有用的。 这是变量名被赋予短前缀如I或F暗示变量类型。 例如:INT iVarname,字符* strVarname。
它被接受,你最终一个结构/类名称与_t,从一个变量名区分开来。 例如:
class cat_t {
...
};
cat_t myCat;
它也普遍接受添加缀来指示指针,如pVariable或variable_p。
总之,实在是没有任何单一的标准 ,但许多。 关于命名变量你的选择并不重要,只要它是可以理解的,最重要的,是一致的。 一致性,连贯性,一致性! (请尝试输入三次!)
如果一切都失败了,google一下。
Answer 10:
这其实并不重要。 只要确保你的名字你的变量和函数描述性。 也一致。
比没出息看到这样的代码更糟糕:
int anInt; // Great name for a variable there ...
int myVar = Func( anInt ); // And on this line a great name for a function and myVar
// lacks the consistency already, poorly, laid out!
编辑:正如我的评论者指出,一致性,需要在整个团队得以维持。 因此,它不会不管你选择什么方法,只要是保持一致性。 有没有正确或错误的方法,但是。 每支球队我在工作已经有了不同的想法,我已经适应了这些。
Answer 11:
几乎没有简洁,你所提供的链接:但下面的章节14 - 24可以帮助:)嘿嘿
REF: http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586/ref=sr_1_1?ie=UTF8&s=books&qid=1284443869&sr=8-1-catcorr
文章来源: What is a good naming convention for vars, methods, etc in C++? [closed]