Prettying up your code

I hate working with messy, inconsistant code. I'm sure I'm not alone in saying that. But sometimes, writing neat code can be a bit of a chore and that's where a couple of online services may come in handy!

Firstly, I like my code beautifully indented and presented. I don't like mumble jumble like this:

function exampleCode ( a,  b) {
var under_score= "let's leave the semi colon out!"
      var camelCase ="Which side should I put space?";
    var avar = "or even spacing";
    // and stupid uneven indents
        // should I use spaces?
    // or tabs?
}

Luckily, there is jsbeautifier.org to the rescue. Simply copy and paste and we're good to go!

function exampleCode(a, b) {
    var under_score = "let's leave the semi colon out!" // Notice how the semicolon is still missing, doesn't fix that :(
    var camelCase = "Which side should I put space?";
    var avar = "or even spacing";
    // and stupid uneven indents
    // should I use spaces?
    // or tabs?
}

But now with all the code looking beautiful, we need to optimize it for deployment. That's when we have to minify our code! And that's where YUI Compressor (from Yahoo!) comes in handy! It is a library that minifies the code and also gzip's it… and gpbmike has created this online tool that pushes the code through the compressor (see here).

function exampleCode(d,c){var f="let's leave the semi colon out!";var e="Which side should I put space?";var g="or even spacing"};

Side note: I believe this is what Wikidot uses to minify its JS code!

Now, the code is nice and beautiful and can be easily minified to be deployed!


comments powered by Disqus

Old Comments

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +

Add a New Comment

Unless otherwise stated, all of the site's contents (including articles, blog post and code snippets) is:
Copyright © Kenneth Tsang 2014