Javon Harper
Javon Harper

Reputation: 196

Setting flags before and after doing work

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?

Upvotes: 2

Views: 69

Answers (1)

zanegray
zanegray

Reputation: 768

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)

Upvotes: 1

Related Questions