How to write better JavaScript code![]() Creating beautiful and readable JavaScript is hard. Most JavaScript code is hard to read, reuse and maintain. I want to improve this and in this guide I will share some of the tips I have learned over the past couple of years. This guide aims to give tips on:
Get the basics of JavaScript correctYou have to get the basics correct. You have to write readable code, use proper variable names and avoid influences from your 10 years experience writing PHP. JavaScript isn't Java, it isn't Scheme, it isn't Python and it isn't PHP. Write in JavaScript using JavaScript conventions and JavaScript idioms. If you don't know what these are then read and study how JavaScript experts write their code.
The web is filled with unreadable JavaScript that looks similar to this: p.highlite = function(i) {
var $ = this;
$._invalid(i) || ($._invalid($.i) || ($.l[$.i][2].className = "normal"),
$.l[$.i = i][2].className += " selected", $._callEvent("onhighlite", $.l[i][0], $.l[i][1]));
};
This may be smart, but having thousands of lines of code that's written like this is a maintenance nightmare. Don't outsmart yourself, but don't write dumb code either.
A good start to improve your JavaScript basics is following guide, you can on the internet find a lot of other guides:
Consistent naming convention and white spaceHave a consistent naming convention, it will make your code more readable and easier to maintain. My convention for writing JavaScript looks like this:
Use whitespace to improve readability, for example:
Use a libraryUsing a JavaScript library will make you more productive since most libraries solve browser compatibility issues and provides lots of code for your disposal. Pick a library that fits your style and learn it well. Some of the libraries that I recommend: Structure your code in directories and filesUsing one file for your JavaScript code is fine if you only have 30 lines of JavaScript. But avoid this if your project requires large amounts of JavaScript or if you plan to reuse your JavaScript code. This is a big problem and a lot of good programmers make this mistake. Here are two examples:
If you use one file scheme then your JavaScript (and CSS) will be highly coupled, which in most cases isn't good, as high coupled code will be harder to understand, maintain and reuse. Have a low coupling and high cohesionThis is a more advanced topic, but it is crucial to understand as it's the only way to write reusable code. Your JavaScript and CSS should have a low coupling to other components and your site. This gives following advantages:
For more information read more about coupling on Wikipedia. To achieve low coupling view your application as a collection of components and try to derive general components that aren't that dependable on each other. This will result in high cohesion and make it easy to reuse and maintain your site. This is all, I hope you have found my tips useful and do share tips in the comments that you follow.
27. Apr 2010
•
Code
·
Code improvement
·
Design
·
JavaScript
·
Tips
|
|