Sometimes I find myself doing the following:
if (some_condition)
set_flag(true)
do_some_work();
// more work ...
if (some_condition)
set_flag(false)
It often feels hacked together. Is there a way I can refactor this so it is cleaner?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In my opion, I think that the suggested psuedo code is fine for several reasons:
1. Checking a flag rather than checking the condition over again should be more efficient
2. Additional abstraction / refactoring may only cause more of a headache down the road and seems to be non-value-added work that could better be used somewhere else
3. Using flags is usually readable if they have meaningful names (or method names in this case)