Why separate compilation is a good thing

I was talking with a friend about why I hate C++ (it’s actually a complicated tolerate/hate relationship), and mentioned that templates don’t have separate compilation. “That’s not really a big deal”, my friend replied. I remember a time when I thought the same thing. So what if I have to recompile some other stuff when I change a file? Projects of the size I work on take under a minute to compile from scratch anyway. And that will become even less of an issue as computers get faster.

The real benefit to separate compilation is not about speed. It means that whether code compiles is independent of the code that depends on it, or in other words, type safety is modular. Imagine if in mathematics theorems were only true sometimes, depending on how they were used. Those are pretty crappy theorems, because you have to know the proof in order to use them (I know I have used theorems whose proofs I haven’t understood!).

And C++ templates are indeed non-modular. Rewriting the body of a function but not its type signature can cause code to fail to compile. You can imagine how irritating this would be if you’re a library author (or even a library user).

It’s for this reason that I like FRP, because its behavior is definitional: the semantics of a value (signal or event) only changes when the code that created it changes, but not when code that uses it changes. So even the runtime of an FRP program is easier to reason about than the compile time of C++ (watch as I make sweeping incorrect generalizations!).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s