Search code examples
language-agnosticpreprocessorlanguage-design

Are preprocessors obsolete in modern languages?


I'm making a simple compiler for a simple pet language I'm creating and coming from a C background(though I'm writing it in Ruby) I wondered if a preprocessor is necessary.

What do you think? Is a "dumb" preprocessor still necessary in modern languages? Would C#'s conditional compilation capabilities be considered a "preprocessor"? Does every modern language that doesn't include a preprocessor have the utilities necessary to properly replace it? (for instance, the C++ preprocessor is now mostly obsolete(though still depended upon) because of templates.)


Solution

  • C's preprocessing can do really neat things, but if you look at the things it's used for you realize it's often for just adding another level of abstraction.

    • Preprocessing for different operations on different platforms? It's basically a layer of abstraction for platform independence.
    • Preprocessing for easily adding complex code? Abstraction because the language isn't generic enough.
    • Preprocessing for adding extensions into your code? Abstraction because your code / your language isn't flexible enough.

    So my answer is: you don't need a preprocessor if your language is high-level enough *. I wouldn't call preprocessing evil or useless, I just say that the more abstract the language gets, the less reason I can think for it needing preprocessing.

    * What's high-level enough? That is, of course, entirely subjective.

    EDIT: Of course, I'm only really referring to macros. Using preprocessors for interfacing with other code files or for defining constants is evil.