Google Coding Style 重点

C++代码

参见Google cpp style


Background


One way in which we keep the code base manageable is by enforcing consistency

注意一致性




Header Files


The #define Guard

应在文件头中使用

#ifndef FOO_BAR_BAZ_H_
#define FOO_BAR_BAZ_H_
...
#endif  // FOO_BAR_BAZ_H

define guard wiki详解


Header File Dependencies

如果不需要访问某个类的函数,则不要引入这个类


Names and Order of Includes

标准顺序为:

C library, C++ library, other libraries' .h, your project's .h.




Scoping


局部变量


声明注意

声明和定义的写法注意

int i;
i = f();      // Bad -- initialization separate from declaration.
int j = g();  // Good -- declaration has initialization.


声明位置的注意

// Inefficient implementation:
for (int i = 0; i < 1000000; ++i) {
  Foo f;  // My ctor and dtor get called 1000000 times each.
  f.DoSomething(i);
}

Foo f;  // My ctor and dtor get called once each.
for (int i = 0; i < 1000000; ++i) {
  f.DoSomething(i);
}



结构体应尽量少用,只有数据时才用结构体

Use a struct only for passive objects that carry data; everything else is a class.


声明顺序

public: before private:, methods before data members (variables), etc.

start with its public: section, followed by its protected: section and then its private: section.


段内顺序

Typedefs and Enums
Constants (static const data members)
Constructors
Destructor
Methods, including static methods
Data Members (except static const data members)


函数长度应尽量短

 If a function exceeds about 40 lines, think about whether it can be broken up without harming the structure of the program.




命名

Constant Names
Use a k followed by mixed case: kDaysInAWeek.




注释


法律声明和作者信息

版权声明

//Copyright 2008 Google Inc

许可版本

//Apache 2.0, BSD, LGPL, GPL

作者信息

//Original Author
//Other author who change the file

文件内容注释

在法律声明和作者信息下,描述文件主要内容

A .h file will describe the classes that are declared in the file with an overview of what they are for and how they are used.

A .cc file should contain more information about implementation details or discussions of tricky algorithms. 


函数声明内容

What the inputs and outputs are.
For class member functions: whether the object remembers reference arguments beyond the duration of the method call, and whether it will free them or not.
If the function allocates memory that the caller must free.
Whether any of the arguments can be NULL.
If there are any performance implications of how a function is used.
If the function is re-entrant. What are its synchronization assumptions?


如果传空指针,最好加声明或者定义名称时暗示是何意思

bool success = CalculateSomething(interesting_value,
                                  10,
                                  false,
                                  NULL);  // What are these arguments??

versus:

bool success = CalculateSomething(interesting_value,
                                  10,     // Default base value.
                                  false,  // Not the first time we're calling this.
                                  NULL);  // No callback.

Or alternatively, constants or self-describing variables:

const int kDefaultBaseValue = 10;
const bool kFirstTimeCalling = false;
Callback *null_callback = NULL;
bool success = CalculateSomething(interesting_value,
                                  kDefaultBaseValue,
                                  kFirstTimeCalling,
                                  null_callback);


Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.

// TODO(kl@gmail.com): Use a "*" here for concatenation operator.
// TODO(Zeke) change this to use relations.




格式


行长度

Each line of text in your code should be at most 80 characters long.


80 characters is the maximum.


Exception: if a comment line contains an example command or a literal URL longer than 80 characters, that line may be longer than 80 characters for ease of cut and paste.


Exception: an #include statement with a long path may exceed 80 columns. Try to avoid situations where this becomes necessary.


Exception: you needn't be concerned about header guards that exceed the maximum length.


条件语句

This is not allowed when the if statement has an else:

// Not allowed - IF statement on one line when there is an ELSE clause
if (x) DoThis();
else DoThat();

如果有else语句那么if和else必须都添加{},如果没有else ,if语句可不添加{}


switch语句

 If the default case should never execute, simply assert:

switch (var) {
  case 0: {  // 2 space indent
    ...      // 4 space indent
    break;
  }
  case 1: {
    ...
    break;
  }
  default: {
    assert(false);
  }
}

Empty loop bodies should use {} or continue, but not a single semicolon.

while (condition) {
  // Repeat test until it returns false.
}
for (int i = 0; i < kSomeNumber; ++i) {}  // Good - empty body.
while (condition) continue;  // Good - continue indicates no logic.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编码风格是Verilog设计中非常重要的方面,它直接影响着代码的可维护性和可读性。以下是关于Verilog编码风格的一些建议: 1. 缩进和对齐:合理的缩进和对齐可以增加代码的可读性。建议使用4个空格进行缩进,并对相关的代码进行对齐,以便于理解代码结构。 2. 命名规范:命名应该具有描述性,能够准确反映信号或模块的功能。遵循一致的命名规范,可以使代码更易于理解和维护。可以使用驼峰命名法或下划线命名法。 3. 注释:适当的注释可以帮助其他人理解你的代码。在代码的关键部分添加注释,解释代码的功能、用途和设计思路。 4. 模块化设计:将代码分成多个小模块,每个模块只负责特定的功能。这样做可以增加代码的可复用性和可维护性。 5. 参数化设计:使用参数化的方式设计模块,可以提高代码的灵活性和可扩展性。通过将一些常量参数化,可以在实例化模块时灵活地调整参数的值。 6. 模块接口:在设计模块时,定义清楚模块的输入和输出接口,并确保适当的信号命名和位宽匹配。 7. 错误处理:编写代码时要考虑到可能出现的错误情况,并采取适当的错误处理机制,例如添加状态机或发送错误提示信号。 8. 使用阻塞和非阻塞赋值:在赋值时要使用适当的赋值操作符,阻塞赋值(=)用于组合逻辑,非阻塞赋值(<=)用于时序逻辑。 9. 适当使用常量和枚举:对于不会改变的数值,应该使用常量来定义。对于有限的状态集合或选项,可以使用枚举来增加代码的可读性。 10. 代码复用:适当的代码复用可以减少重复编写相似代码的工作量。可以使用模块、宏定义、函数等方式重新使用已有的代码块。 总的来说,编写Verilog代码时,需要注重代码的可读性、可维护性和灵活性,合理的编码风格将大大提高代码质量和工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值