By no means should this article serve as the bible of CSS & JS coding standards, though it should get the ball rolling for those who are unsure where to start. Over the years, I’ve had the privilege of working with many talented developers, each of whom have left little bits and pieces of their coding style behind. From those pieces I’ve assembled this article.
Let’s begin…
Alphabetize Your Code
Alphabetizing your code is an extra organizational step often overlooked. It’s especially helpful in PHP classes containing getter and setter functions, but it also comes in handy for CSS and JS. Often times similarly named elements will be somewhat related in function, so clustering them together makes them easier to find.
I’d recommend alphabetizing everything from JS class and function names, to CSS classes and ID’s. You’ll notice that every code example in this article is alphabetized to further exemplify the point.
Comment Everything
Comments are essential, especially among devs working in groups. The styles seem to vary slightly from person to person, but the theme is the same – you can never comment too much.
Here is my commenting style for JS / PHP:
/******************************************************
* Cart.class.js
*
* A class of JS functions associated with the
* tasks performed by the shopping cart.
* @author Koa Metter em@mail.com
* @copyright Copyright (c) 2010, Company
******************************************************/
var Cart = {
// ...
/**
* Add item to cart
* @param string item_id
* @param string item_quantity
* @todo Check for string validity
*/
addCartItem: function (item_id, item_quantity)
{
if (!Utilities.isEmpty($('#cart-items')))
{
Cart.AJAX({
data: 'call=addOfferingToCart&args[0]='+item_id+'&args[1]='+item_quantity,
// Things we'd like to do upon 'status => success' ajax response
success: function (msg)
{
Cart.displayNewCartItem(msg);
// Update cart total price
Cart.displayCartTotal();
// Also update checkout rebilling terms!
if (msg.response.rebilling_terms_agreement){
$('#billing-terms-agreement-label').html(msg.response.rebilling_terms_agreement);
}
Cart.proceedBtnCheck();
}
});
}
}
}
When commenting in PHP, phpDocumentor can be used to build a comprehensive dev manual for your source code. I use the same documentation style for JS simply because I come from a PHP background.
Though CSS doesn’t need the phpDocumentor-type of commenting, it should nevertheless remain detailed to explain the elements and their purpose. Clustering elements that are related to the same stylistic area or function of the website helps in organizing without the need of many comment blocks.
Indent CSS
Indenting your code is an obvious standard with most programing languages, but often times CSS is left out of the loop. Indent a CSS element if it’s a child of a parent element, like so:
#container {
color:#222;
width:100%;
z-index:1;
}
#container .lg-block {
height:300px;
}
#container .sm-block {
height:100px;
}
.etc {
/* ... */
}
The above CSS example shows that the class .lg-block is specifically contained within the ID #container, thus we are indenting it to visually represent the same thing.
OOP-ish Javascript
This topic deviates slightly from actual “coding standards”, but I think it’s equally important – how to format your JS into usable, organized classes. This rule takes into account all other coding standards (alphabetization, indentation, commenting, etc) and applies them to JS, adding in OOP-like objects.
Here’s an example:
var Vehicles = {
/*
* Give us the word "Bus"
* @return string
*/
bus: function ()
{
return 'Bus';
},
/*
* Give us the word "Car"
* @return string
*/
car: function ()
{
return 'Car.';
},
/*
* Give both car() and truck()
* @return string
*/
getCarAndTruck: function ()
{
return this.car + ' and ' + this.truck;
},
/*
* Give us the word "Truck"
* @return string
*/
truck: function()
{
return 'Truck.';
}
}
// Let's call the function getCarAndTruck like so:
Vehicles.getCarAndTruck();
Shuffling your JS into objects helps associate functions with tasks / groups and thus creates a more organized codebase.
Hopefully this helps. If you have a different method of commenting and/or organizing your CSS or JS code, let me know!

[...] here to read the rest: CSS & JS Coding Standards / Organization [OOP] » Labs This entry was posted on Thursday, July 1st, 2010 at 11:55 pm and is filed under Uncategorized. [...]
- Indenting -
Coincidental that you bring up alphabetizing and indenting… I started writing a post for my site saying the exact same thing. I’m particularly fond of doing this in CSS…
I stopped writing my post because I started writing a script that would do this for me on existing files. It’s panning out alright so far… have a few more features to implement, then will make it public. (didn’t look to see if anything exists for it yet, but it’s a fun little project.)
- Commenting -
About the commenting style… I always think it looks nice, but, my boss actually drove it home to me that /**/ comment style should be reserved for block commenting CODE while debugging, rather than notes.
It’s pretty irritating to get into an old project (that is less than OO friendly/themed…) and start commenting sections of code, then have to change the original comment style just so you can debug. It’s easily avoided by using # and or //.
Now, I do bend the rule a bit… When I am documenting an object, or contents of a file (at the TOP of the page). I think block comments OUTSIDE class/method/function definitions are OK. But, I still use # and // as much as possible any other time.
The only other exceptions I have to this rule are Java and CSS. I don’t have to work much with Java but even then, I use a lot of // to start the comment line. CSS forces you to use /**/ so… I do. :)
Ultimately, it’s preference, yes… If you’re the only developer, and are a good OOPer, you’re probably fine using block commenting. Don’t be surprised if the project moves to another [set of] developer[s], and one or two of them gripe about it though. :)
I should just update and publish my post… TO BE CONTINUED? We’ll see. :)
Cheers – welcome back.
If that alphabetizing script you write happens to be downloadable, please please please post it here. I have about a million uses for it.
Thank you for the comment and hope to hear more from you soon…
You got it – I didn’t end up getting back to it this weekend… time and plans got away from us. :/ I will definitely give you a heads up when it’s public, though.
Thanks for expressing interest in it. Like I said, it’s been a fun project so far. Not uber-complicated.. just trying to make sure I accommodate for as many layout-style cases as I can before I turn it loose. (definitely honing my regular expression skills!)
Update on the CSS formatter…
I’m going to have to start over… a buddy left some less than clean software on my pc, and I ended up with a pretty bad rootkit. I had to wipe two drives on my desktop, reform two of our personal laptops, get a new Xbox 360, and re-flash the firmware on my modem & router because of it. Nasty stuff. I’m still in the process of getting new financial info; debit cards, credit cards, bank account numbers, paypal, etc.
Symantec failed. Prevx failed. And not surprisingly, MS’ Defender failed as well. Don’t have a name for it, but, anyone reading this, be on alert. It’s not been fun.
Hopefully this weekend I’ll get some one-on-one time with it, again.