-1

I've been doing this with my code for quite a while and I wanted the opinions of the experts on if this is a good idea or not.

#include <iostream>

#define OutPut std::cout <<

int main()
{
  OutPut "This sentence should appear in console.";
  return 0;
};

Is this macro safe to use in terms of shorting things like cout, cin, and the like? Could it potentially cause more harm than good down the line?

  • 3
    This could be easily broken, and I can see no advantage. I'd prefer not to obfuscate that with a preprocessor macro. – πάντα ῥεῖ Jun 18 '19 at 19:03
  • Is there really any good reason to use it? It doesn't make the code any more clear, it might even make it worse. Additionally, there's always the chance that different compilers may have trouble with it. So I personally see no benefit and all risk. – anonmess Jun 18 '19 at 19:04
  • 4
    For the saving of a typing a few characters, you make your code unreadable. –  Jun 18 '19 at 19:06
  • 4
    The preprocessor is The Devil. – Eljay Jun 18 '19 at 19:07
  • And it doesn't get much of my sympathy. – user4581301 Jun 18 '19 at 19:12
  • I would consider this much more harmful than just `using std::cout` (which isn't that bad) and if character count bothers you, `cout<<` is the same length as `OutPut`. Does this macro cause more harm than good? Seeing as, in my opinion, it causes zero good and non-zero harm, it definitely does cause more harm than good. – François Andrieux Jun 18 '19 at 19:12
  • While this is opinion based, it;'s the same sort of opinion-based as the old ads about 9 out of 10 Dentists agreeing that brushing teeth was good. I always wondered what was wrong with that 10th Dentist. – user4581301 Jun 18 '19 at 19:15
  • 1
    If you need to use macros for speeding up typing (such as making abbreviations), I highly recommend you take a keyboarding class. – Thomas Matthews Jun 18 '19 at 19:35
  • you can use the preprocessor turn almost anything into valid c++ code or vice versa, but why dont you write c++ code in the first place? It may feel convenient at the moment, but consider that nobody but you is familiar with `OutPut` while literally everybody is familiar with `std::cout <<`. Code is mainly to be read by others. May sound rude, but if you are too lazy to write code you better dont write code ;) – 463035818_is_not_an_ai Jun 18 '19 at 19:38

1 Answers1

0

No it is not a good idea. A C++ programmer expects to read C++ and not some kind of made up language that someone decided to concoct.

Using the Preprocessor with C++ is almost always a poor idea as there are almost always better alternatives.

See Why are preprocessor macros evil and what are the alternatives?

See https://softwareengineering.stackexchange.com/questions/249767/is-it-a-good-idea-to-define-me-this

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106