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. Other Discussions
  3. The Weird and The Wonderful
  4. How to test equality

How to test equality

Scheduled Pinned Locked Moved The Weird and The Wonderful
helptutorialquestion
20 Posts 13 Posters 4 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.
  • N Offline
    N Offline
    nathan northcutt gmail com
    wrote on last edited by
    #1

    My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

    // getting some user session settings
    if(!!isLocked)
    {
    // abort the login and show an error message.
    }
    else
    {
    // login the user.
    }

    This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

    L J S S R 6 Replies Last reply
    0
    • N nathan northcutt gmail com

      My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

      // getting some user session settings
      if(!!isLocked)
      {
      // abort the login and show an error message.
      }
      else
      {
      // login the user.
      }

      This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      nathan.northcutt@gmail.com wrote:

      if(!!isLocked)

      I wonder if it means DefinitelyNot()? :laugh:

      S J D 3 Replies Last reply
      0
      • N nathan northcutt gmail com

        My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

        // getting some user session settings
        if(!!isLocked)
        {
        // abort the login and show an error message.
        }
        else
        {
        // login the user.
        }

        This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

        J Offline
        J Offline
        Jeroen De Dauw
        wrote on last edited by
        #3

        This does remind me of the admin authentication of a custom made e-shop I saw once. The script checked whether the user name and admin matched, but there was no check whatsoever if this user was an admin. In other words, everyone with an account on the site could get in the admin panel :wtf:

        Jeroen De Dauw --- Forums ; Blog ; Wiki --- 70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!

        1 Reply Last reply
        0
        • L Lost User

          nathan.northcutt@gmail.com wrote:

          if(!!isLocked)

          I wonder if it means DefinitelyNot()? :laugh:

          S Offline
          S Offline
          StM0n
          wrote on last edited by
          #4

          c#5 will be shipping with this features ! => CertainlyNot() !! => DefinitelyNot() !!! => ReallyTrustMeItIsNot() :laugh: PS: still lying on the floor...

          (yes|no|maybe)*

          L 1 Reply Last reply
          0
          • N nathan northcutt gmail com

            My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

            // getting some user session settings
            if(!!isLocked)
            {
            // abort the login and show an error message.
            }
            else
            {
            // login the user.
            }

            This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #5

            it's cute! maybe I should do that too! :) Well maybe he did that just because he like the look of it and, anyway (!!b == b) is true (at least in C#, I guess it's not in C), so that's alright! ;-)

            A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

            1 Reply Last reply
            0
            • N nathan northcutt gmail com

              My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

              // getting some user session settings
              if(!!isLocked)
              {
              // abort the login and show an error message.
              }
              else
              {
              // login the user.
              }

              This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

              S Offline
              S Offline
              supercat9
              wrote on last edited by
              #6

              While I wouldn't generally use "!!" in the argument of an 'if' statement (which will inherently test whether the operand is non-zero) I see nothing wrong with using "!!" in certain contexts. For example, some compilers support bit variables and/or allow single-bit fields within a struct. If I need to set such variables based upon certain bits in a variable, I think it's cleaner to say bitVar = !!(byteVar & 0x40); than to say bitVar = ((byteVar & 0x40) != 0);. Basically, I regard "!!" as an operator which turns an integer into a Boolean. Anything wrong with that?

              L PJ ArendsP 2 Replies Last reply
              0
              • S supercat9

                While I wouldn't generally use "!!" in the argument of an 'if' statement (which will inherently test whether the operand is non-zero) I see nothing wrong with using "!!" in certain contexts. For example, some compilers support bit variables and/or allow single-bit fields within a struct. If I need to set such variables based upon certain bits in a variable, I think it's cleaner to say bitVar = !!(byteVar & 0x40); than to say bitVar = ((byteVar & 0x40) != 0);. Basically, I regard "!!" as an operator which turns an integer into a Boolean. Anything wrong with that?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                supercat9 wrote:

                Anything wrong with that?

                Everything! It is not an operator in the sense of being part of the language. It is a clever trick that you may understand, but is likely to confuse someone who later has the job of maintaining the code. In my experience the more simple and standard the code is the better. Whether you write bitVar = !!(byteVar & 0x40); or bitVar = ((byteVar & 0x40) != 0);, chances are the compiler will optimise it and generate the same object code.

                OriginalGriffO S 2 Replies Last reply
                0
                • L Lost User

                  supercat9 wrote:

                  Anything wrong with that?

                  Everything! It is not an operator in the sense of being part of the language. It is a clever trick that you may understand, but is likely to confuse someone who later has the job of maintaining the code. In my experience the more simple and standard the code is the better. Whether you write bitVar = !!(byteVar & 0x40); or bitVar = ((byteVar & 0x40) != 0);, chances are the compiler will optimise it and generate the same object code.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Yay! :thumbsup:

                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • L Lost User

                    nathan.northcutt@gmail.com wrote:

                    if(!!isLocked)

                    I wonder if it means DefinitelyNot()? :laugh:

                    J Offline
                    J Offline
                    Jaime Olivares
                    wrote on last edited by
                    #9

                    This sounds to me like: it was evaluating for false and now is evaluation for true.

                    Best regards, Jaime.

                    1 Reply Last reply
                    0
                    • S supercat9

                      While I wouldn't generally use "!!" in the argument of an 'if' statement (which will inherently test whether the operand is non-zero) I see nothing wrong with using "!!" in certain contexts. For example, some compilers support bit variables and/or allow single-bit fields within a struct. If I need to set such variables based upon certain bits in a variable, I think it's cleaner to say bitVar = !!(byteVar & 0x40); than to say bitVar = ((byteVar & 0x40) != 0);. Basically, I regard "!!" as an operator which turns an integer into a Boolean. Anything wrong with that?

                      PJ ArendsP Offline
                      PJ ArendsP Offline
                      PJ Arends
                      wrote on last edited by
                      #10

                      Actually, I would use bitVar = ((byteVar & 0x40) == 0x40);. Using the != 0 or in your case the !! style will work if you are comparing a single bit, but it will break if you are comparing multiple bits: bitVar = ((byteVar & 0x41) == 0x41); is not the same as bitVar = ((byteVar & 0x41) != 0);.


                      You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

                      Within you lies the power for good; Use it!

                      S 1 Reply Last reply
                      0
                      • PJ ArendsP PJ Arends

                        Actually, I would use bitVar = ((byteVar & 0x40) == 0x40);. Using the != 0 or in your case the !! style will work if you are comparing a single bit, but it will break if you are comparing multiple bits: bitVar = ((byteVar & 0x41) == 0x41); is not the same as bitVar = ((byteVar & 0x41) != 0);.


                        You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

                        S Offline
                        S Offline
                        supercat9
                        wrote on last edited by
                        #11

                        PJ Arends wrote:

                        Using the != 0 or in your case the !! style will work if you are comparing a single bit, but it will break if you are comparing multiple bits: bitVar = ((byteVar & 0x41) == 0x41); is not the same as bitVar = ((byteVar & 0x41) != 0);.

                        If I am testing multiple bits, I will either explicitly test for being unequal to zero, or being equal to a particular number (depending upon whether the requirement is that at least one bit be set, or that all bits be set). I do not like the syntax you seem to favor, since its normal semantic meaning requires that the same constant appear on both halves of the comparison operator; if the two halves get edited out of sync, the code will break.

                        L 1 Reply Last reply
                        0
                        • S supercat9

                          PJ Arends wrote:

                          Using the != 0 or in your case the !! style will work if you are comparing a single bit, but it will break if you are comparing multiple bits: bitVar = ((byteVar & 0x41) == 0x41); is not the same as bitVar = ((byteVar & 0x41) != 0);.

                          If I am testing multiple bits, I will either explicitly test for being unequal to zero, or being equal to a particular number (depending upon whether the requirement is that at least one bit be set, or that all bits be set). I do not like the syntax you seem to favor, since its normal semantic meaning requires that the same constant appear on both halves of the comparison operator; if the two halves get edited out of sync, the code will break.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          I agree. Furthermore I'm all in favor of using named constants for these kinds of tests, so the code becomes self-documenting. :)

                          Luc Pattyn


                          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                          S 1 Reply Last reply
                          0
                          • L Lost User

                            supercat9 wrote:

                            Anything wrong with that?

                            Everything! It is not an operator in the sense of being part of the language. It is a clever trick that you may understand, but is likely to confuse someone who later has the job of maintaining the code. In my experience the more simple and standard the code is the better. Whether you write bitVar = !!(byteVar & 0x40); or bitVar = ((byteVar & 0x40) != 0);, chances are the compiler will optimise it and generate the same object code.

                            S Offline
                            S Offline
                            supercat9
                            wrote on last edited by
                            #13

                            Richard MacCutchan wrote:

                            In my experience the more simple and standard the code is the better. Whether you write bitVar = !!(byteVar & 0x40); or bitVar = ((byteVar & 0x40) != 0);, chances are the compiler will optimise it and generate the same object code.

                            I use the more verbose syntax when testing more than one bit, to make explicit whether I am checking for any bit being set, or all bits being set. Whether or not anyone else uses the same convention, I find that at least in my own code it's nice to be able to distinguish at a glance between single-bit tests and multi-bit tests.

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              I agree. Furthermore I'm all in favor of using named constants for these kinds of tests, so the code becomes self-documenting. :)

                              Luc Pattyn


                              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                              S Offline
                              S Offline
                              supercat9
                              wrote on last edited by
                              #14

                              Luc Pattyn wrote:

                              Furthermore I'm all in favor of using named constants for these kinds of tests, so the code becomes self-documenting.

                              I like named constants too, but many of the same issues remain. If I have a test: if ((somevariable & SOMEIDENTIFIER) == SOMEIDENTIFIER) I have to worry about whether the two "SOMEIDENTIFIER"s are the same. If the identifiers are the same and the value gets changed (but remains a power of two) things will be fine, but if the code is copied/pasted and then modified to use a different identifier, an imperfect patch job could cause all sorts of problems.

                              L 1 Reply Last reply
                              0
                              • S supercat9

                                Luc Pattyn wrote:

                                Furthermore I'm all in favor of using named constants for these kinds of tests, so the code becomes self-documenting.

                                I like named constants too, but many of the same issues remain. If I have a test: if ((somevariable & SOMEIDENTIFIER) == SOMEIDENTIFIER) I have to worry about whether the two "SOMEIDENTIFIER"s are the same. If the identifiers are the same and the value gets changed (but remains a power of two) things will be fine, but if the code is copied/pasted and then modified to use a different identifier, an imperfect patch job could cause all sorts of problems.

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #15

                                I agree once more. If SOMEIDENTIFIER has more than one bit set, it all depends on the intent of the snippet; a switch might even be in order then. :)

                                Luc Pattyn


                                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                                S 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  I agree once more. If SOMEIDENTIFIER has more than one bit set, it all depends on the intent of the snippet; a switch might even be in order then. :)

                                  Luc Pattyn


                                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                                  S Offline
                                  S Offline
                                  supercat9
                                  wrote on last edited by
                                  #16

                                  I guess my main point is that I view testing for a single bit as being a different operation from testing a multi-bit expression numerically (a judgment colored perhaps by the existence of instructions on my favorite microcontrollers to perform such tests). Using an expressions like "!!(expr & BITMASK)" for the cases where BITMASK is a single bit makes clear that the code is supposed to look at a single bit, as opposed to numerically masking the expression and examining the result. The compiler will recognize the single-bit mask and generate code to exploit the bit-test instructions regardless of how the source code is written, but I like having a different way of expressing single-bit tests from multi-bit ones. BTW, I sometimes use 'case' structures for the multi-bit case, though if all four cases of two bit flags are populated, I'll often use an if/else-if tree, depending on what sort of speed is required.

                                  1 Reply Last reply
                                  0
                                  • N nathan northcutt gmail com

                                    My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

                                    // getting some user session settings
                                    if(!!isLocked)
                                    {
                                    // abort the login and show an error message.
                                    }
                                    else
                                    {
                                    // login the user.
                                    }

                                    This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

                                    R Offline
                                    R Offline
                                    Robert Surtees
                                    wrote on last edited by
                                    #17

                                    This could have resulted from a quick conversion of Assert() statements. The assert is a positive statement that explodes if not true. To covert them to execute a non-lethal action the quickest and perhaps safest approach is to just change to an if() throwing a NOT in front of the expreseion. New error handling code follows. ...not advocating this, just offering an explanation.

                                    1 Reply Last reply
                                    0
                                    • N nathan northcutt gmail com

                                      My favorite coding horror isn't over the top, nor does it leave the system more vulnurable than it began. But how can I honestly trust anyone who does the following:

                                      // getting some user session settings
                                      if(!!isLocked)
                                      {
                                      // abort the login and show an error message.
                                      }
                                      else
                                      {
                                      // login the user.
                                      }

                                      This wasn't a mistake on their part either. They used this kind of test in more than 50 places...

                                      N Offline
                                      N Offline
                                      Nemanja Trifunovic
                                      wrote on last edited by
                                      #18

                                      That construct is often used to avoid warning C4800 [^]

                                      utf8-cpp

                                      1 Reply Last reply
                                      0
                                      • S StM0n

                                        c#5 will be shipping with this features ! => CertainlyNot() !! => DefinitelyNot() !!! => ReallyTrustMeItIsNot() :laugh: PS: still lying on the floor...

                                        (yes|no|maybe)*

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        Add this to the list: !!!! => ISwearByGodItIsNot()

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          nathan.northcutt@gmail.com wrote:

                                          if(!!isLocked)

                                          I wonder if it means DefinitelyNot()? :laugh:

                                          D Offline
                                          D Offline
                                          dojohansen
                                          wrote on last edited by
                                          #20

                                          Definitely not! It means not not, does it not?

                                          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