A trick to avoid excessive macros

A trick to avoid excessive macros

A quick trick to avoid excessive macros. Excessive macros is bad for the code base as it makes it really hard to edit code after some point, and hard to fix the compilation errors.

One quick way to avoid is to define variables using macros. This way, we can pass in compile time flags, no redundant code are generated, and no need to use MACROS everywhere.

This is especially useful for flags that do verbose, timing and all that.

#ifdef VERBOSE

bool verbose = true;


bool verbose = false;



In the code, you can do

if (verbose) {




Instead of the full macro

#ifdef VERBOSE






Or another way might be

#ifndef VERBOSE
# define VERBOSE 0
const int verbose = VERBOSE;


And yes, you can pass in runtime flags with variables. The problem is that you need to have a lot of options defined in main, and you have to generate the timing code. It is probably more useful for large projects. But for medium size projects that wants good performance, this trick can come in handy.

This entry was posted in C++, Uncategorized. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s