Saturday, March 26, 2011

Java Conditional Compilation.

For a "main stream" programming language, Java has a few aggravating shortcomings. The lack of conditional compilation is one of them.

Most internet forums' response to a user asking about it is something along the lines, "well, actually, you shouldn't do that anyways". or "architect your solution so that you don't need conditional compilation". Utter BS. That is not helpful. One of my pet peeves is when someone is asking for idea on how to do 'X' and the only advice someone has to offer is to revert to a generalized dogma about why doing 'Y' is the only way do anything.

Of course it's a language aspect that done poorly could lead to really unworkable code. Anyone who has worked on a large legacy C++ application knows what I'm talking about.

C# mitigates some of the mess that can result with it's strict limitations on it's "pseudo preprocessor" macro system, and that typically works nicely. Actually, C# has one of the most powerful and easy to use systems for conditional compilation I've seen. There are at least three complementary systems that come to mind, straight #if , the [Conditional("")] construct, and "partial methods".

But I digress...

In certain architecture/design circumstances conditional compilation is the simplest solution. When I want to add a few diagnostic data members to my vector struct for use in diagnostic builds, I want to be turn that off or on in one place, if I accidentally use those members in production code, I want the compiler to catch it. I do not want to have to re-architect a class hierarchy for trivial diagnostic, debugging or experimental concerns.

There are third party solutions, and in my Android App, I will most likely explore those, but a small language feature could have made it a no-brain-er, and could have reduced complexity, and that is a good thing.


1 comment:

  1. Of course, if you _are_ looking for ways to emulate conditional compilation in Java, I suppose my rant is not helpful :) I promise I will post my findings once I come up with something I'm satisfied with.


I welcome you're thoughts. Keep it classy, think of the children.