Related link: http://c2.com/cgi/wiki?DontRepeatYourself

Question for programmers:

How do you know when you’ve found the right solution?

This weekend I was working on one of the toughest but most satisfying projects I’ve ever done. Everything was coming together beautifully. Things that had been messy problems for 8 months were all getting neatly solved using best practices (especially D.R.Y.) - I was really on a roll.

… and then I got stuck on a function that looked like this:


function blah($id, $rare_option='n', $seldom_used='', $something_else='', $ugly='') {
  switch($id) {
    case 'a':
      return $id . 'y__p' . $rare_option;
      break;
    case 'b':
      return $seldom_used . 'x|x|x' . $seldom_used;
      break;
    case 'c':
      return 'CDB_12345' . $something_else . $rare_option;
      break;
    case 'd':
      return $ugly * $ugly - $seldom_used;
      break;
    }
  }

(The above example is fake. The real function needed to create output filenames based on partner-company preferences, of which there are 5 or 6 different formats, based on different information inside the file.) I spent almost an hour trying to simplify it, but unfortunately that was as far as I could get. It was a drag to leave something ugly like that, knowing I’m going to have to look up the function every time I use it in the future, to find out what list of variables I’m suppsed to pass it, and in what order.

Which got me wondering how other programmers handle this situation: when you’ve written something that WORKS, but goes against your good taste in design. Is the ugly solution ever the best solution? Is it not right until it’s as simple as possible? I keep thinking that ugly solutions are always a sign of the wrong approach. Is anything that works "good enough"? Or do you strive to find the truly beautiful solution to everything? Thoughts, anyone?

Thoughts, anyone?