简体   繁体   中英

Generalization of code

I'm working on a small Real-Time OS project and I ran into a small problem.

void printOutput(void)                                       
{                                                             
   Log_info2("Fib(%d) = %d", FIB_N , cur) ;               
   System_printf("Fib(%d) = %d", FIB_N , cur) ;                   
   System_flush() ;                                                            
}

As you can see this function calls both Log and printf functions with the exact same arguments. My question is - is there any way to make this piece of code more generalized?

I'd appreciate your ideas. Thank you

A small simplification is all you might need.

void printOutput(void)                                       
{
   char const* format = "Fib(%d) = %d";
   Log_info2(format, FIB_N , cur);
   System_printf(format, FIB_N , cur) ;
   System_flush();
}

Further simplification to reduce code duplicaion.

void printOutput(void)
{
   char message[200];  // Make it large enough for your neeeds.
   sprintf(message, "Fib(%d) = %d", FIB_N , cur);

   Log_info2(message);
   System_printf(message) ;
   System_flush();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM