28 Oct 2010 Ferado

How to be a good web developer

I recently added live chat to my site in hopes of helping my readers and picking up more business. Adding live chat is easy when you use a service. I’m using Olark. They offer different plans, depending on your budget; I’m using their free version.

When implementing the code, I had a few different, easy to implement, options:

  1. Copy and paste the code, as is, without change (most slackers do it this way).
  2. Put the code in a theme function with a descriptive function name using the wp_footer hook.
  3. Put the code in a plugin, existing or new, using the wp_footer hook.

I’m sure the quickest and easiest way would have been to use option #1. It’s easy to implement and it takes 2 seconds. Right? Well, here’s my problem with that approach. You have ugly, loose code in your theme. That right there should scare you away from that approach; however, your average developer will do it this way. It’s sad, really. What you need to understand is that being a good developer does not necessarily imply that you’re the fastest. Remember the story about the tortoise and the hare? This approach is horrible. Having un-organized code just laying around is bad. What happens when you need to make changes? What happens when you forget what the hell that code is? Forgive my cursing, sometimes I forget where I am.

The second approach is better; however, what happens down the line when you need to change the theme and you want to keep live chat on your site? You’d have to move the code over to the new theme.

My approach, #3 from the preceding list, is better and only takes a few moments to think about what you’re doing before doing it. What I’m suggesting here is creating a new plug-in or using an existing one that matches what you’re trying to do. In my situation I had to create a new plug-in named “Barton Enterprises” because of the new company I’m forming. I’ll be putting my live chat code on many sites. Therefore it makes sense to put it in a plug-in like this. Even if I wasn’t putting this code on multiple sites, it still makes sense to put the code in a plug-in for reuse. This is EXACTLY what your teacher in school would have told you. Unfortunately, in the business world, I’m one of the few people to do it the proper way.

The code using the wp_footer hook

[cc lang=”php”]

//… preceding code

add_action(‘wp_footer’, ‘bartonenterprises_livechat’);
function bartonenterprises_livechat() {
echo ‘…code…’;
}

// … other code

[/cc]

Moral of the Story

What I’m trying to teach here is that it’s better to take a moment and think about future use cases before working on a project or developing anything. Lots of people out there are just there to knock things out and do things quickly (the first time) without thinking about future implications. Time and time again these people have to go back and fix their work.

I’m merely suggesting that you write your code to last. Is that too much to ask?