几个简单代码片段-- Google C++ style guide

                       

欧洲杯,德国VS意大利。战车遇到浪漫之师,结果如何?
Who Cares!!!

开球之前,review一下近期写的代码,发现一些代码写的不是很规范。于是,重新温习一下 Google C++ style guide。

之前博客有过介绍,谷歌C++编程规范笔记,现在只是用几个简单的代码片段展示一下。

定义常量、宏定义、枚举:

// 使用下划线分隔#define FLAG_FOO 0x0// 要有括号#define FLAG_BAZ (0x1 << 3)// 对于常量,使用kconst int kStateFoo = 0;typedef struct linked_list LinkedList;// 枚举跟宏定义类似typedef enum {  MODE_FOO,  MODE_BAR,  MODE_BAZ,  MODE_QUX} Mode;// 枚举也可以像常量一样typedef enum {  kStateFoo,  kStateBar,  kStateBaz,  kStateQux} State;typedef struct sample {  int first_field;  bool second_field;  Mode mode;  State state;  struct sample *next;} Sample;
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

函数:

//注意第一个大括号的位置bool SampleEqual(Sample *self, Sample *other) {  // Local variables are lower_case and separated by underscores.  if (self == NULL && other == NULL) {    return true;  }  if (self == NULL || other == NULL) {    return false;  }  //多行  if (self->first_field == other->first_field &&      self->second_field == other->second_field &&      self->state == other->state &&      self->mode == other->mode &&      self->next == other->next) {    return true;  }  return false;}//多个参数Sample *SampleNew(int first_field,                  bool second_field,                  Mode mode,                  State state,                  Sample *next) {  Sample *sample = (Sample *) malloc(sizeof(*sample));  if (sample == NULL) {    return NULL;  }  memset(sample, 0, sizeof(sample));  sample->first_field = first_field;  sample->second_field = second_field;  sample->mode = mode;  sample->state = state;  sample->next = next;  return sample;}Sample *SampleClone(Sample *sample) {  if (sample == NULL) {    return NULL;  }  return SampleNew(sample->first_field,                   sample->second_field,                   sample->mode,                   sample->state,                   sample->next);}static void SampleDoSomethingWithALongName(    Sample *sample,    int parameter_with_a_long_name,    bool another_parameter,    int another_parameter) {  if (sample == NULL) {    return;  }  bool local_variable;  if (parameter_with_a_long_name == kStateFoo) {    local_variable = true;  } else {    local_variable = false;  }  sample->first_parameter += another_parameter;  sample->second_parameter |= local_variable;}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值