2011-07-11 9 views
0

私はこの特定のパターンを数回見つけました。これが正しく動作するためには、フラグ変数を宣言しなければならないのはやや面倒です。私が見ていないこのコードを整理する簡単な方法はありますか?これを処理するより良い方法はありますか?

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 

答えて

0

ここにフラグ変数がない方法があります。 do_something_when_important_stuff_did_notがメソッド呼び出しのような単一の単純なステートメントであれば合理的です。

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 
関連する問題