代码规范的意义: 使你花更少的时间写出符合阅读习惯和团队容易维护的代码,任何时候保持格式能够让自己专注于内容本身。
变量命令的格式和位置
代码块的组织
字符的间距
注释
让函数更简洁
重构代码
统一风格
- 变量声明靠近最近要使用的区域,尽量初始化,采用小驼峰(个人习惯),变量名更易读,对于常见词学习经典书籍的写法,例如iton, write(宾语, 定语, 宾语), pid等。
- 单个符号后空一格。
- 尽量简洁if((i = read(...))<0) return 1;
- #define EXPECT(c, ch) do { assert(*c->json == (ch)); c->json++; } while(0) //预处理器
- static void lept_parse_whitespace(lept_context* c) {
const char *p = c->json;
if (x == 1) { /*...*/ };
}//符号用法, 变量命名 LevelDB源码
static Status Open(const Options& options, RandomAccessFile* file, uint64_t file_size, Table** table);
class BlockBuilder;
Slice BlockBuilder::Finish() { // Append restart array for (size_t i = 0; i < restarts_.size(); i++) { PutFixed32(&buffer_, restarts_[i]); } PutFixed32(&buffer_, restarts_.size()); finished_ = true; return Slice(buffer_); }
void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) { assert(func != nullptr); CleanupNode* node; if (cleanup_head_.IsEmpty()) { node = &cleanup_head_; } else { node = new CleanupNode(); node->next = cleanup_head_.next; cleanup_head_.next = node; } node->function = func; node->arg1 = arg1; node->arg2 = arg2; }
static Iterator* BlockReader(void*, const ReadOptions&, const Slice&);