Don’t Code by Accident

minute read. Posted on December 11, 2011 in Code & Design

I’ve been a front-end developer for about 6 years now and feel like I pretty much understand every line of HTML or CSS that I write. However, now that I’m teaching myself a server-side programming language (Ruby on Rails), I find my self understanding only a measly portion of the code I’m writing. And that’s wrong.

I just finished reading The Pragmatic Programmer and in it the author talks about “programming by coincidence”. There are a number of reasons why coding by accident or by coincidence is a terrible way to write software or web applications, but here’s the biggest one that I’m finding out firsthand: it’s almost impossible to fix things when you don’t even know how you created them in the first place!

Like I said, I’m pretty guilty of programming by accident and just going with code that works, without knowing HOW it works. I’m trying to slow down though and realize what methods do, what functions are actually saying, how my models are interacting with the database and controllers, etc. And by doing so, I’m starting to make up the time I lost over the past couple months getting a certain feature to work for a few days, then having to go back and redo it or spend hours debugging after I screwed something up, by coding more deliberately. However, I still need help!

Here are some ways I’ve been using to try to code more deliberately and less by accident:

  • When you post a question on StackOverflow or Forrst or wherever, don’t just take the bits of code those oh-so-generous folks write for you. Poke around with it and figure out why it works. Ask the person for questions on why it works. They’re usually more than happy to answer! But don’t just plug it in, go on your dandy coding way and forget all about it.
  • Start over. And over. And over. When you get something working, start over and try to write it again. This time without looking at tutorials or books. You’ll learn by repetitiveness.
  • Fully grasp the issue it is you’re about to write code to solve before you write it. Jumping in to your IDE and writing code will only serve for confusion and get you frustrated if you don’t know exactly how you want your models to interact, your databases set up, and so on.

Slow down and grasp what the code you’re writing is doing before you move on to the next feature set. It gets more fun when you actually understand it. And unfortunately for me, I’m not the king of the coding playground just yet, so fun is many times replaced with frustration as I learn to code more deliberately. Happy coding!