Unmaintainable Code
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:
But for that I have tests :-)
 

Comments
That first bit of code is great... though I would throw that pesky BuildSpecialCase delegate away and return a Func<Control, TextBox> instead ;o)
I occasionally end up with similar generics "hell" in production code... it's one of the few places where I miss macros...
When I end up with generics like that, I usually make my self a class to neaten things up a bit.
Something like:
class NamedPolicyDictionary : Dictionary<Customer, PolicyDictionary>
{}
class PolicyDictionary : Dictionary<string, IList<Policy>
{}
So then you can do: NamedPolicyDictionary namedPoliciesByCustomers = new ...;
Ditto to what James said, I do the same thing. I rarely have any nested generics in production code. 3 nested generics I've never done. Too difficult to read, IMO.
Comment preview