Hacker News new | ask | show | jobs
by lostcolony 4453 days ago
Depends on the language/compiler/linker/preprocessor/interpreter/etc.

I'm not really a C developer, but since it's a lingua franca, GCC lets you pass in -D (name), which will define that passed in as a macro, letting you do stuff like

  #ifdef DEBUG
  //Do debug stuff
  #endif
so that if you run gcc with -DDEBUG, you will have DEBUG defined and set to 1 (you can also do DEBUG=val and similar I believe), and if you don't, it will be removed.

Java, I don't believe javac has a similar compiler mechanism; you either need a hardcoded global value in the source code

  public static final boolean DEBUG = true;
and you can do similar

  if DEBUG { (...) }
and then for your prod compile you set DEBUG to false and recompile everything; or, you can swap out implementations of a particular class, and have everything code to the interface (all that typical Java IoC dependency injection joy).

For any other language, consult your documentation. If there's nothing else, and the language allows you to pass in arguments into the runtime executable, you could always do something really ungainly like accept an argument for 'environment', and have code that executes differently based on that. It's a minimal cost (since any given run will lead to that particular switch always executing the same way, I'd wager your CPUs branch prediction is going to effectively make it free, but even if it doesn't, it's minimal impact), and it lets you execute the code differently based on what environment you specify you're in.