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.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料