开发者

avoid a lot of if-else checks

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?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜