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. Ugly, ugly, ugly!

Ugly, ugly, ugly!

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++help
3 Posts 3 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.
  • J Offline
    J Offline
    jamie550
    wrote on last edited by
    #1

    I was digging around some code I wrote three years ago, looking for the cause of a bug some reported, when I found this. And no wonder - the full code is so full of holes that I'm surprised it works as expected.

    /// <summary>
    ///
    /// </summary>
    /// <param name="adjancies"></param>
    /// <param name="regions"></param>
    /// <returns>True if was a special case.</returns>
    private static bool CheckSpecialCases(List<List<int>> adjancies, Dictionary<int, Region> regions)
    {
    if (regions.Count < 2) return true;
    if (regions.Count == 2)
    {
    int key1 = 0, key2 = 0, inc = 0;
    foreach (KeyValuePair<int, Region> kvp in regions)
    {
    if (inc == 0)
    {
    inc++;
    key1 = kvp.Key;
    }
    else
    key2 = kvp.Key;
    }

    	lock (listLock)
    	{
    		if (adjancies\[key1\].Contains(key2))
    			return true;
    
    		adjancies\[key1\].Add(key2);
    		adjancies\[key2\].Add(key1);
    		return true;
    	}
    }
    return false;
    

    }

    Hmm, where to start. How about that comment "True if it was a special case". Yes, because it is obvious what a special case is. Then the whole looping over a collection of two items, with a flag to set two items to specific values (instead of say, asking for the keys of the Dictionary). Oh, and the whole reason for the reported bug is that the indices of key1 and key2 were not checked for being in bounds somewhere prior in the code, which would have saved user trouble from this. Plus, the redundancy of using lists to specify connections, while matching the underlying implementation of data that I was working with, makes things unnecessarily complicated.

    A L 2 Replies Last reply
    0
    • J jamie550

      I was digging around some code I wrote three years ago, looking for the cause of a bug some reported, when I found this. And no wonder - the full code is so full of holes that I'm surprised it works as expected.

      /// <summary>
      ///
      /// </summary>
      /// <param name="adjancies"></param>
      /// <param name="regions"></param>
      /// <returns>True if was a special case.</returns>
      private static bool CheckSpecialCases(List<List<int>> adjancies, Dictionary<int, Region> regions)
      {
      if (regions.Count < 2) return true;
      if (regions.Count == 2)
      {
      int key1 = 0, key2 = 0, inc = 0;
      foreach (KeyValuePair<int, Region> kvp in regions)
      {
      if (inc == 0)
      {
      inc++;
      key1 = kvp.Key;
      }
      else
      key2 = kvp.Key;
      }

      	lock (listLock)
      	{
      		if (adjancies\[key1\].Contains(key2))
      			return true;
      
      		adjancies\[key1\].Add(key2);
      		adjancies\[key2\].Add(key1);
      		return true;
      	}
      }
      return false;
      

      }

      Hmm, where to start. How about that comment "True if it was a special case". Yes, because it is obvious what a special case is. Then the whole looping over a collection of two items, with a flag to set two items to specific values (instead of say, asking for the keys of the Dictionary). Oh, and the whole reason for the reported bug is that the indices of key1 and key2 were not checked for being in bounds somewhere prior in the code, which would have saved user trouble from this. Plus, the redundancy of using lists to specify connections, while matching the underlying implementation of data that I was working with, makes things unnecessarily complicated.

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

      Harsh! I hope the guy or gal who wrote this can take a little constructive criticism. ;)

      [Forum Guidelines]

      1 Reply Last reply
      0
      • J jamie550

        I was digging around some code I wrote three years ago, looking for the cause of a bug some reported, when I found this. And no wonder - the full code is so full of holes that I'm surprised it works as expected.

        /// <summary>
        ///
        /// </summary>
        /// <param name="adjancies"></param>
        /// <param name="regions"></param>
        /// <returns>True if was a special case.</returns>
        private static bool CheckSpecialCases(List<List<int>> adjancies, Dictionary<int, Region> regions)
        {
        if (regions.Count < 2) return true;
        if (regions.Count == 2)
        {
        int key1 = 0, key2 = 0, inc = 0;
        foreach (KeyValuePair<int, Region> kvp in regions)
        {
        if (inc == 0)
        {
        inc++;
        key1 = kvp.Key;
        }
        else
        key2 = kvp.Key;
        }

        	lock (listLock)
        	{
        		if (adjancies\[key1\].Contains(key2))
        			return true;
        
        		adjancies\[key1\].Add(key2);
        		adjancies\[key2\].Add(key1);
        		return true;
        	}
        }
        return false;
        

        }

        Hmm, where to start. How about that comment "True if it was a special case". Yes, because it is obvious what a special case is. Then the whole looping over a collection of two items, with a flag to set two items to specific values (instead of say, asking for the keys of the Dictionary). Oh, and the whole reason for the reported bug is that the indices of key1 and key2 were not checked for being in bounds somewhere prior in the code, which would have saved user trouble from this. Plus, the redundancy of using lists to specify connections, while matching the underlying implementation of data that I was working with, makes things unnecessarily complicated.

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

        jamie550 wrote:

        unnecessarily complicated

        In OOP they call it "extendable". :rolleyes:

        Greetings - Jacek

        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