C++ – Using #define for Easier Typing of Repeated Code

ccoding-standardscoding-style

Is there any view on whether using the #define to define full lines of code for simplifying coding is good or bad programming practice? For example, if I needed to print a bunch of words together, I'd get annoyed typing

<< " " <<

To insert a space between words in a cout statement. I could just do

#define pSpace << " " <<

and type

cout << word1 pSpace word2 << endl;

To me this neither adds or subtracts from the clarity of the code and makes typing slightly easier. There are other cases that I can think of where typing will be much easier, usually for debugging.

Any thoughts on this?

EDIT: Thanks for all the great answers! This question just came to me after doing a lot of repetitive typing, but I never thought there would be other, less confusing macros to use. For those who don't want to read all the answers, the best alternative is to use the macros of your IDE to reduce repetitive typing.

Best Answer

Writing code is easy. Reading code is hard.

You write code once. It lives for years, people read it a hundred times.

Optimize code for reading, not for writing.

Related Topic