C++编码规范中的可靠性原则是确保代码的可读性、可维护性和稳定性,以下是几个小点及其例子:
避免使用全局变量:
如果需要多个变量在全局范围内使用,可用context(结构体/类)解决耦合性问题
// 不推荐的写法:使用全局变量,提高了函数耦合度,和代码维护难度
int globalVar = 10;
// 推荐的写法:在合适的作用域内使用局部变量
void function() {
int localVar = 5;
// 使用局部变量 localVar
}
//context
struct context{
int localVal = 5;
..
}
context *ctx = new