Home > OS >  Better way to handle timing related action
Better way to handle timing related action

Time:07-09

How can I write in a better way(in terms of coding styles, or more advanced level of programming ) this:

void main() {
  static int count = 0;    
  while (1) {
    count  ;
    if (count % 4 == 0) {
      doThis();
      count = 0;
    } else {
      doThat();
    }
  }
}

This of course works correclty but it seems a little bit a kind of "noob" coding :)

CodePudding user response:

This looks pretty basic and simplified to me:

#include <stdio.h>

void doThis() {
    printf("doThis\n");
}
 

void doThat() {
    printf("doThat\n");
}
 
 
void main() {
    static int count = 0;    
    while (1) {
        count = (count 1)%4;
        (count? doThat : doThis)();
    }
}

CodePudding user response:

You can formate the code style that will reduce some extra lines and arrange brackets properly. You can use a one-liner if/else.

  • Related