Wednesday, November 25, 2015

JavaScript Best Practices

Good practices:


  • If something is obvious but taking time to implement, innovate. Find ways to do it simply instead of hard way. If you are frustrated there is problem with code or with you or with boss or with environment. Fix it.
  • Create functions with single object parameter. Object parameter can have multiple properties. Then, the user of your function does not need to worry about order and number of parameters.
  • Use debug = true/false to load dev vs minified files.
  • Use debug = true/false to load files from production or from local server.
  • Use event bus (pub/sub pattern). It provides Loose Coupling.
  • Declare variables, on top. Then, use.

Worst practices to avoid:


  • Avoid fear to touch, modify, replace your own code later.
  • Do not rely too much on magic of frameworks doing workarounds around the black boxes such as using timeinterval
  • Aoid unpredictable code which may make you unsure that changes may impact unknown parts of your application
  • Using jugad for the time being to make the code work.
  • No fine control - by using your framework, tools automating in your code
  • Remove code duplicaiton. Do not repeat same code and logic at many places.
  • Not refactoring your code.
  • Code sticked to DOM structure does not allow to move the component from one page/place to another.
  • Sometimes things are obvious to implement but restricted by architecture/framework/library.
  • Do not have global variables except if absolutely necessary.


No comments: