Search This Blog

Sunday, March 28, 2010

Maintainable JavaScript



What it means
Code should be Understandable to others as well as for us after sometime, Intiuitve,Adaptable, Extendable, Debuggable
Maintainability is writing or making code easy to understand and easy to debug

Why it is needed


  1. Mostly we are maintaining the code rather writing a new piece.

  2. It takes more time to figure out where we have to look out to solve a problem comparitively to create a solution for the problem

  3. It will be helpful for Co-worker present and in future.

  4. It will be helpful for Development community if we work for opensource projects
How it can be achieved

  1. Indenting Code which is easy as well as very important

  2. Commenting the code atleast at the top of each function and classes , while we use large code chunks , if we write any new algorithm or if we write any hack for display purpose on various browser

  3. Variable name should be nouns and function name should start with verbs with their usage.

  4. In client side we have three components they are JavaScript (behaviour),CSS(Presentation) and HTML (Structure )

  5. Maintain these separate dont mingle these things in one file which will leads to confusion.

  6. Keep Bussiness logic out of event handler.

  7. Dont extend objects which are native (Array,String,RegExp)

  8. Dont override methods which are from native objects

  9. Functions and variables should be attached to an object

  10. Dont use global variables instead use var Constants={name:value}

  11. Throw your own errors

  12. Avoid null comparisons (variable =='null' instead use variable instanceof Array or typeof variable =='string')

  13. Use constants when it is UI string ,repeated value ,URL and values which may change the future

No comments:

Post a Comment