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.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP