Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. to bool or not to bool?

to bool or not to bool?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14980433
    wrote on last edited by
    #1

    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?

    Mircea NeacsuM D 2 Replies Last reply
    0
    • M Member_14980433

      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?

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        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

        M Offline
        M Offline
        Member_14980433
        wrote on last edited by
        #3

        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 "

        Mircea NeacsuM 1 Reply Last reply
        0
        • M Member_14980433

          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 "

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Member_14980433

            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?

            D Offline
            D Offline
            Daniel Pfeffer
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups