Ada 95 Quality and Style Guide | Chapter 8 |
8.4.5 Conditional Compilationguideline
Structure reusable code to take advantage of dead code removal by the compiler. example
------------------------------------------------------------------------ package Matrix_Math is ... type Algorithm is (Gaussian, Pivoting, Choleski, Tri_Diagonal); generic Which_Algorithm : in Algorithm := Gaussian; procedure Invert ( ... ); end Matrix_Math; ------------------------------------------------------------------------ package body Matrix_Math is ... --------------------------------------------------------------------- procedure Invert ( ... ) is ... begin -- Invert case Which_Algorithm is when Gaussian => ... ; when Pivoting => ... ; when Choleski => ... ; when Tri_Diagonal => ... ; end case; end Invert; --------------------------------------------------------------------- end Matrix_Math; ------------------------------------------------------------------------rationale
Some compilers omit object code corresponding to parts of the program that they detect can never be executed. Constant expressions in conditional statements take advantage of this feature where it is available, providing a limited form of conditional compilation. When a part is reused in an implementation that does not support this form of conditional compilation, this practice produces a clean structure that is easy to adapt by deleting or commenting out redundant code where it creates an unacceptable overhead.
This feature should be used when other factors prevent the code from being separated into separate program units. In the above example, it would be preferable to have a different procedure for each algorithm. But the algorithms may differ in slight but complex ways to make separate procedures difficult to maintain.
caution
Be aware of whether your implementation supports dead code removal, and be prepared to take other steps to eliminate the overhead of redundant code if necessary.
< Previous Page | Search | Contents | Index | Next Page > |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC |
Appendix | References | Bibliography |