英文:
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")
这种格式对我来说很容易阅读
英文:
You can do two separate checks instead:
if(a){
doSomething();
}
else if(a || b){
doSomethingElse();
}
else throw new Exception("Exception");
This format is well readable to me personally
答案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("blah");
}
That way you know that you're checking the condition of "being valid".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论