Archive for the ‘Javascript’ Category

Learning Javascript the Right Way

Tuesday, May 6th, 2008

What?

Marijn Haverbeke is the coder behind CodeMirror, the best in-browser code editor available on the web (I have considered MarkitUp, Helene, EditArea, and used others such as Heroku’s in-browser editor). As an exemplar of the open source spirit, it is widely available to use as you please (e.g. BSD, not GPL). 

I just stumbled upon his Elequent Javascript Tutorial, which is by far the best javascript coding tutorial I have come across (and I have searched far and wide!). In my mind it is the Javascript equivalent of Why’s (Poignant) Guide to Ruby

His tutorial is not only highly interactive, with examples that run right in your browser as you read, but also teaches excellent coding practices.

Anyone looking to learn how to program, or specifically are looking to learn javascript, should read this. Thank you Marijn!

Thoughts

Most source developers I know code because they are good at it, enjoy it, and feel they do something worthwhile with it. There is a definite reward.

It takes a completely different mentality to use your time (which is precious indeed at the level of design skill we see in Marijn’s work) and put in a significant effort into having others learn what you know.

Pretty Code on your (wp) Blog

Tuesday, May 6th, 2008

What? How to make your code look pretty on your blog with minimal effort. Meaning language appropriate highlighting without even specifying which programming language the code is in. Why? Because

/**
* A miss manners guest
* @constructor
* @param {Object} params The parameters object. All other parameters are named properties of this object
* @param {String} name The guest name
* @param {String} gender The guest gender. 'male' or 'female'
* @param {String} interest The guest interest. A single string. You should give guests interests so that some match up - this is what the rule engine bases their placemenet on.
* @param {String} color Hexadecimal value of the color to represent the guest with.
*/
MissManners.Guest = function(params) {
  this.name = params.name || this.Default.name;
  this.gender = params.gender ? params.gender.toLowerCase() : this.Default.gender;
  this.interest = params.interest ? params.interest.toLowerCase() : this.Default.interest;
  this.color = params.color || this.Default.color;
  this.id = 'MMGuest-' + MissManners.nextUniqueId++;
}

Looks infinitely better and is much more readable than

/**
* A miss manners guest
* @constructor
* @param {Object} params The parameters object. All other parameters are named properties of this object
* @param {String} name The guest name
* @param {String} gender The guest gender. 'male' or 'female'
* @param {String} interest The guest interest. A single string. You should give guests interests so that some match up - this is what the rule engine bases their placemenet on.
* @param {String} color Hexadecimal value of the color to represent the guest with.
*/
MissManners.Guest = function(params) {
this.name = params.name || this.Default.name;
this.gender = params.gender ? params.gender.toLowerCase() : this.Default.gender;
this.interest = params.interest ? params.interest.toLowerCase() : this.Default.interest;
this.color = params.color || this.Default.color;
this.id = 'MMGuest-' + MissManners.nextUniqueId++;
}

But this can be a pain to get right (you know if you’ve tried). There is an easy way, especially for all us WP bloggers. How? With wordpress, the easiest thing in the world. Just download Google Code Prettify for wordpress v1.1 to your wordpress wp-content/plugins directory with


curl -O http://www.deanlee.cn/downloads/google_code_prettify_v1.1.zip
unzip google_code_prettify_v1.1.zip

Then go to your wordpress plugins admin area at /wp-admin/plugins.php, and activate Google Code Prettify. The next time you write code, just wrap it in <pre class=”prettyprint”> /* your code here */ </pre> and the plugin takes care of the rest. Seriously - you don’t even have to specify the programming language your code is in. *Thank you!*