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. Return the correct null

Return the correct null

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
9 Posts 7 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.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
    if (something != null)
    {
    return something;
    }
    else
    {
    return null;
    }

    Oh no, how come you thought you could return something when it is null? That's a different null!

    L L K F 4 Replies Last reply
    0
    • B Bernhard Hiller

      ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
      if (something != null)
      {
      return something;
      }
      else
      {
      return null;
      }

      Oh no, how come you thought you could return something when it is null? That's a different null!

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

      Saw similar in javascript last week:

      function checkIfValid() {
      var isValid = false;
      if (doSomeOtherCheck()) {
      isValid = true;
      }

      ...

      if (isValid == true) {
      return true;
      } else {
      return false;
      }
      }

      [Edit] Updated to show more content from the function.

      Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.

      Sander RosselS Richard DeemingR 2 Replies Last reply
      0
      • B Bernhard Hiller

        ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
        if (something != null)
        {
        return something;
        }
        else
        {
        return null;
        }

        Oh no, how come you thought you could return something when it is null? That's a different null!

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

        Using the "null" keyword in code should produce a compilation error. Not that it would break enything, right?

        1 Reply Last reply
        0
        • B Bernhard Hiller

          ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
          if (something != null)
          {
          return something;
          }
          else
          {
          return null;
          }

          Oh no, how come you thought you could return something when it is null? That's a different null!

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #4

          Maybe it is work in progress and the something should get some calls in the future...:suss:

          Press F1 for help or google it. Greetings from Germany

          1 Reply Last reply
          0
          • B Bernhard Hiller

            ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
            if (something != null)
            {
            return something;
            }
            else
            {
            return null;
            }

            Oh no, how come you thought you could return something when it is null? That's a different null!

            F Offline
            F Offline
            F ES Sitecore
            wrote on last edited by
            #5

            Actually they are different things, kind of. The FirstOrDefault is going to return default(ISomething) Your assertion that "something" is going to be null is actually an assumption. The code would indeed be redundant if it was this

            ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
            if (something != null)
            {
            return something;
            }
            else
            {
            return default(ISomething);
            }

            however we're getting into new levels of pedantry here :D I sometimes write things like in the OP if I want to make it explicitly clear that it is expected that the function can return a null value.

            Richard DeemingR 1 Reply Last reply
            0
            • L Lost User

              Saw similar in javascript last week:

              function checkIfValid() {
              var isValid = false;
              if (doSomeOtherCheck()) {
              isValid = true;
              }

              ...

              if (isValid == true) {
              return true;
              } else {
              return false;
              }
              }

              [Edit] Updated to show more content from the function.

              Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.

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

              That makes sense if valid is "true" (string) or 1 (int), but you want to return a boolean :)

              Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

              L 1 Reply Last reply
              0
              • L Lost User

                Saw similar in javascript last week:

                function checkIfValid() {
                var isValid = false;
                if (doSomeOtherCheck()) {
                isValid = true;
                }

                ...

                if (isValid == true) {
                return true;
                } else {
                return false;
                }
                }

                [Edit] Updated to show more content from the function.

                Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                That almost makes sense in Javascript: Truthy and Falsy: When All is Not Equal in JavaScript[^] Although you could shorten it to:

                return !!isValid;


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • F F ES Sitecore

                  Actually they are different things, kind of. The FirstOrDefault is going to return default(ISomething) Your assertion that "something" is going to be null is actually an assumption. The code would indeed be redundant if it was this

                  ISomething something = listOfSomethings.Where(...).OrderBy(...).FirstOrDefault();
                  if (something != null)
                  {
                  return something;
                  }
                  else
                  {
                  return default(ISomething);
                  }

                  however we're getting into new levels of pedantry here :D I sometimes write things like in the OP if I want to make it explicitly clear that it is expected that the function can return a null value.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  F-ES Sitecore wrote:

                  The FirstOrDefault is going to return default(ISomething)

                  Not necessarily. Assuming ISomething is an interface, default(ISomething) will be null. But:

                  interface ISomething { }
                  struct Something : ISomething { }

                  var listOfSomethings = new List<Something>();
                  ISomething result = listOfSomethings.FirstOrDefault();
                  Console.WriteLine(result == null); // False

                  For an empty sequence, FirstOrDefault returns default(TSource), where TSource is the type parameter of the input sequence. And if ISomething isn't an interface, then whoever wrote the code needs to be introduced to the clue-bat. Now that's a new level of pedantry! :-D


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    That makes sense if valid is "true" (string) or 1 (int), but you want to return a boolean :)

                    Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

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

                    I should have wrote that further up the function, isValid is set to either true or false :)

                    Ah, I see you have the machine that goes ping. This is my favorite. You see we lease it back from the company we sold it to and that way it comes under the monthly current budget and not the capital account.

                    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