Vikas Pathak

Software Developer

Conditional Compilation Framework



Technology: JAVA
Purpose: For IDM internal use only


This framework was designed to achieve feature toggling during development process. It provides the ability to disable unfinished code in the production.

The conditional compilation practice is used to optionally remove chunks of code from the compiled version of a class. It uses the fact that compilers will ignore any unreachable branches of code.



Problem Statement:
From a functional point of view, informational output can be classified as either:

Unfinished new feature code: If new version release date is near but some new functionality is not implemented yet then we need to delete unfinished code. But using conditional compilation we can prevent unfinished code from compilation.

Diagnostic messages: Designed to remain in the code even after release. These are what an as-released application writes out into its logs when executed in verbose mode.

Debug messages: Used by programmers in the development stage to debug their code. These correspond to the program's debug mode execution and would ideally be deleted from the release version of the code.


Therefore, we need a simple lightweight approach that can:
1. Ignore the unfinished code wrapped in the CC block if CC is set to true

2. Handle selection of classes to run in debug (or verbose) mode without having to recompile them
3. Leverage the compiler's built-in optimization capabilities to safely ignore debug code when compiling the release version of the application



Limitations: 

     We have used this approach profitably in a couple of modules. The limitations of this approach are as follows:
1. Every top-level class needs to implement the interface ConditionallyCompile.
2. It takes a little discipline on the part of programmers to enclose their unfinished/debug/diagnostic code appropriately, but the gains far outweigh this burden.


Conclusion:

     This approach can be used to conditionally compile any code, not just debug/diagnostic code. On those rare occasions when programmers do need to port their Java code, it could prove just as valuable to Java as conditional compilation is to C/C++.

What people say ?

Visit my blog