Search code examples
cprogramming-languagespreprocessor

Why other languages don't support something similar to preprocessor directives like C and its descendant?



I wonder why other languages do not support this feature. What I can understand that C / C++ code is platform dependent so to make it work (compile and execute) across various platform, is achieved by using preprocessor directives. And there are many other uses of this apart from this. Like you can put all your debug printf's inside #if DEBUG ... #endif. So while making the release build these lines of code do not get compiled in the binary.
But in other languages, achieving this thing (later part) is difficult (or may be impossible, I'm not sure). All code will get compiled in the binary increasing its size. So my question is "why do Java, or other modern compiled languages no support this kind of feature?" which allows you to include or exclude some piece of code from the binary in a much handy way.


Solution

  • The major languages that don't have a preprocessor usually have a different, often cleaner, way to achieve the same effects.

    Having a text-preprocessor like cpp is a mixed blessing. Since cpp doesn't actually know C, all it does is transform text into other text. This causes many maintenance problems. Take C++ for example, where many uses of the preprocessor have been explicitly deprecated in favor of better features like:

    • For constants, const instead of #define
    • For small functions, inline instead of #define macros

    The C++ FAQ calls macros evil and gives multiple reasons to avoid using them.