Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, October 18, 2010

Javascript patterns and antipatterns

Original post here



This most likely is just the first in a series of posts on the matter. Let me preface this post with the thought that I've come across quite a bit of code in my professional life. When I was a bit younger and happened to come across a piece of code that I knew was horrible, I'd was for the most part apathetic. But since being heavily involved in core Javascript for the past year from writing plug-ins to playing around with server-side JS and everything in-between, I've come across not only bad legacy JS, but dangerous JS that really does more damage than good. I guess this is to be expected because JS is so easy to get something going without know what is being done.

I admit that starting out I too fell victim to common bad practices, or antipatterns. It was only natural that I learned from reading someone else's solution to a problem without going into detail of what the language was actually doing; I wasn't concerned with that at the time. This is no longer the case since JS has gain tremendous traction in the development role. In order to have super premium code that will preform the way that it is expected to without doing harm to others code is to: seriously know the language inside and out along with adhering to certain design patterns that will not only be optimized, but safe, clean and easy to read.

Here is my little attempt to show little snippets that I've learn along the way that are essential for me to be 100% confident in my code.

One of the most common antipatterns that I've come across is the overuse of global variables. Yes, they are quick and easy but also potentially dangerous, not to mention annoying. But aside from my personal annoyance at the sloppy coding, JS does have some bugs within the language can have unforeseen consequences on the sloppy code. Along with the overuse of global variables, are sometimes the way the variables are defined.


// global namespace
myVar = 5; //antipattern
var myVar = 5;


Both of these statements are valid, and for the most part they will do what the author expects them to do, but they are different. The variable lacking the 'var' is called an implied global and the other is an explicitly defined variable.


// antipattern
function MyFunction( args1 ) {
myVar = 5;
if ( args1 === myVar ) {
return true;
}
return false;
}


This function is easy enough, it checks the parameter passed in with the author's intended local variable 'myVar'. Since myVar in this example is defined without 'var', it is an implied global. What author might not have intended is now 'myVar' has been added to the global namespace which can cause problems if there are variables already defined with the same name.

Corrected:

// correct pattern
function MyFunction( args1 ) {
var myVar = 5; //corrected
if ( args1 === myVar ) {
return true;
}
return false;
}


Another different between implied globals and explicit ones, or ones defined with 'var' is the ability to delete the variable if wished. It is safe to say if a global variable is necessary it most likely shouldn't be deleted and be available through the program's life-cycle. Variable that are implied globals can be deleted and explicit cannot. Seeing why this is a problem requires you to take a step back and see the true purpose of a global variable. Implied globals can be deleted which means that they are properties or elements of the global object. This is undesired for the purpose of truly using a global variable. Lastly, since we are working with Javascript and not the DOM 100% of the time, adding elements or global variables to the window object on DOM is ill-advised. To have your program less prone to bugs while still relying on using global variables no matter what environment you may be working in, create your own global object.


var globalObj = (function () {
return this;
}() );

Friday, August 20, 2010

Future Development

Being a developer is an interesting as well as an extremely frustrating job. I could work two weeks on a project with an additional two or three days debugging for my five second pay-off. As crazy as that might sound, I could not imagine doing anything else (except for maybe music or art....or philosophy...or even cultural studies).

Being a programmer, and more specifically web developer, has it's share of interesting problems that I couldn't find in any other field. This is the only field that I've come across that has new paradigms, revolutions, and shifts every couple of years if not faster. This requires an extensive amount of reading and research to stay at the top of the field consistently. Due to the reinventing nature of the field, traps and pit-falls can easily happen to the best of us. Either reading too much, which only allows for a theoretical approach without implementation or experience to reading too little which puts one behind the rest of their peers is a hard thing to balance. When reading the latest and greatest technology out, how does one know whether invest time into learning or simply just skip over? I struggle with this constantly.

By trade, I'm a PHP developer. The platform that I build on is an ever-changing, fast-paced one. But there is one thing that seems to make itself extremely obvious in the future of my field: javascript. Yes, this may sound like old news to some because javascript has been out since the mid '90s, but with the trend of where the Web is going, only recently javascript has started to make huge leaps and bounds ahead of anything as a staple of future web development. Tools such as jQuery (yes, also been around since the mid 2000), node.js, and many others has A) brought development with javascript to an extremely high level, B) development possible on the front end as well as the back (yes, all ECMAscripts are agnostic in theory, but the practice of such was rare) and C) it has branched out to mobile development such as pure scripting game engines, jqtouch, phone gap, and many others.

I've always been decent enough in javascript to get done what needed to get done, but lately I've been spending the majority of my time learning the language's popular design patterns and best practices. There is a wealth of information out there to turn a novice into an experienced developer. I've been spending some serious time developing jQuery plugins because I truly believe that my work will last longer in javascript than any other technology at the moment. It is a gamble, but I think it is a safe gamble.