Coding style 有没有更好的方法来处理这个问题?

Coding style 有没有更好的方法来处理这个问题?,coding-style,if-statement,nested,Coding Style,If Statement,Nested,我已经遇到过这种特殊的模式好几次了,为了让它正常工作,必须声明flag变量有些烦人。有没有一种更简单的方法来安排我没有看到的代码 flag = true if x.is_okay? some_stuff_that_needs_x_to_be_okay if some_condition_that_depended_on_x actually_important_stuff flag = false end end if flag do_something_whe

我已经遇到过这种特殊的模式好几次了,为了让它正常工作,必须声明flag变量有些烦人。有没有一种更简单的方法来安排我没有看到的代码

flag = true
if x.is_okay?
  some_stuff_that_needs_x_to_be_okay
  if some_condition_that_depended_on_x
    actually_important_stuff
    flag = false
  end
end

if flag
  do_something_when_important_stuff_did_not
end

这里有一种不使用flag变量的方法。如果当重要的东西没有做的时候,
做某事是一个简单的语句,比如方法调用,这是合理的

if x.is_okay?
  some_stuff_that_needs_x_to_be_okay
  if some_condition_that_depended_on_x
    actually_important_stuff
  else
    do_something_when_important_stuff_did_not()
  end
else
  do_something_when_important_stuff_did_not()
end