Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making your javascript maintainable [closed]

I am in the process of converting an internal application to use more Ajax via jQuery. I am moving away from the standard code behind of the current ASP.NET application and incorporating JavaScript methods that are going to run client side. My concern is what is the best method for allowing this application to stay maintainable for those who come behind me.

like image 496
Bobby Borszich Avatar asked Jan 29 '09 16:01

Bobby Borszich


People also ask

What is Open Closed Principle in JavaScript?

The Open-Closed principle states that code should be "Open for extension" and "Closed for modification". There is quite a lot of confusion about the term, but essentially it means that if you want to implement a supported change, you should be able to do it without changing the code in a large number of places.


2 Answers

I've found that build scripts are the key to success in maintainable Javascript. Essentially in order to have something fast and concise on the final page you need a script to make that maintainable in development.

If you use a build process you can break your JS into files based on the functionality. You can use the debug versions of libraries during development, and leave glorious code comments smattered around your code with impunity.

A build script should:

  • Lint all your code
  • Strip comments
  • Concatenate the files together
  • Minify/Obfuscate/Compress the code
  • Version files for deployment
  • Push the version files to the server ready to be "turned on" in production

This leaves you to work on files that make logical sense and it to tidy up after you for production.

like image 174
sh1mmer Avatar answered Sep 21 '22 05:09

sh1mmer


I would say that applying the same sort of OOP principles to your javascript that you apply to server-side code is one of the best ways to enhance maintainability. Instead of global functions, think about creating classes that encapsulate your javascript methods and data. Use javascript unit testing frameworks to test this code to make sure that it is robust.

like image 27
tvanfosson Avatar answered Sep 22 '22 05:09

tvanfosson