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. Dictionary Misuse

Dictionary Misuse

Scheduled Pinned Locked Moved The Weird and The Wonderful
ruby
10 Posts 8 Posters 2 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
    Martin Thwaites
    wrote on last edited by
    #1

    Just found this little gem in a live application that has been wrote (for us) and maintained by an outsourcing partner we use... paraphrased to make it easier to read.

    string keyToFind = "asdfgsd44rtgr";

    Dictionary dctTemp = GetDictionary();

    object theObject = dctTemp.ToList().FirstOrDefault(x => x.key == keyToFind).Value;

    This is in a live piece of code, and is run on every application login. As far as I'm aware, the compiler would not be able to optimise this to use dctTemp[keyToFind] and therefore would not be using a HashCode

    H H E 3 Replies Last reply
    0
    • M Martin Thwaites

      Just found this little gem in a live application that has been wrote (for us) and maintained by an outsourcing partner we use... paraphrased to make it easier to read.

      string keyToFind = "asdfgsd44rtgr";

      Dictionary dctTemp = GetDictionary();

      object theObject = dctTemp.ToList().FirstOrDefault(x => x.key == keyToFind).Value;

      This is in a live piece of code, and is run on every application login. As far as I'm aware, the compiler would not be able to optimise this to use dctTemp[keyToFind] and therefore would not be using a HashCode

      H Offline
      H Offline
      Henning Dieterichs
      wrote on last edited by
      #2

      This is "wise foresight": In the next update, they can say that they increased their performance by about 20%. I've the feeling, some companies introduce bugs on purpose in their own application to boost the sale for their future updates.

      If you find spelling- or grammer-mistakes, please let me know, so that I can correct them (at least for me) - english is not my first language...

      P 1 Reply Last reply
      0
      • H Henning Dieterichs

        This is "wise foresight": In the next update, they can say that they increased their performance by about 20%. I've the feeling, some companies introduce bugs on purpose in their own application to boost the sale for their future updates.

        If you find spelling- or grammer-mistakes, please let me know, so that I can correct them (at least for me) - english is not my first language...

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        The duece you say.

        1 Reply Last reply
        0
        • M Martin Thwaites

          Just found this little gem in a live application that has been wrote (for us) and maintained by an outsourcing partner we use... paraphrased to make it easier to read.

          string keyToFind = "asdfgsd44rtgr";

          Dictionary dctTemp = GetDictionary();

          object theObject = dctTemp.ToList().FirstOrDefault(x => x.key == keyToFind).Value;

          This is in a live piece of code, and is run on every application login. As far as I'm aware, the compiler would not be able to optimise this to use dctTemp[keyToFind] and therefore would not be using a HashCode

          H Offline
          H Offline
          Harley L Pebley
          wrote on last edited by
          #4

          Martin Thwaites wrote:

          run on every application login

          So, how often is this? Unless there are a lot of logins/second, don't sweat the small stuff. IMO, the use of ToList (which can simply be deleted) and FirstOrDefault is less of an issue than the fact that they're storing objects. That's a bigger WFT than the optimization from the dictionary specific methods. Don't optimize for speed until you have confirmation it's a hotspot. Optimize first for robustness and maintainability. And be aware, it can't be replaced it with:

          object theObject = dctTemp[keyToFind];

          For the same behavior, it'd have to be:

          object theObject = dctTemp.ContainsKey(keyToFind) : dctTemp[keyToFind] : null;

          M B 2 Replies Last reply
          0
          • H Harley L Pebley

            Martin Thwaites wrote:

            run on every application login

            So, how often is this? Unless there are a lot of logins/second, don't sweat the small stuff. IMO, the use of ToList (which can simply be deleted) and FirstOrDefault is less of an issue than the fact that they're storing objects. That's a bigger WFT than the optimization from the dictionary specific methods. Don't optimize for speed until you have confirmation it's a hotspot. Optimize first for robustness and maintainability. And be aware, it can't be replaced it with:

            object theObject = dctTemp[keyToFind];

            For the same behavior, it'd have to be:

            object theObject = dctTemp.ContainsKey(keyToFind) : dctTemp[keyToFind] : null;

            M Offline
            M Offline
            Martin Thwaites
            wrote on last edited by
            #5

            I wasn't saying it was a hotspot, just that it's poor use of code. It's actually nowhere near to being a hotspot (they have 1 particular click in the application that takes 15 minutes to load the page, and therefore only works when there is a decent internet connection and a high timeout set!). The object thing is FAR worse, however, in the context of how they've wrote the application it does make sense. That is actually one of the things I'm tackling at the moment. This is one of many issues I've found that they've put in (before I started reviewing the code), and I suspect that they will be doing less now. I also appreciate it can't be replaced by exactly what I've said based on the code sample, and in the application itself it would actually need to be different than you've suggested.

            1 Reply Last reply
            0
            • H Harley L Pebley

              Martin Thwaites wrote:

              run on every application login

              So, how often is this? Unless there are a lot of logins/second, don't sweat the small stuff. IMO, the use of ToList (which can simply be deleted) and FirstOrDefault is less of an issue than the fact that they're storing objects. That's a bigger WFT than the optimization from the dictionary specific methods. Don't optimize for speed until you have confirmation it's a hotspot. Optimize first for robustness and maintainability. And be aware, it can't be replaced it with:

              object theObject = dctTemp[keyToFind];

              For the same behavior, it'd have to be:

              object theObject = dctTemp.ContainsKey(keyToFind) : dctTemp[keyToFind] : null;

              B Offline
              B Offline
              Brisingr Aerowing
              wrote on last edited by
              #6

              Harley L. Pebley wrote:

              object theObject = dctTemp.ContainsKey(keyToFind) : dctTemp[keyToFind] : null;

              Shouldn't that be

              object theObject = dctTemp.ContainsKey(keyToFind) ? dctTemp[keyToFind] : null;

              ? EDIT: And why was this downvoted?

              Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

              H 1 Reply Last reply
              0
              • B Brisingr Aerowing

                Harley L. Pebley wrote:

                object theObject = dctTemp.ContainsKey(keyToFind) : dctTemp[keyToFind] : null;

                Shouldn't that be

                object theObject = dctTemp.ContainsKey(keyToFind) ? dctTemp[keyToFind] : null;

                ? EDIT: And why was this downvoted?

                Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

                H Offline
                H Offline
                Harley L Pebley
                wrote on last edited by
                #7

                Yep! No red underline to alert me to the typo. :-D

                J 1 Reply Last reply
                0
                • M Martin Thwaites

                  Just found this little gem in a live application that has been wrote (for us) and maintained by an outsourcing partner we use... paraphrased to make it easier to read.

                  string keyToFind = "asdfgsd44rtgr";

                  Dictionary dctTemp = GetDictionary();

                  object theObject = dctTemp.ToList().FirstOrDefault(x => x.key == keyToFind).Value;

                  This is in a live piece of code, and is run on every application login. As far as I'm aware, the compiler would not be able to optimise this to use dctTemp[keyToFind] and therefore would not be using a HashCode

                  E Offline
                  E Offline
                  ely_bob
                  wrote on last edited by
                  #8

                  Martin Thwaites wrote:

                  string keyToFind = "asdfgsd44rtgr";   Dictionary<string, object> dctTemp = GetDictionary();   object theObject = dctTemp.ToList().FirstOrDefault(x => x.key == keyToFind).Value;

                  Should be:

                  object theObject = GetDictionary()[keytoFind];

                  will return null if it's not in there, and with a dictionary you can only add one unique Key per...See:

                  using System.Collections.Generic;

                  namespace ConsoleApplication2
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  Class1.Sample();
                  }
                  static class Class1
                  {
                  private static Dictionary GetDictionary()
                  {
                  var retval = new Dictionary();
                  retval.Add("asdfgsd44rtgr", "1");

                  			//Throws Error
                  			//System.ArgumentException was unhandled
                  			//Message=An item with the same key has already been added.
                  			//Source=mscorlib
                  			//StackTrace: {ommitted}
                  			retval.Add("asdfgsd44rtgr", "throws error");
                  			return retval;
                  		}
                  		public static void Sample()
                  		{
                  			string keyToFind = "asdfgsd44rtgr";
                  
                  			Dictionary dctTemp = GetDictionary();
                  
                  			//object theObject = dctTemp.ToList().FirstOrDefault(x => x.Key == keyToFind).Value;
                  			object theObject = GetDictionary()\[keyToFind\];
                  		}
                  
                  
                  	}
                  
                  }
                  

                  }

                  or did I miss Something ?:confused:

                  I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
                  -----
                  "The conversations he was having with himself were becoming ominous."-.. On the radio...

                  1 Reply Last reply
                  0
                  • H Harley L Pebley

                    Yep! No red underline to alert me to the typo. :-D

                    J Offline
                    J Offline
                    James Lonero
                    wrote on last edited by
                    #9

                    Aren't we in a sad state when we trust our tools more than our minds? Don't feel bad, I am also guilty of such offenses. (I am using MS Word to type this to check may spelling and grammar errors as well as use the thesaurus.)

                    F 1 Reply Last reply
                    0
                    • J James Lonero

                      Aren't we in a sad state when we trust our tools more than our minds? Don't feel bad, I am also guilty of such offenses. (I am using MS Word to type this to check may spelling and grammar errors as well as use the thesaurus.)

                      F Offline
                      F Offline
                      Florin Jurcovici
                      wrote on last edited by
                      #10

                      And the reliance on tools bites you as you comment on it ...

                      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