Whenever I write code, I churn out garbage. I do it this way so I can get the general idea down and mess around with the problem. I don’t need a good solution to solve my problem while I’m in development, I need a working solution. Once I get a working solution, I go back and I optimize the code and I eventually write something that doesn’t look like a baby panda threw up on my keyboard.
Today I optimized this:
tags.each do |tag| tag.strip! tag_object = Tag.find_by_name(tag) tag_object = Tag.create!(:name => tag) if tag_object.nil? tag_list << tag_object end
into this:
tags.each { |tag| tag_list << Tag.first_or_create(:name => tag.strip) }
The actual refactoring I performed isn’t as important to me as the process. I write a lot of junk. I think it’s okay to write a lot of junk as long as you’re getting working ideas out in the wild, messing around, and making them work. Once you get those ideas working, it’s important to go back and clean them up so that you can maintain your ideas, build on them, and grow them into something bigger and better.