Unmaintainable Code

time to read 5 min | 812 words

I have several ways of categorizing code. Production quality, test, demo, and utilities. When I am talking about utilities, I am talking about little programs that I use to do all sorts of small tasks. There I rarely worry about maintainability, and today there was a gem here:

public delegate Control BuildSpecialCase(TextBox textbox);

private BuildSpecialCase GetSpecialCaseControl(string name)
{
 if (name.IndexOf("ConnectionString", StringComparison.InvariantCultureIgnoreCase) != -1)
 {
  return delegate(TextBox t)
  {
   Button change = new Button();
   change.Click += delegate { t.Text = PromptForConnectionString(t.Text); };
   change.Dock = DockStyle.Right;
   change.Text = "...";
   change.Width = 30;
   return change;
  };
 }
 if (name.IndexOf("File", StringComparison.InvariantCultureIgnoreCase) != -1)
 {
  return delegate(TextBox t)
  {
   Button change = new Button();
   change.Click += delegate
   {
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Multiselect = false;
    dialog.FileName = t.Text;
    if (dialog.ShowDialog(this) == DialogResult.OK)
    {
     t.Text = dialog.FileName;
    }
   };
   change.Dock = DockStyle.Right;
   change.Text = "...";
   change.Width = 30;
   return change;
  };
 }
 if (name.IndexOf("Folder", StringComparison.InvariantCultureIgnoreCase) != -1 ||
  name.IndexOf("Dir", StringComparison.InvariantCultureIgnoreCase) != -1)
 {
  return delegate(TextBox t)
  {
   Button change = new Button();
   change.Click += delegate
   {
    FolderBrowserDialog dialog = new FolderBrowserDialog();
    dialog.SelectedPath = t.Text;
    if (dialog.ShowDialog(this) == DialogResult.OK)
    {
     t.Text = dialog.SelectedPath;
    }
   };
   change.Dock = DockStyle.Right;
   change.Text = "...";
   change.Width = 30;
   return change;
  };
 }
 return null;
}

Can you figure out what is going on?

As long as we are talking about unmaintainable code, here an example from code meant for production that I just wrote:

IDictionary<Customer, IDictionary<string, IList<Policy>>> namedPoliciesByCustomers = new ...;

But for that I have tests :-)