Wednesday, October 11, 2017

Flagging in large code bases. ( Wise Coding )

Simple usage of Code management with the help of macros in C.

Make the code look wiser than putting more code.

The macros by far are the good way of substitution operators. While macros are mostly used for setting some constant values which can be used through out the code, until or unless it has been undef`ed ( aka undefining a macro ).

There is quite a lot of use of these macros in the real time embedded system code development.


Here is the simple program that explain:

Lets say I have 2 functions which need to be built for different versions.
 function A()
function B().

In a deliverable I want a customer to use function A to get some data.
But I want to put some other logic that computes the same way function A works, but function B is more better than and you want to compare the function B vs funtion A.

program A:
#define CODEA 1
#define CODEB 0

void functionA( void )
{
...
...
...
}


void functionB(void)
{
....
....
...
}


void main ( void )
{
...
...
#if CODEA
  functionA()
#elif CODEB
functionB
#endif
....\
...

}



Program B:

#define CODEA 0
#define CODEB 1


void functionB(void)
{
....
....
...
}


void main ( void )
{
...
...
#if CODEA
  functionA()
#elif CODEB
functionB
#endif
....\
...

}




It does not make sense in the smaller programs like the ones described above.

But these are pretty use ful when you have larger code bases, which span 20000 files.


And you want the program to work with specific functionality for Program A but program B wants some functionality that B offers.

These are useful when the same functions are used called at multiple places in the code base.


Example:

Lets say we have a timer call back function for Program A that set a portA.2 high is Program A.
But Program B uses other chip but uses the same code base, but need PORTC.1 to be high as a result of the timer call back.


This flagging can also be applied to specific code fragments in code.

taking the example above:


void HLT_TimerCallback( void *AppPtr )
{
         sometmr =0 ;
#if CODEA
   PORTA.A1 = 1;
#elif CODEB
  PORTB.B1 = 1'
#else
   PORTC.C1 = 1;
#endif

}