是否有必要在每个 if 条件中写入 else 部分? [英] Is it necessary to write else part in every if condition?

查看:77
本文介绍了是否有必要在每个 if 条件中写入 else 部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问的问题可能已经结束,但我只想知道是否有必要在每个 if 条件中编写 else 部分.我的一位资深程序员跟我说你应该在每个 if 条件中都写 else 部分".假设我们没有条件写入 else 部分,那么我们应该怎么做?我认为这里会进行健康的讨论......

The question I asked might be closed, But i just want to know that is it necessary to write else part of every if condition. One of my senior programmer said me that "you should write else part in every if condition" . Suppose we have no condition for write in else part then what should we do ? I assume a healthy discussion will going on here....

推荐答案

这是一个可怕的想法.您最终得到以下形式的代码:

That's a horrible idea. You end up with code of the form:

if (something) {
    doSomething();
} else {
}

怎么会有人认为完全没有 else 更具可读性或可维护性,这超出了我的理解.这听起来像是那些有太多空闲时间的人制定的规则之一.尽快解雇他们,或者至少冷静而安静地离开:-)

How anyone could think that's more readable or maintainable that not having an else at all is beyond me. It sounds like one of those rules made up by people who have too much free time on their hands. Get them fired as quickly as you can, or at least move away calmly and quietly :-)

这篇关于是否有必要在每个 if 条件中写入 else 部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆