重构 if-else 控制流。

huangapple go评论82阅读模式
英文:

Refactor if-else control flow

问题

我遇到了以下的代码片段,当我浏览代码时:

if(a || b){
    if(a) {
        doSomething();
    }
    doSomethingElse();
} else {
    throw new Exception("blah");
}

我在想如何重构这段代码以提高可读性(或者它已经是最佳形式了?)。以下是我的第一次尝试:

if(!a && !b){
    throw new Exception("blah");
}
if(a){
    doSomething();
}
doSomethingElse();

这看起来更好吗?

英文:

I came across the following snippet while going through the code:

if(a || b){
    if(a) {
        doSomething();
    }
    doSomethingElse();
} else {
    throw new Exception("blah");
}

I was wondering how can I refactor this code for better readability (or it's already in optimal shape?). Below is my first attempt:

if(!a && !b){
    throw new Exception("blah");
}
if(a){
    doSomething();
}
doSomethingElse();

Does this look better?

答案1

得分: 10

只有条件 a || b 执行 doSomethingElse

if (a) {
    doSomething();
} 
if (a || b) {
    doSomethingElse();
} else {
    throw new Exception("blah");
}

当对条件进行否定,即 !a && !b 时可能会令人困惑,我更喜欢正向条件。

英文:

Only condition a || b execute doSomethingElse:

if(a) {
    doSomething();
} 
if (a || b) {
    doSomethingElse();
} else {
    throw new Exception("blah");
}

When negating condition as !a && !b it can get confusing, I prefer positive conditions

答案2

得分: 3

你也可以这样做:

if (a) {
    做某事();
    做其他事情();
} else if (b) {
    做其他事情();
} else {
    抛出新异常("blah");
}
英文:

You can also do it as

if(a) {
    doSomething();
    doSomethingElse();
} else if(b) {
	doSomethingElse();
} else {
	throw new Exception("blah");
}

答案3

得分: 2

只需按照逻辑过程进行。您可以检查a,然后执行doSomething()doSomethingElse(),否则如果b,则执行doSomethingElse(),否则抛出Exception

if (a) {
    doSomething();
    doSomethingElse();
} else if (b) {
    doSomethingElse();
} else {
    throw new Exception("blah");
}
英文:

Just go through the logical process. You can check for a, then doSomething() and doSomethingElse(), else if b, then doSomethingElse(), else Exception:

if (a) {
    doSomething();
    doSomethingElse();
} else if (b) {
    doSomethingElse();
} else {
    throw new Exception("blah");
}



</details>



# 答案4
**得分**: 2

你可以进行两次分开的检查

```python
if (a):
  doSomething()
elif (a or b):
  doSomethingElse()
else:
  raise Exception("Exception")

这种格式对我来说很容易阅读 重构 if-else 控制流。

英文:

You can do two separate checks instead:

if(a){
  doSomething();
}

else if(a || b){
  doSomethingElse();
}

else throw new Exception(&quot;Exception&quot;);

This format is well readable to me personally 重构 if-else 控制流。

答案5

得分: 1

在我看来,如果在你的if语句中,不是比较值,而是将它们进行比较并将结果存储在一个有关上下文信息的有用名称的变量中,代码会变得更加可读。

假设"a"或"b"是一个"有效"值。那么代码会像这样:

boolean isValid = a || b;
if (isValid) {
    if (a) {
        doSomething();
    }
    doSomethingElse();
} else {
    throw new Exception("blah");
}

这样你就知道你正在检查"是否有效"的条件。

英文:

In my opinion, a code becomes more readable if instead of comparing the values in your if statement, you compare them and store the result in a variable with a helpful name that gives you some information about the context.

Let's say that being "a" or "b" is being a "valid" value. Then it would look like that:

boolean isValid = a||b;
if(isValid){
    if(a) {
        doSomething();
    }
    doSomethingElse();
} else {
    throw new Exception(&quot;blah&quot;);
}

That way you know that you're checking the condition of "being valid".

huangapple
  • 本文由 发表于 2020年8月4日 17:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63243621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定