Qt/C/C++工程推荐使用下面代码规范:
- 代码采用C/C++11标准,尽量使用智能指针,尽量不使用裸指针(QT中可以使用QScopedPointer)
- 函数和变量命名使用骆驼式命令法,采用动宾语法,例如printEmployeePaychecks(). C++头文件用.hpp后缀, 源文件用.cpp后缀, C头文件用.h后缀, 源文件用.c后缀. 文件的命名使用全小写并下划线分割,如a_b_c.cpp
- 尽量使用Qt5新的信号和槽连接方式,信号和槽的命名不加signal和slot前缀,用动作和on动作方式,如信号clicked(),槽为onClicked().
- 关键代码需要有单元测试,非界面代码一般认为是关键代码
- 默认debug build前推荐使用静态代码检查工具(Cppcheck/Clang/VS)检查,编译时使用gcc sanitizer增加地址和未定义行为动态检查,
- 开启qtcreator插件beautifier中的代码格式化工具Clang-format, 风格选择文件, 请保存下面的Clang-Format 风格文件到.clang-format风格文件并放在代码根目录下.
- 代码注释方式采用Doxygen方式, 有需要的地方需要增加注释,注释采用中文。函数,类,结构声明都认为是有需要的地方, 注释放在要被注释的代码上一行,并单独一行,不提倡放在代码同一行末尾或下一行(除非是参数列表)。
- 在填入界面显示文字时,使用英文,通过Qt Linguist翻译成中文,便于以后多语言, 文件编码一律使用UTF-8,添加BOM
在绘制界面的时候,尽量使用layout和stretch来布局,尽量不要使用固定的大小,如固定10像素, 以便适应窗口大小变化.
- 不能直接提交代码,需要创建合并请求,并把合入请求分配给另一个同事,其他同事如果发现有问题,填写评论后给回提出合入请求的同事。问题都解决后分配给有合入权限的同事。
- 最终提交的代码必须通过服务器的编译, 并且执行测试没有发现错误和警告.
- 未说明的风格请参考Qt风格, https://wiki.qt.io/Coding_Conventions, https://wiki.qt.io/Qt_Coding_Style
版本的命名方式遵循[语义化版本 2.0.0](http://semver.org/lang/zh-CN/)
Clang-Format 风格文件
# Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
# This is the clang-format configuration style to be used by Qt,
# based on the rules from https://wiki.qt.io/Qt_Coding_Style and
# https://wiki.qt.io/Coding_Conventions
---
# Webkit style was loosely based on the Qt style
BasedOnStyle: WebKit
Standard: Cpp11
ColumnLimit: 100
# Disable reflow of qdoc comments: indentation rules are different.
# Translation comments are also excluded
CommentPragmas: "^!|^:|^=|^~"
# We want a space between the type and the star for pointer types
PointerBindsToType: false
# We want to break before the operators, but not before a '='
BreakBeforeBinaryOperators: NonAssignment
# Braces are usually attached, but not after functions or classes declaration
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
# The coding style does not specify the following, but this is what gives
# results closest to the existing code.
AlignAfterOpenBracket: true
AlwaysBreakTemplateDeclarations: true
# Ideally we should also allow less short function in a single line, but
# clang-format does not handle that
AllowShortFunctionsOnASingleLine: Inline
# The coding style specifies some include order categories, but also tells to
# separate categories with an empty line. It does not specify the order within
# the categories. Since the SortInclude feature of clang-format does not
# re-order includes separated by empty lines, the feature is not used.
SortIncludes: false
# Extra
AlignTrailingComments: true
网友评论