doru52911 2018-12-04 18:46
浏览 28
已采纳

遵循规则 - “if”语句需要调用一个函数而不是一个函数 - 我选择了三种方法

How to write code without if body and call a function instead for better readability. Each approach as I think has disadvantages 1. Unnecessary else word. I can complete this without else 2. A ternary operator used for returning value. It seems clean, but is it right using ternary operator without returning value? 3. Every time I need to repeat the keyword return. Seems not so clean; Unlike the 1. approach this approach has only 1 branch. And calling b() seems clean, unlike the 1 approach.

I'm stucking with this. I want to write clean code but I don`t know what to choose. Maybe do you know a better way than these 3 approaches? Or can you argue wich approach in what situation to use?

1.

 if ($contidition) {
        a();
    } else {
        b();
    }

2.

$contidition ? a() : b();

3.

if ($contidition) {
    a();
    return;
}

b();
  • 写回答

1条回答 默认 最新

  • dongying6896 2018-12-04 19:09
    关注

    I don't understand your question. If you want to put something in the if statement but not in the else statement you can just say:

    if a != b {print(a)} 
    

    You don't have to write else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?