美文网首页
1.33、建议:做条件判断时,应使用Golden Path模式

1.33、建议:做条件判断时,应使用Golden Path模式

作者: 半升多瑙河 | 来源:发表于2016-07-19 15:34 被阅读41次

说明:The goal here is to make the code on the left margin to be the “expected”

code execution path and the code that is indented to be the exception. Consistency

in this area is important to code readability.示例:

良好的风格:

-(void)someMethod{

if (![someOther boolValue]) return;

//Do something important }

不良好的风格:

-(void)someMethod{

if ([someOther boolValue]) { //Do something important}}

================================

良好的风格:

-(void)someMethod{if ([someOther boolValue]) { //Do something important return;

}

//Do something else important }

不良好的风格:

-(void)someMethod{

if ([someOther boolValue]) { //Do something important

} else {//Do something else important

}}

================================

例外:

-(void)someMethod

{if ([someOther boolValue]) {

//Do something important } else {

//Do something else important }

//Do this no matter what }

相关文章

  • 1.33、建议:做条件判断时,应使用Golden Path模式

    说明:The goal here is to make the code on the left margin t...

  • Elixir 简明笔记(三)--- 模式匹配

    函数式语言的一大特点就是使用模式匹配来做条件判断。Elixir甚至可以使用模式匹配来替代 if else 这些传统...

  • js控制结构

    做判断(if语句) if语句是基于条件成立才执行相应代码时使用的语句。 语法: if(条件) { 条件成立时执行代...

  • 多表查询

    多表查询时,容易产生笛卡尔积,应避免在无任何条件时做多表联合查询。 内连接 等值连接查询条件中使用=作为连接条件 ...

  • IF函数

    用来完成非此即彼的判断。 用法: =IF(判断条件,符合条件时返回的值,不符合条件时返回的值) 示例: 使用IF函...

  • 卫语句

    阿里巴巴Java开发手册——“多层条件语句建议使用卫语句、策略模式、状态模式等方式重构”。 卫语句 把复杂的条件表...

  • 惰性加载函数

    1.常规 缺点:每次调用都会执行条件判断,可避免 2.改进,只在加载时做判断 缺点:虽避免了判断,但如果从未使用过...

  • (快速入门)条件判断+循环+Map+Set

    条件判断 JavaScript使用if () { ... } else { ... }来进行条件判断。 多行条件判...

  • Swift补基础之Selector、条件编译、编译标记、NSOb

    在swift中使用条件编译比较直接 例如 :在debug模式和release模式编译不同代码 在swift中判断i...

  • JavaScript 条件判断

    条件判断 JavaScript使用 if () { ... } else { ... } 来进行条件判断。例如,根...

网友评论

      本文标题:1.33、建议:做条件判断时,应使用Golden Path模式

      本文链接:https://www.haomeiwen.com/subject/ydwmjttx.html