-2

Goal

In the end, I want to know if using #define is bad for your code, and why.

Code

#include <iostream>
using namespace std;

#define favouriteNumber 20;

int main()
{
    int number = favouriteNumber;
    cout << number;
}
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

1 Answers1

-1

According to Stroustrup it's particularly "bad" for defining constants because the compiler can't check types then.

On the other hand, if one line of macro saves you 20 lines of explicit code, some people will certainly agree that it's useful even if it's inherently unsafe. That's because writing more code usually implies higher probability for mistakes.

oliver
  • 2,771
  • 15
  • 32
  • May I ask why the downvote? – oliver Mar 03 '18 at 17:27
  • 1
    Maybe it's because you answered a question that shouldn't be answered. It was clearly opinion based. – Nicol Bolas Mar 03 '18 at 17:29
  • 1
    It is far from being clearly opinion based. It's state of the art. I was only trying to help. If somebody doesn't like the question, please downvote that. – oliver Mar 03 '18 at 17:31
  • "*almost anyone will agree*" - weasel words. – melpomene Mar 03 '18 at 17:33
  • Oh lord forgive me for using "weasel words". :-D – oliver Mar 03 '18 at 17:34
  • The problem isn't the "weasel words" themselves, it's just that they make it clear that your answer is opinion based. – melpomene Mar 03 '18 at 17:37
  • I have already changed it. Let me remark however that opinion can be based on experience and then suppressing it is hypocritical... at least in my opinion. ;-) – oliver Mar 03 '18 at 17:39
  • 2
    @melpomene Is it really "opinion based" that writing more code implies more errors? I can't see anything in this answer that i not objectively true tbh. – Galik Mar 03 '18 at 17:40