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. XOR is for the weak

XOR is for the weak

Scheduled Pinned Locked Moved The Weird and The Wonderful
15 Posts 8 Posters 1 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.
  • L Lutoslaw

    public class SomeLocationData
    {
    // fields...

    public SomeLocationData(Long someAreaId, String longitude, String latitude)
    {
        if ((someAreaId == null) == (longitude == null || latitude == null))
        {
            throw new IllegalArgumentException("Either someAreaId or coordinates must be specified.");
        }
        // ... init fields
    

    THAT CHECK. :cool:

    R Offline
    R Offline
    Rob Philpott
    wrote on last edited by
    #2

    That's the most horrible thing I've seen today.

    Regards, Rob Philpott.

    L 1 Reply Last reply
    0
    • R Rob Philpott

      That's the most horrible thing I've seen today.

      Regards, Rob Philpott.

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #3

      TBF it's an IDE's "autofix". I've written a condition:

      isValid = (someAreaId != null) ^ (longitude != null && latitude != null);
      if (!isValid) { ...

      Which literally translates to "either someAreaId is not null or both coordinates are not null". This is a required business logic and it doesn't seem to be a way around it. And then I got a "simplify boolean" and "inline variable" quickfixes. If you pass the initial "WTF" feeling, then it starts to make sense.

      1 Reply Last reply
      0
      • L Lutoslaw

        public class SomeLocationData
        {
        // fields...

        public SomeLocationData(Long someAreaId, String longitude, String latitude)
        {
            if ((someAreaId == null) == (longitude == null || latitude == null))
            {
                throw new IllegalArgumentException("Either someAreaId or coordinates must be specified.");
            }
            // ... init fields
        

        THAT CHECK. :cool:

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

        Looks like a buggy check... what if all 3 are specified? That's will throw the error, that's what! :doh:

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        Sander RosselS 1 Reply Last reply
        0
        • S Super Lloyd

          Looks like a buggy check... what if all 3 are specified? That's will throw the error, that's what! :doh:

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #5

          I think that's what it's supposed to do. Either one or the other needs to have a value, not both and not neither ;)

          Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

          S 1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            I think that's what it's supposed to do. Either one or the other needs to have a value, not both and not neither ;)

            Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

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

            well perhaps.. but then the error message is misleading! :o

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            M 1 Reply Last reply
            0
            • S Super Lloyd

              well perhaps.. but then the error message is misleading! :o

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              M Offline
              M Offline
              MarkTJohnson
              wrote on last edited by
              #7

              How is

              Either someAreaId or coordinates must be specified.

              misleading, it doesn't say AND?

              S 1 Reply Last reply
              0
              • M MarkTJohnson

                How is

                Either someAreaId or coordinates must be specified.

                misleading, it doesn't say AND?

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

                Mmm.. maybe the nuance of English are lost on me.. not being a native... This message doesn't seem to imply to me that it is wrong to have both condition true...

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                Sander RosselS 1 Reply Last reply
                0
                • S Super Lloyd

                  Mmm.. maybe the nuance of English are lost on me.. not being a native... This message doesn't seem to imply to me that it is wrong to have both condition true...

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  Sander RosselS Offline
                  Sander RosselS Offline
                  Sander Rossel
                  wrote on last edited by
                  #9

                  It's the very definition of "either... or..." (a.k.a. XOR) ;) Actually, I don't think the code is that bad. I think a lot more programmers can read that than an XOR, which is far from common in C#.

                  Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                  L L 2 Replies Last reply
                  0
                  • Sander RosselS Sander Rossel

                    It's the very definition of "either... or..." (a.k.a. XOR) ;) Actually, I don't think the code is that bad. I think a lot more programmers can read that than an XOR, which is far from common in C#.

                    Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

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

                    :thumbsup:

                    It does not solve my Problem, but it answers my question

                    1 Reply Last reply
                    0
                    • Sander RosselS Sander Rossel

                      It's the very definition of "either... or..." (a.k.a. XOR) ;) Actually, I don't think the code is that bad. I think a lot more programmers can read that than an XOR, which is far from common in C#.

                      Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                      L Offline
                      L Offline
                      Lutoslaw
                      wrote on last edited by
                      #11

                      Sander Rossel wrote:

                      Actually, I don't think the code is that bad. I think a lot more programmers can read that than an XOR, which is far from common in C#.

                      OK so I don't need to duck & cover if I say that I have left it as it was? :-O

                      1 Reply Last reply
                      0
                      • L Lutoslaw

                        public class SomeLocationData
                        {
                        // fields...

                        public SomeLocationData(Long someAreaId, String longitude, String latitude)
                        {
                            if ((someAreaId == null) == (longitude == null || latitude == null))
                            {
                                throw new IllegalArgumentException("Either someAreaId or coordinates must be specified.");
                            }
                            // ... init fields
                        

                        THAT CHECK. :cool:

                        S Offline
                        S Offline
                        SteveTheThread
                        wrote on last edited by
                        #12

                        Also, you should not be using

                        String

                        but

                        string

                        I recommend taking a look at this. string vs. String is not a style debate[^]

                        L 1 Reply Last reply
                        0
                        • S SteveTheThread

                          Also, you should not be using

                          String

                          but

                          string

                          I recommend taking a look at this. string vs. String is not a style debate[^]

                          L Offline
                          L Offline
                          Lutoslaw
                          wrote on last edited by
                          #13

                          It's java.

                          L 1 Reply Last reply
                          0
                          • L Lutoslaw

                            It's java.

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

                            Ouch!. My bad. Thanks

                            1 Reply Last reply
                            0
                            • L Lutoslaw

                              public class SomeLocationData
                              {
                              // fields...

                              public SomeLocationData(Long someAreaId, String longitude, String latitude)
                              {
                                  if ((someAreaId == null) == (longitude == null || latitude == null))
                                  {
                                      throw new IllegalArgumentException("Either someAreaId or coordinates must be specified.");
                                  }
                                  // ... init fields
                              

                              THAT CHECK. :cool:

                              M Offline
                              M Offline
                              Matias Lopez
                              wrote on last edited by
                              #15

                              It's old school rock! :cool: I remembered how to MessageBox (in Client) some texts on ASP.NET...

                              protected void ShowMessage(string message)
                              {
                              string msg = "alert('" + message + "');";
                              base.ClientScript.RegisterStartupScript(base.GetType(), "jsShowMessage", msg, true);
                              }

                              Yeah!

                              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