As
technology advances, it's also becoming user friendly and easier to
implement. Then how can JavaScript lag behind? Over the past few years,
using JavaScript has become very easy. In early days, there was a need
for developers to know about every browser to write JavaScript code
compatible to them. But these days, there are many libraries such as
Dojo, MooTools, jQuery and YUI, which allow a person without any
knowledge of JavaScript, to work with the HTML code of a website.
No matter how much JavaScript has emerged, the ground rules of the
script didn't change - maintainable, solid and secure JavaScript. So
let's go through seven sins which you should not make to your code if
you don't want to be screwed up while delivering, maintaining and
handling the code to a third party.
One of the biggest problems with JavaScript was that it was browser
specific. Many of the codes in JavaScript were written to be compatible
to IE6 when it was the state-of-the-art browser. This is the reason
that IE6 can't be removed completely by many companies and the newer
versions of IE have to be compatible to support these IE6 specific
applications. The role of JavaScript libraries is to make the
development predictable so that developers can be relieved of the
pressure of browser specific coding.
Listed here are some JavaScript common mistakes that should be kept in mind while developing JavaScript code:
1. DOM1 event handlers, global variables and functions: Problems that may arise because of these are illustrated below:
* If a variable or function name is defined, and any other include
statement uses the same name, then the initial one will be overwritten.
Solution: use closures, var keyword, namespace your functions and variables to avoid this problem.
o The same concept applies to the event handlers if they are attached in the old on-Event way.
Solution: don't use on-Event handlers but the DOM level 2 event handlers.
o The rights of every script are same in HTML and a script can
overwrite what other scripts have done before, if there is a need.
Solution: Wrapping of functions in more unique names can prevent this problem.
2. Expecting that everything is right and thus not testing:
expectation that every element in the code is in the right format, the
values which user enter are in the right format and every HTML element
you are trying to enhance is available.
Solution: use typeof, indexof(), split, length, alerts() etc.
3. Use of wrong technology for the work: the maintenance becomes difficult and quality of code deteriorates.
Solution:
o Regardless of the display environment, essential content and mark-up should be written in HTML.
o CSS should be used to maintain "look and feel" elements.
o JavaScript should be used for interactions with user that goes beyond hover effect.
4. Although JavaScript is easy to use, but all the functionalities can't be implemented in it, so don't promise them.
Solution:
o JavaScript should not be used for any vital functionality of product.
o If you build anything with JavaScript, add it to document using DOM>
o JavaScript functionality should work with keyboard and mouse.
5. Making maintenance of code unnecessarily hard: because of work environment, deadlines and hiring practices.
Solution:
o Make form without using JavaScript.
o Keep maintenance of all HTML in the same document.
6. No documentation of code: Documentation is important
because coding styles change with time and technology, hence the code
will be easier to understand if documented.
Solution: use comments, especially with sections like start of
classes and reusable functions, third party code, sections with
dependencies.
7. Optimizing for people, but not for machines.
Solution: If you can do anything which can make the work of other machines easier, then do it.
Just by following these simple steps, you can make your code easier, cleaner and long lasting.
|