to bool or not to bool?
-
This has been hashed over umpteen times... C&R said .. loosely... , thus directly NOT defining "bool" as it is used in C++ if(true) process , if not true skip processing One would then expect
if(discoveryAgent->discoveredDevices().isEmpty())
do not iterate devices...code will skip iteration. Would
if(discoveryAgent->discoveredDevices().isEmpty()== 0 )
do not iterate devices...be overcautious overkill or create a headache or wrong assumption that 0 = false in C++ ? Perhaps some with more experience in C+ could chime in with some practical advise?
-
This has been hashed over umpteen times... C&R said .. loosely... , thus directly NOT defining "bool" as it is used in C++ if(true) process , if not true skip processing One would then expect
if(discoveryAgent->discoveredDevices().isEmpty())
do not iterate devices...code will skip iteration. Would
if(discoveryAgent->discoveredDevices().isEmpty()== 0 )
do not iterate devices...be overcautious overkill or create a headache or wrong assumption that 0 = false in C++ ? Perhaps some with more experience in C+ could chime in with some practical advise?
The second one is the exact opposite of the first one and shows you why you shouldn't mix bool with int: it just creates confusion. I assume that
isEmpty()
returns true is the set is empty. So first one says "if set is empty, do not iterate devices". The second one says "if empty is false (that means there are some devices), do not iterate devices". Probably not what you had in mind.Mircea
-
The second one is the exact opposite of the first one and shows you why you shouldn't mix bool with int: it just creates confusion. I assume that
isEmpty()
returns true is the set is empty. So first one says "if set is empty, do not iterate devices". The second one says "if empty is false (that means there are some devices), do not iterate devices". Probably not what you had in mind.Mircea
if(discoveryAgent->discoveredDevices().isEmpty()) -> if ( statement is true ) do not iterate devices... -> process / do this code Poor wording /formating on my part code will skip iteration should read "process this code "
-
if(discoveryAgent->discoveredDevices().isEmpty()) -> if ( statement is true ) do not iterate devices... -> process / do this code Poor wording /formating on my part code will skip iteration should read "process this code "
Anyway my advice remains the same: mixing booleans and integers is bad form. People will frown at your code and we don’t want that to happen, do we? :laugh:
Mircea
-
This has been hashed over umpteen times... C&R said .. loosely... , thus directly NOT defining "bool" as it is used in C++ if(true) process , if not true skip processing One would then expect
if(discoveryAgent->discoveredDevices().isEmpty())
do not iterate devices...code will skip iteration. Would
if(discoveryAgent->discoveredDevices().isEmpty()== 0 )
do not iterate devices...be overcautious overkill or create a headache or wrong assumption that 0 = false in C++ ? Perhaps some with more experience in C+ could chime in with some practical advise?
Given that the bool type has existed in C from its earliest days, don't you think that if the result of a method is Boolean (true / false), you should treat it as such in code?
bool isFoo();
void bar()
{
if (isFoo())
{
// do something
}
else
{
// do something else
}
}Even C has had a built-in _Bool type (define bool, true, and false by including stdbool.h) for 21 years!
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.