Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 516
Earlz Avatar asked May 30 '10 21:05

Earlz


People also ask

Which language does not have a preprocessor?

Java does not include any kind of preprocessor like the C cpp preprocessor. It may seem hard to imagine programming without #define, #include, and #ifdef, but in fact, Java really does not require these constructs.

Which language has a preprocessor?

In some computer languages (e.g., C and PL/I) there is a phase of translation known as preprocessing. It can also include macro processing, file inclusion and language extensions.

Why do we need Preprocessors?

With CSS Preprocessor, you can add variables and functions brings a new dimension and scope to CSS which facilitates easier and efficient development. It also makes your code more organized and clean. CSS Preprocessors offers a special functionality of joining multiple stylesheets into one.

What is the need of preprocessor in C?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.


1 Answers

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.

like image 131
Oak Avatar answered Oct 04 '22 16:10

Oak