Solving Problems I’ve Already Solved

Yesterday, I was working on a small, single page website, and I needed a way to filter the events on the page using a live-update search (results change as you type). Quite some time later I got something that resembled a working solution. Strutting proudly, I handed my laptop to my fiance and said, “Try this out!” expecting glorious praise and much applause. Instead, I got  “Why does it do that?”

Of course, she was absolutely right to question what it was doing (she usually is right). Firstly, I hadn’t really implemented a live search (you had to hit the enter key to update the results). Secondly, if you wanted to make all of those sad, hidden non-match entries reappear, you had to clear out the search box and hit Enter again.

I returned to my desk slightly deflated and resigned to the fact that I’d need to start over from scratch. The changes I’d need to make already zipping around in my head, I realized I’d already solved this problem before. I’d written a little web app that had exactly the search functionality I wanted to use on this page. I couldn’t believe that I’d forgotten about it.

I sat down at my desk, brought up the previous code, did a bit of copying and pasting, and in about 10 minutes, with very little modification, I had exactly the functionality I’d spend the last hour and a half trying to implement. For anyone who’s interested, here’s the little solution I’d forgotten I’d already created.


$('#event-search').on('keyup', function(){
  var query = $(this).val();
  var filter = new RegExp('.*' + query + '.*','i');

  $('.event').each(function(i,value) {
    if ($(this).children().text().match(filter)) {
      $(this).show();
    }else {
      $(this).hide();
    }
  });
});

It’s simple and straight-forward, and I’m not entirely sure why I didn’t think of it the second time, but the point is, I shouldn’t have had to think of it again. Hopefully, the next time I sit down to write some code, I’ll remember I don’t always have to start from scratch even if managing to remember this lesson means my fiance will miss my exasperated look when I realize “Hey, I think I’ve done this already” after an hour of work.

Hello world!

Have to start with a “Hello World” right? I must have written that first program in 20+ different languages now, so it only seems fair that my first foray into blogging will begin the same way. I’ve been programming for years now, going back to my time in middle school when I was trying to get my Ti-83 to play a quick text-based game with me. When I was analyzing black holes for my undergraduate research, I learned a bit of Perl and quite a bit about Python. I didn’t really do too much with either of them after I graduated; I wrote a few vPython simulations for a high school physics class I was teaching, but that was about the extent of it.

Last year, I decided to build a website for my physics classes. I wanted something that was a mixture of a blog and an online homework platform for my students. I started learning PHP, JavaScript, SQL, jQuery, and Bootstrap; pretty soon I was enjoying the programming more than the content I was teaching. It reminded me of just how much I enjoyed programming, so I started learning about things beyond just what I needed to put the website together (you can check out McDuffPhysics.com if you’re interested).

Somewhere in all this digging around on the internet, reading articles, watching videos, and asking questions, there came a point when I realized that this was what I wanted to do for a living. I love coding, and I really want to love what I do everyday. So, that’s what has brought me here. I’m going to share my journey to becoming a developer with all of you fine folks out there on the internet. If I say something wrong, by all means correct me because that’s what this is all about. If you have a really cool way to solve a problem I’m working on, share it with me. And in those rare cases where you have questions that you think I might be able to answer, shoot me an email or leave a comment and I’ll try not to disappoint.