Question about macros

This is a discussion on Question about macros within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a question about macros. I am developing a cpp program that will be run as a standalone ...

I personally wouldn't do "if (inverse == true)", but do "if (inverse)".

--
Mats

I always do if ( something == true ) or if ( ptr != NULL ) because it's more explicit and easier to read if you're skimming through the code quickly; and it was one of the coding standards at my first company.

Yes, assuming that the left hand expression is an rvalue or const. However, because this does not map to the usual way one would say it, I would argue that this would then make it harder "to read if you're skimming through the code quickly".

Basically, I see no advantage to comparing to a boolean literal. If the variable is not a bool, and you want to be explicit about it, then compare with an appropriate value (e.g., zero), or call an appropriate named function (e.g., use the fail() member function of an input stream instead of relying on an implicit conversion). In that sense, I see some value with comparing a pointer with a null pointer constant.

That would make no sense. That's not being any more explicit, that's just being crazy.

Just to clarify: under what situations did your first company's coding standard mandate the comparison with a boolean literal?

As I noted in post #8, I can understand the comparison with a null pointer constant to be explicit, and of course the use of a named function instead of an implicit conversion (directly or indirectly) to bool. But to compare a boolean flag or a boolean value returned by a function call with a boolean literal?

That would make no sense. That's not being any more explicit, that's just being crazy.

Just to clarify: under what situations did your first company's coding standard mandate the comparison with a boolean literal?

As I noted in post #8, I can understand the comparison with a null pointer constant to be explicit, and of course the use of a named function instead of an implicit conversion (directly or indirectly) to bool. But to compare a boolean flag or a boolean value returned by a function call with a boolean literal?

Yes, assuming that the left hand expression is an rvalue or const. However, because this does not map to the usual way one would say it, I would argue that this would then make it harder "to read if you're skimming through the code quickly".

Basically, I see no advantage to comparing to a boolean literal. If the variable is not a bool, and you want to be explicit about it, then compare with an appropriate value (e.g., zero), or call an appropriate named function (e.g., use the fail() member function of an input stream instead of relying on an implicit conversion). In that sense, I see some value with comparing a pointer with a null pointer constant.

I did not say I used this form. It feels backwards to me and I wouldn't use it. Just felt like mentioning the alternative.