DevDisasters

Collections Class

A Junior Developer Learns that Seniority Doesn't Mean Smarter.

Yakir was excited to take a new job. During his first week, a senior developer, James, approached Yakir for some help with .NET collections.

"I can never seem to remember," James said. "What collection type do I want to use: an ArrayList or Hashtable?"

In any other circumstance, Yakir would have considered this question an impromptu quiz from a higher-up. But in James' case, it was a legitimate query.

"It all depends on how you want to access your data," Yakir explained. "If you want to access your data by index, you store it in an ArrayList. If it's easier to store things as a key-value pair, then you use a Hashtable."


Hashing It Out
A few days later, however, James ran into some trouble. "I think you were wrong about the Hashtable," he said. "Once I have real data in there, it takes almost five seconds to add, remove or find an item."

Yakir knew something was wrong and sat with James to look at his code:

class HashTable
{
  public object[] keys;
  public object[] vals;

  public HashTable()
  {
    keys = new object[0];
    vals = new object[0];
  }

  public void Add(object key, 
                  object val)
  {
    Array.Resize(ref keys, 
                 keys.Length + 1);
    Array.Resize(ref vals, 
                 vals.Length + 1);
    keys[keys.Length - 1] = key;
    vals[vals.Length - 1] = val;
  }

  public void Remove(object key)
  {
    object[] tKeys = new object[0];
    object[] tVals = new object[0];
    	 for (int i = 0; 
         i <= keys.Length - 1; i++)
    {
      if (!keys[i].Equals(key))
      {
        Array.Resize(
	  ref tKeys, 
	  tKeys.Length + 1);
	Array.Resize(
	   ref tVals, 
	   tVals.Length + 1);

        tKeys[tKeys.Length - 1] 
	                = keys[i];
        tVals[tVals.Length - 1] 
	                = vals[i];
      }
    }
    keys = tKeys;
    vals = tVals;
  }

  public object GetItem(object key)
  {
    for (int i = 0; 
         i <= keys.Length - 1; i++)
    {
      if (keys[i].Equals(key))
      {
        return vals[i];
      }
     }
    return null;
   }
   
  public int NumberOfItems
  {
   get
     {
      return keys.Length;
     }
   }
 }

Yakir was baffled. "Why didn't you use the built-in Hashtable class from System.Collections?" he asked.

"I checked it out," James explained, "but it had too many functions. In my experience, that means it'll be slow. My class only has 3 functions, so it's much more efficient."

After a few weeks on the job, Yakir learned a valuable lesson: One's title had little to do with one's coding skills.


About the Author

Alex Papadimoulis is a managing partner at Inedo LLC and publisher of the Web site "Worse Than Failure" (WorseThanFailure.com). He writes the DevDisasters page in every issue of Redmond Developer News.

Reader Comments:

Fri, Apr 30, 2010

Wow. Seems like some of the people commenting clearly missed the whole point of the story. They saw it as an invitation to try and show the "right" or "best" way to handle collections. You guys are about as clueless as the senior developer in the story.

Sat, Apr 17, 2010

I've had to rewrite a lot more code (or make them rewrite it, more often) from junior programmers who hadn't learned to do their homework than from people who miraculously got to senior positions without learning anything. It's fun to poke at stereotypes, but goofy stuff from recent grads is a lot more common. It just isn't worth writing about since it's everywhere. If you want a challenge, try organizing a team of inexperienced programmers such that they create something that doesn't make you cringe.

Fri, Apr 16, 2010

Arrogance is not something tied to a title. That and one's ability and attitude define one's character. As an older guy who was a senior developer, I now work under an inexperienced arrogant junior developer who makes tons of mistakes and insists I follow him. I know better. Instead I am doing my best to learn the right ways to use these new tools. Still, there is plenty to learn from both old and young developers. The key is listen to advice when offered. You don't know what you don't know until it is pointed out.

Tue, Mar 23, 2010 Scott

Wow, talk about unclear on the concept - in fact, several concepts all at once. There's almost nothing right about the senior developer's approach or thinking here. In my experience, I've had to rewrite very little code from junior programmers because they don't tend to get fancy. The only code I really had to rewrite was written by a "senior developer" who wrote algorithms so obfuscated that the bugs were impossible to find. An arrogant "senior developer" who doesn't have the chops to back it up is a very dangerous employee.

Fri, Sep 25, 2009 Charles

Even worse, many developers don't know the framework well enough, so they end up recreating something that the framework already provides. For example, if you wanted to bind to a sorted list. I've seen developer create a foreach loop, adding an item to a list box for each iteration in the loop. Rather, you could use the sorted list as the data source for the dropdown. They "key" or "value" can be assigned the datatextfield or datavaluefield of the dropdown.

Fri, Sep 25, 2009 Charles

I've rarely seen anyone knowledgeable enough to discuss in a meaningful way the tradeoffs between collections. I would say that if a developer does not know collections well, I would not consider that person a senior-level programmer. Each collection, whether it be an array, array list, sorted list, hash table, or generic list, has a specific purpose.

Tue, Aug 11, 2009

I have seen both extremes, a senior programmer with 5+ certifications who doesn't have a clue of how write OOP, and a purely Sql Administrator getting the point on the same fairly complex .Net concept, way ahead of the senior devs.

Sun, Aug 9, 2009

I could point out that whether storing by index or key/value pair is not surely the most overriding reason you choose an arraylist over a hashtable but rather how you will be using the collection (searching vs adding removing etc) but that might be considered pedantic?

Fri, Jul 31, 2009

In my career, I've rewritten far more code from Junior developers than I have from Senior developers. Sure there are exceptions to titles and there are many people who do not know what they are doing in more professions than just Software Development. Help him out with code and become friends, then if he has good character, will reciprocate in an area you're not the most knowledgeable in. We all have things to learn.

Add Your Comment:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above