2

I am working on a moderately large C file where I need to do processing for 3 different exclusive conditions. (if condition 1 is present do this, if 2 is present do something else and likewise). So, at many places in that file, I need to do if/else checks - which looks dirty. I have some 50 places where I am doing these checks.

Is there a better way, to make code look cleaner?

hari
  • 9,439
  • 27
  • 76
  • 110

4 Answers4

5

If the conditions are really exclusive then I'd start with three separate functions, one for each process. Break any common code out into their own functions that you can call from the three process functions. The only conditional left should be where you decide which of the three process functions to call.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Thanks Bill. But here in my case, there is a lot more common code between these 50+ condition checks. Also, wouldn't this be a lot of function calls? Appreciate your help here. – hari Jan 13 '11 at 21:08
  • @hari: Yes, if you have 50+ condition checks now, then it's possible that you'll end up with 50+ function calls. The resulting three top-level functions should be pretty linear though, since they'll just be a sequence of calls. If you give the functions descriptive names this might be the most readable code you've ever seen. :) – Bill the Lizard Jan 13 '11 at 21:12
2

there are two main options, and they are dependent on the problem your code solves

1.) if your condition is the same throughout your c code file, meaning the condition does not change, but code must behave differently in several places.

i.e.

/* prepare */
if(cond == 1){ 
  /*prepare 1 */
}elseif(cond == 2){
  /*prepare 2 */
}

/* run */
if(cond == 1){ 
  /*run 1 */
}elseif(cond == 2){
  /* run 2 */
}

in this case you should just refactor things to be under a single condition. i.e.

/* process and run */
if(cond == 1){ 
  /* process 1 */
  /* run 1 */
}elseif(cond == 2){
  /* process 2 */
  /* run 2 */
}

if you have a changing condition throughout the code. i.e.

cond = DEFAULT_COND /* = 1 */;
/* prepare */
if(cond == 1){ 
  cond = prepare_1();
}elseif(cond == 2){
  cond = prepare_2();
}

/* run */
if(cond == 1){ 
  /* run 1 */
}elseif(cond == 2){
  /* run 2 */
}

in this case your code is too complex to simply refactor because the cond variable at the time that the "run" code is evaluated may have been changed by the "process" code, in this case, but only in a case like this. will you be unable to refactor the code into a single condition.

Fire Crow
  • 7,499
  • 4
  • 36
  • 35
1

If I understand correct - you check the same conditions over and over again?

If so I would have this check only once, and if this requires duplicating code - put this code in functions.

Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
1

Several avenues to consider:

  • A switch statement might help to make your code more readable if those conditions refer (or can be made to refer) to the same variable.

  • If the controlled statements are simple (e.g. an assignment), you might be able to #define a macro for the if ... else if ... else ... construct.

  • If the common parts among the cases are relatively small, it would be best to define three different functions. This may cause a moderate amount of code duplication, though.

  • If the common parts are larger, move them to functions and define a separate function for each part. If the "part" functions become too complex, you could use macros instead, although that increases the size of the produced object code. Then create a separate function for each of the three cases, using these "part" functions or macros.

The last two avenues will reduce the conditional checks to one, with minimal or no code duplication.

PS: By "common parts" I mean those parts of the code that are executed regardless of which of the three cases is actually active.

thkala
  • 84,049
  • 23
  • 157
  • 201