Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:18:36+00:00 2026-06-07T00:18:36+00:00

I had a debate about macros and their readability. I think that in some

  • 0

I had a debate about macros and their readability.
I think that in some cases using macros can make the code shorter, more comprehensible and less tiring to read.

For example:

#include <iostream>

#define EXIT_ON_FAILURE(s) if(s != 0) {std::cout << "Exited on line " << __LINE__ << std::endl; exit(1);}

inline void exitOnFailure(int s, int lineNum) {
    if (s != 0) {
        std::cout << "Exited on line " << lineNum << std::endl; 
        exit(1);
    }
}

int foo() {
    return 1;
}

int bar(int a, int b, int c) {
    return 0;
}

int main() {
    // first option
    if (foo() != 0) {
        std::cout << "Exited on line " << __LINE__ << std::endl;
        exit(1);    
    }
    if (bar(1, 2, 3) != 0) {
        std::cout << "Exited on line " << __LINE__ << std::endl;
        exit(1);    
    }

    // second option
    EXIT_ON_FAILURE(foo());
    EXIT_ON_FAILURE(bar(1, 2, 3));

    // third option
    exitOnFailure(foo(), __LINE__);
    exitOnFailure(bar(1, 2, 3), __LINE__);

    return 0;
}

I prefer the second option here, since it’s short and compact and the caps lock text is just clearer and easier to read than camel case.

Is there anything wrong with this method, particularly in C++, or is it just bad (but acceptable) style?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:18:38+00:00Added an answer on June 7, 2026 at 12:18 am

    Macros are a very powerful feature of C/C++ and like all C features
    are pointed at your foot by default. Consider the following use of
    your macro:

    if (doSomething())
        EXIT_ON_FAILURE(s)   /* <-- MISSING SEMICOLON! OH NOES!!! */
    else
        doSomethingElse();
    

    Does the else belong to the if in the statement or the if
    created by expanding EXIT_ON_FAILURE? Either way, the behaviour of
    one missing semicolon is entirely unexpected. If EXIT_ON_FAILURE()
    were a function, you’d get a compiler error. In this case, you’d get
    code that compiles but does the wrong thing.

    And that’s the problem with macros. They look like functions or
    variables but they aren’t. A badly-written macro is the gift that
    keeps on giving. Every use of the macro is a potential subtle bug and
    every change to a macro threatens to introduce logic errors into code
    that you didn’t touch.

    In general, you should avoid macros unless absolutely necessary.

    If you need to define a constant, use a const variable or an enum.
    A good compiler (which you can get for free) will turn them into
    literals in the resulting executable just like a #define’d constant
    but it will also handle type conversions the way you expect and will
    show up in the debugger’s symbol table.

    If you need something like an inline function, use an inline function.
    C++ and C99 both provide them and most decent compilers (including the
    free ones) have done it as an extension for a long time.

    Functions force their arguments to be evaluated, unlike macros, so

    inline int DOUBLE(int a) {return a+a;}
    

    will only ever evaluate a once while

    #define DOUBLE(a) (a + a)
    

    will evaluate a twice. This means that

    x = DOUBLE(someVeryLongFunction());
    

    will take twice as long if DOUBLE is a macro than if it is a function.

    Also, I (deliberately) forgot to parenthesize the macro arguments, so
    this:

    DOUBLE(a << b)
    

    will give an entirely surprising result. You’d need to remember to
    write the DOUBLE macro as

    #define DOUBLE(a) ((a) + (a))
    

    In other words, you need to write a macro perfectly just to minimize
    the chances of shooting yourself in the foot. If you make a mistake,
    you’ll be paying for it for years.

    All that being said, yes, there are cases where a macro will make
    the code more readable. They are few and far between, but they
    exist. One of them is the assert macro which your code reinvents.
    It’s pretty common for complex systems to use their own custom
    assert-like macro to tie into the local debugging scheme, and those
    are almost always implemented with macros in order to get at __FILE__,
    __LINE__ and the text of the condition.

    But even then, this is how the typical assert is implemented:

    #ifdef NDEBUG
    #   define assert(cond)
    #else
    #   define assert(cond) __assert(cond, __FILE__, __LINE__, #cond)
    #endif
    

    In other words, the function-like macro expands into a function call.
    This way, when you call assert, the expansion is still pretty close
    to what it looks like and the argument expansion happens the way a
    you’d expect it to.

    And there are a few other uses. Basically, anytime you need to
    pass information to the program from the build process itself, it will
    probably need to go through the macro system. Even then though, you
    should minimize the amount of code that touches the macro and how
    much the macro does.

    One final thing. If you are tempted to use a macro because you think
    the code will be faster, be aware that this is the Devil talking. In
    the olden days, there may have been cases where converting small
    functions into macros gave a noticeable performance improvement. These
    days though:

    1. Most compilers support inline functions. Some even do it
      automatically to static functions.

    2. Modern computers are so fast that you almost certainly won’t notice
      the overhead of calling even a trivial function.

    Only if your compiler doesn’t do inline functions and you can’t just
    replace it with a better one and you’ve proven that function call
    overhead is a bottleneck can you maybe justify writing a few macros.

    Maybe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today, I've had some debate with my colleague about choosing data types in our
Had a good search here but can't see anything that gets my mind in
I had a weird issue arise after using CodeMaid to clean up my code.
Had a conversation about sitemaps with someone from marketing. It was stated that a
There seems to be some debate about whether JPEGs with alpha channels are valid
Had a question that I've often wondered about. Is it better to have multiple
I had a weird problem about GZip and I hope you can help me.
Had to deploy some PHP code onto a shared server with only PHP5.2.8. All
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
Had to download Command Line Tools from Apple to get make to work, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.