How to deal with boolean or enum variables used to decide code flow?

  softwareengineering

I am working on C++ code that maintains several enum types. Say 3 to 4 enum types each with at least 4 different values. Plus the code maintains several boolean variables. The code is multi threaded and across different threads, it uses the state indicated by these enum variables and boolean values to drive code flow. This led to following:

  1. Code is littered with nested if-else blocks of depth 2 to 5 checking state and doing whats required to be done.
  2. The if-else-if conditions are composite involving ANDing / ORing these enum and boolean variables.
  3. Many functions have a check for these states for deciding whether it should proceed to do what they indicate to do by their names.

I guess this made the code somewhat less intuitive, bug prone and high maitenance. I know there are several threads discussing that using boolean variables for determining code behavior isnt a good idea. (For example, this and this.)

But, now I have following doubts:

Q1. Whether we use enum or boolean variables, above three points stays. So, is using enum variables also bad?
Q2. If the approach to check variable values for determining code flow itself is a bad idea, what is good idea / how should we approach this problem?

LEAVE A COMMENT