Blog Moved

My blog has moved!

Monday, February 2, 2015

Javascript Practices Make Perfect

Hello again, I'm back with another short blog about javascript.  I was really surprised and pleased to see the amount of positive feedback that my last blog post generated.  The idea of Javascript scoping is one of those things that many of us think of as best practices.

But at the end of the day, everyone has a different skill set, and a different background.  I recently was talking to a colleague, someone who I do have a great deal of respect for as a developer and a friend.  We had to go in for a technical interview, to talk to a potential resource.  And during the interview, I asked a bunch of javascript questions.  After the interview was over, he and I talked offline.  And got into a bit of a debate.  His point was that Javascript is "the duct tape of the internet", and "it's the quick and dirty way of doing things when there is no other option."

Now, do I disagree with him on this point.  Absolutely, but at the end of the day my point is that there are a lot of people out there who believe this.  If you had asked me 5 years ago, I would have totally agreed with that statement.  But then Javascript was a language plagued by cross browser portability issues, and general problems.

But that was then, and this is now.  Since then, thanks to frameworks like Knockout.js, Angular.js, jQuery, Jasmine, etc.  Javascript is no longer the "red-headed step child" of web languages.  And instead is a fully formed language that can provide a lot of functionality and even be the primary focus of business logic (in the care of Angular).

Here are some practices, I definitely recommend for javascript:

  • Scoping the logic:  See my previous post, where I discuss the idea of how to scope javascript code and why that scoping is appropriate and necessary to maintain modular code that is self contained with minimal coupling and chance of error.  
  • External Files only, no others need apply:  With very limited exception, you should always be writing your javascript code in external javascript files.  This is a point that many people will likely disagree with me, as they will say that if you are using scoped javascript, what does it matter.  Additionally, you probably are thinking.  Kevin, if we use external files, we can't leverage Razor syntax in our javascript files.   In my experience, this is a bad practice, and a bad habit to get into.  The problem is that by using Razor syntax in your code you are creating a tight coupling between your view, and the model and your javascript code.  This flies directly in the face of general best practices, as your code is no directly dependent on the elements in the view.  I will talk about ways to get around this in my next post.  It also creates code that is not testable, and in this day and age there is no excuse for that.
  • NO HARD CODED URLS:  Honestly, if this were server side code, this would be a no brainer.  But for some reason many a developer I know feels the need to hard code urls when they right jQuery AJAX calls.  I don't know why everyone seems to think this is an excuse, but Javascript should never be an excuse for bad practices.  Now, I know this falls into the same category as people who say you should be writing your javascript embedded, and using Razor syntax.  Again, I argue here that you are creating a tight coupling, which is a mistake.  If you are going to utilize "@Url.Action", I would argue a better practice would be to put that value into a hidden field and have your "scoped" javascript object grab the hidden field to populate the url.  What you'll find is that you now have javascript code that can be dropped onto other forms and be in an external file.  
That's all for now, I just wanted to take a quick talk about javascript.  I'm going to do more articles in a series, and next topic is going to be about separating the javascript logic from your URL.