For example printf
instead of cout
, scanf
instead of cin
, using #define
macros, etc?
"Macros are unsafe, have no type checking and should be avoided whenever possible. An alternative to macro is inline function for some use cases." He is right that a macro doesn't respect a namespace. That's just a caveat of using one.
One major problem with #define is that it is outside of the language itself and therefore is not limited to a given scope. You will replace dItemName anywhere in the translation unit, in all namespaces, classes, functions, etc. Save this answer.
Generally, new is not such bad practice, and it's not a bad idea. The answer you linked recommends removing the new() s to address a compile-time error: You can't pass a pointer to a function that doesn't expect one! If you did away with "raw pointer" in C++ you wouldn't have much left.
So, macros not only make names shorter, and infact typedefs and ref alias can also be used to make names shorter, but macros can also avoid runtime overheads. Macros happen way before runtime. Macros have been avoiding run time over heads way before CPP features such as move and template.
I wouldn't say bad as it will depend on the personal choice. My policy is when there is a type-safe alternatives is available in C++, use them as it will reduce the errors in the code.
It depends on which features. Using define
macros in C++ is strongly frowned upon, and for a good reason. You can almost always replace a use of a define
macro with something more maintainable and safe in C++ (templates, inline functions, etc.)
Streams, on the other hand, are rightly judged by some people to be very slow and I've seen a lot of valid and high-quality C++ code using C's FILE*
with its host of functions instead.
And another thing: with all due respect to the plethora of stream formatting possibilities, for stuff like simple debug printouts, IMHO you just can't beat the succinctness of printf
and its format string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With