-4

I like to use macro to make me code faster, especially doing a for loop from 0 to n. And I did this way:

 #define For(i,n) for(int i = 0; i < n; i++)

Above code works fine, but I saw others do like this:

 #define For(i,n) for(int i = 0; i < (n); i++)

The only different is they add a parenthesis. I have no idea about why they wanna add the (), I get reply from the coder and it said the purpose is to make it more safer. But I didn't get further explaination from him, and I couldn't find relates thing on web.

I simply tried doing nested loop and it is fine if without adding ().

I'm curruntly adding () when I use macro for quite long time andit didn't shows any different.

I'm curious on the exact meanng of "safer programe" and I want to know more about the risk of using macro(like hard to debug?).

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 7
    Don't do that. But the general answer is that the expression passed to the macro can be more complicated than a simple name, and the parentheses ensure that it gets interpreted correctly. Often it doesn't matter, but when it does, the problem can be really hard to find. For example, with `#define mulby2(x) x * 2`, if you use `mulby2(1+1)` you'll get 3, not 4. – Pete Becker May 15 '23 at 14:43
  • 7
    One reason you probably got downvoted because most professional developers will not like the use of macros to create your own shorthand because the use of such macros reduce readability and also it's not like it saves that much at all. Your IDE could have helped you type the full code. – drescherjm May 15 '23 at 14:43
  • 2
    Consider what happens if you use the macro like `For(i, x < 0 ? 2 : 3)`. See [C macros and use of arguments in parentheses](https://stackoverflow.com/questions/7186504/c-macros-and-use-of-arguments-in-parentheses) – Guy Incognito May 15 '23 at 14:43
  • 7
    This is a serious anti-pattern: Creating your own quirky dialect of C that nobody else has ever seen, nor wants to use. Please don't. There are cases where macros *do* make life easier, this is not one of them. – tadman May 15 '23 at 14:44
  • 1
    "I like to use macro to make me code faster" — This does not make your code faster. What gave you the idea that it did?! – Konrad Rudolph May 15 '23 at 14:53
  • @KonradRudolph He think _he_ can code (the verb) faster using tricks like these. Not that it makes the code _run_ faster. Not that I agree but its consistent. – Mike Vine May 15 '23 at 15:00
  • 4
    `For(i,n++)` Muhuhahahahahahahahahahahaha! – user4581301 May 15 '23 at 15:01
  • https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es30-dont-use-macros-for-program-text-manipulation – Marek R May 15 '23 at 15:09
  • at the very least you should do it with function, not macro. – apple apple May 15 '23 at 15:16
  • I love abbreviation macros. Try this: `For("hamburger", 3.14159)`. Remember, macros are just substitutions and have no type checking. – Thomas Matthews May 15 '23 at 15:24
  • @ThomasMatthews I hope you are being cynical. Taking the bait : Convenience macros makes your code unreadable for other developers (who only studied C++ and not your macros/sources). Just ust standard C++ constructs! – Pepijn Kramer May 15 '23 at 15:30
  • 5
    Coding faster is NOT a professionals main goal. The goal is to write good, readable, typesafe C++ code that can easily be worked on by a TEAM. Code is written once, read/debugged/tested many times. The main effort should go into design, and then typing a few characters more is not making a big difference. – Pepijn Kramer May 15 '23 at 15:32
  • No, no, no, don't do that. Use a function or just type out the full code. Macros just obfuscate things and make your code hard to work on by others - and you throw type safety out the window as well. Considering how much time is going to be spent on reading, debugging and modifying the code (which your macro use is going to slow down quite a bit), saving a few keystrokes while initially writing it is nowhere *near* worth it. – Jesper Juhl May 15 '23 at 23:06

3 Answers3

5

They may use an expression n with an operator with lower precedence than <.

For(i, a ? 10 : 20)

The logic is broken, this w/o parentheses is: (i < a) ? 10 : 20, that is always true and leads to an infinit loop.

273K
  • 29,503
  • 10
  • 41
  • 64
4

if you used the first one

#define For(i,n) for(int i = 0; i < n; i++)

and did

 For(i, j + 12)

you would get

 for(int i = 0; i < j + 12; i++)

is that correct? Maybe - depends on the operator precedence,

 for(int i = 0; i < (j + 12); i++)

is always correct

pm100
  • 48,078
  • 23
  • 82
  • 145
1

Do not use/define this macro. Macros has been inherited form C and are source of many problems which are hard to understand and fix.

Here is example when this macro will fail and break code which do not uses this macro:

Imagine you are using library, which has something like this (what is valid):

struct MyContainer
{
public:
    int For(ArgType x) const;
};

Now everything works until in same translation unit you will include header file for this library and your nasty macro.

Suddenly this macro breaks library you like to use.

Note that problem is not how macro is used (as in other answers) but how it impacts unrelated code.

Disclaimer: This is not answer to question (why parenthesis are needed), but this do not fit as comment.

Marek R
  • 32,568
  • 6
  • 55
  • 140