实现模式读书笔记四

  这些天继续阅读《实现模式》这本书。

  书中讲到了对称性: 也可以说是代码的一致性, 也就是方法的命名最好能做到一致, 符合人类的认知习惯, 比如有个add方法, 最好能提供个remove方法
  比如不一致的代码:

void process(){
input();
count++;
output();
}

  勉强一致的代码:

void process(){
input();
incrementCount(); // 方法名没有体现意图而是在说实现细节
output();
}

  最终一致的代码:

void process(){
input();
tally();
output();
}

posted @ 2021-01-05 12:55  Protect_Winter  阅读(57)  评论(0)    收藏  举报