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. Why try when it's easier to fail?

Why try when it's easier to fail?

Scheduled Pinned Locked Moved The Weird and The Wonderful
pythoncomdata-structuresjsonquestion
8 Posts 8 Posters 17 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
    Marc Clifton
    wrote on last edited by
    #1

    foo.TryGetValue("bar", out object value);
    var somestr = value.ToString();

    Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

    Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

    A Sander RosselS D H L 7 Replies Last reply
    0
    • M Marc Clifton

      foo.TryGetValue("bar", out object value);
      var somestr = value.ToString();

      Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

      Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

      A Offline
      A Offline
      AFell2
      wrote on last edited by
      #2

      Brilliant! Promote this programmer to management so they don't write code that breaks the system with random null reference exceptions.

      1 Reply Last reply
      0
      • M Marc Clifton

        foo.TryGetValue("bar", out object value);
        var somestr = value.ToString();

        Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

        Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

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

        At least he tried ;p

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

        1 Reply Last reply
        0
        • M Marc Clifton

          foo.TryGetValue("bar", out object value);
          var somestr = value.ToString();

          Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

          Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

          D Offline
          D Offline
          Dominic Burford
          wrote on last edited by
          #4

          A classic case of "when a little knowledge is a dangerous thing". They understood that they needed to use TryGetValue() but not how to use it.

          "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

          1 Reply Last reply
          0
          • M Marc Clifton

            foo.TryGetValue("bar", out object value);
            var somestr = value.ToString();

            Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

            Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

            H Offline
            H Offline
            Harrison Pratt
            wrote on last edited by
            #5

            Prolog has the notion of "fail" and handles this gracefully (Visual Prolog in the example below):

            facts % key_value lookup table
            key_value : (string Key, integer Value).

            predicates
            tryGetValue : (string Key) -> integer Value determ.
            clauses
            tryGetValue(K) = V :-
            key_value(K, V), % FAILs here if undefined key_value/2
            !.

            1 Reply Last reply
            0
            • M Marc Clifton

              foo.TryGetValue("bar", out object value);
              var somestr = value.ToString();

              Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

              Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

              L Offline
              L Offline
              LoRezz
              wrote on last edited by
              #6

              Ah, but there is no fail without try.

              Nobody special

              1 Reply Last reply
              0
              • M Marc Clifton

                foo.TryGetValue("bar", out object value);
                var somestr = value.ToString();

                Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

                Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                R Offline
                R Offline
                RugbyLeague
                wrote on last edited by
                #7

                They were probably so thrilled with using inline type declaration they thought their job that day was done

                1 Reply Last reply
                0
                • M Marc Clifton

                  foo.TryGetValue("bar", out object value);
                  var somestr = value.ToString();

                  Sort of defeats the purpose of the try when foo doesn't have the key "bar" or even if it does, the value might be null. :sigh: If I show this to the person who wrote it, do you think they'll suggest a try-catch block to not break the rest of the code? :rolleyes:

                  Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

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

                  var somestr = foo.TryGetValue("bar", out var value) ? value?.ToString() : null;

                  Would that be better?! :o

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

                  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