Refactoring Code in to an Object-Oriented Paradigm #5: Cleaning Up Our Messy User Controls

This is article number 5 of a series of article, which simply documents my refactoring of some code I originally wrote (pretty poorly) last year. To get the gist of what’s going on. Check the original post – Refactoring Code in to an Object-Oriented Paradigm.

Cleaning Up Our Messy User Controls

We’re pretty close to being done here in terms of refactoring our Javascript. But the Auto-Scrobbler has a UI, an incredibly simple UI, but it still has one, and because this is a bookmarklet for a third-party website, the only way I can show this UI is by injecting the HTML on to the page. This not always the best way to do things, often it’s suggested that just the data is entered into pre-existing HTML on the page, but in this case it’s about the only thing we can do. But there are still best practices that come with this.

Here’s the current HTML that we inject into the page (post-injection):

<div id="autoScrobbler" style="background: #FFFFFF; border-top: 1px solid #000000; border-left: 1px solid #000000; position: fixed; bottom: 0; height: 50px; width: inherit;"><input id="autoScrobblerStart" type="button" value="Start auto-scrobbling" onclick="autoScrobbler.start();" /> | <input id="autoScrobblerStop" type="button" value="Stop auto-scrobbling" onclick="autoScrobbler.stop();" /><p><span id="autoScrobblerScrobbleCount">0</span> tracks scrobbled</p></div>

This is a horribly messy bit of HTML, having originally written this code quickly I haven’t even included any structure, it’s all just one flowing line. To make things worse I am using inline styles and, worse still, inline event handlers. The way I’m injecting the code isn’t great either, I’m adding it to the end of another element, which means the styles are liable to change and possibly break the UI.

Structure

That’s actually a lot of stuff to turn around, so we’ll just start with the structure. To get some structuring, we need to use the new line character at the end of each line (“\n” in most languages) in our Javascript. Like so:

var userControls = "<div id=\"autoScrobbler\" style=\"background: #FFFFFF; border-top: 1px solid #000000; border-left: 1px solid #000000; position: fixed; bottom: 0; height: 50px; width: inherit;\">\n"+
"<input id=\"autoScrobblerStart\" type=\"button\" value=\"Start auto-scrobbling\" onclick=\"autoScrobbler.start();\" /> | <input id=\"autoScrobblerStop\" type=\"button\" value=\"Stop auto-scrobbling\" onclick=\"autoScrobbler.stop();\" />\n"+
"<p><span id=\"autoScrobblerScrobbleCount\">0</span> tracks scrobbled</p>\n"+
"</div>\n";

Which should inject something like this:

<div id="autoScrobbler" style="background: #FFFFFF; border-top: 1px solid #000000; border-left: 1px solid #000000; position: fixed; bottom: 0; height: 50px; width: inherit;">

<input id="autoScrobblerStart" type="button" value="Start auto-scrobbling" onclick="autoScrobbler.start();" /> | <input id="autoScrobblerStop" type="button" value="Stop auto-scrobbling" onclick="autoScrobbler.stop();" />

<p><span id="autoScrobblerScrobbleCount">0</span> tracks scrobbled</p>

</div>

Better, but those inline styles and event handlers are still making things messy.

Styling

We’ll solve the inline styles first.

It should first be mentioned that those inline styles aren’t just messy but also frustrating. Inline styles are generally the most specific you can get with CSS to styling elements, which makes sense, by putting styles inline, they will only ever be applied to that element unlike any other CSS that you can apply. But what it means is that another developers, or even I, myself trying to extend the bookmarklet in some way, won’t be able to alter the styling in any way without either changing the original source code or using “!important” after my rule. Using these rules can get messy because it’s only going to lead down a messy road of adding secondary or even tertiary “!important” rules to allow developers to get the result they’d like.

So we won’t even start going down that route, we’ll instead include a separate stylesheet with these styling rules. This should look something like…

.auto-scrob-cont {

position:fixed;
bottom: 0;
width: inherit;
min-height: 50px;
background: #FFFFFF;
border-top: 1px solid #000000;
border-left: 1px solid #000000;

}

Simple enough, and to load it in, we’ll use a similar function to the one that’s used to load in the bookmarklet in the first place (which is based on some of the code used in an article at betterexplained.com):

/** Inject the css stylesheet into the <head> of the page.
*/
AutoScrobbler.prototype.injectStyles = function() {

var styles = document.createElement('SCRIPT');
styles.type = 'text/javascript';
styles.src = this.stylesUrl;
document.getElementsByTagName('head')[0].appendChild(styles);

}

We’ll add this into our javascript, but this will essentially call a second file with the CSS above. This now means that there’s a certain flexibility, I can have several instances of the UI (maybe showing different bits of information for instance) around the page. To do this I’ve added to classes to each of the main HTML elements, as using the ID attribute to select the elements in CSS would eliminate that possibility of having several instances, it also means we can have more general names for elements, such as “start” and “stop”.

<div id="autoScrobbler" class="auto-scrob-cont">

<input id="autoScrobblerStart" class="start" type="button" value="Start auto-scrobbling" onclick="autoScrobbler.start();" /> | <input id="autoScrobblerStop" class="stop" type="button" value="Stop auto-scrobbling" onclick="autoScrobbler.stop();" />

<p class="status-report"><span id="autoScrobblerScrobbleCount">0</span> tracks scrobbled</p>

</div>

Event Handling

Next we’ll fix those inline event handlers. Along with making the HTML more messy, inline event handlers are an old method of reacting to different events, it goes against unobtrusive javascript because we’re pushing javascript into the HTML, rather than keeping each language separate.

The way we do this is with event handlers, in the previous article I actually wrote a custom event handler mechanism to side-step having to deal with the compatibility issues which come with using event handlers (which are only doubled when trying to create custom events like I was previously). Here though, we can’t get away from using them if we want to improve our code. My gripe with eventListeners is compatibility, but as that’s the title of the next article, I’ll continue side-stepping the compatibility issues which exist. For now, we’ll just implement the “modern browser” method, but I’ll put it into a new function so we can address the compatibility issues more easily later.

/** Set an event listener regardless of the browser you're using.
*
*   @param eventElm The element to which the element to listen for, will involve.
*   @param eventType The type of event to listen for.
*   @param callback The function to call when the event happens.
*   @return True if listener was set successfully, false otherwise.
*/
AutoScrobbler.prototype.setNativeListener(eventElm, eventType, callback) {

eventElm.addEventListener(eventType, callback, false);

}

//Implemented like this
this.setNativeListener(this.startElm, ‘click’, this.start);

Okay so now with all that stripped out, we have some much cleaner HTML. It’s far easier to read than before, and we can assume what kind of thing the complementing Javascript will do to the elements.

<div id="autoScrobbler" class="auto-scrob-cont">

<input id="autoScrobblerStart" class="start" type="button" value="Start auto-scrobbling" /> | <input id="autoScrobblerStop" class="stop" type="button" value="Stop auto-scrobbling" />

<p class="status-report"><span id="autoScrobblerScrobbleCount">0</span> tracks scrobbled</p>

</div>

Code Injection

My final stage for this article is actually moving back to the Javascript code (I did say we were “nearly” there with it). As I said in the introduction to the article, the injection method I use is fine, but not perfect, it inserts it into another element on the page, which is dangerous, not only could that element be deleted, but the styles could change or even the structure of the page could change! It relies on the external code too heavily when it needn’t.

I’ll make an HTML injection method for the class, and I’ll make it pretty generic so that any HTML can be injected onto the page. This means that it’s a bit future-proofed, if we want to inject several bits of UI on the page, that facility is there!

/** A function which will inject a piece of HTML wrapped in a
*   <div> within any node on the page.
*
*   @param code The HTML code to inject.
*   @param where The node to inject it within.
*   @param extraParams An object which allows optional parameters
*   @param extraParams.outerDivId The id to be given to the wrapping <div>
*   @param extraParams.outerDivClass The class to be given to the wrapping <div>
*   @param extraParams.insertBeforeElm An element within the element given
*                                      in where, to insert the code before.
*/
AutoScrobbler.prototype.injectHTML(code, where, extraParams) {

if (typeof extraParams) {

if (extraParams.hasOwnProperty("outerDivId"))

var divId = extraParams.outerDivId;

if (extraParams.hasOwnProperty("outerDivClass"))

var divClass = extraParams.outerDivClass;

var insBefElm = (extraParams.hasOwnProperty("insertBeforeElm")) ? extraParams.insertBeforeElm : null;

}

var node = document.querySelector(where);
var elm = document.createElement('DIV');

if (divId)

elm.id = divId;

if (divClass)

elm.className = divClass

elm.innerHTML = code;
node.insertBefore(elm, insBefElm);

}

//To implement we use
this.injectHTML(userControls, "#mainBody");

Okay, we now have clean HTML, CSS, event handling and code injection. Just for reference, here’s our full code.

/** AutoScrobbler is a bookmarklet/plugin which extends the Universal Scrobbler
*   web application, allowing automatic scrobbling of frequently updating track
*   lists such as radio stations.
*
*   This is the constructor, injecting the user controls and starting the first
*   scrobble.
*/
function AutoScrobbler() {

var userControls = "<div id=\"autoScrobbler\" class="auto-scrob-cont">\n"+
"<input id=\"autoScrobblerStart\" class="start" type=\"button\" value=\"Start auto-scrobbling\" /> | <input id=\"autoScrobblerStop\" class="stop" type=\"button\" value=\"Stop auto-scrobbling\" />\n"+
"<p class="status-report"><span id=\"autoScrobblerScrobbleCount\">0</span> tracks scrobbled</p>\n"+
"</div>\n";
this.stylesUrl = "http://www.andrewhbridge.co.uk/bookmarklets/auto-scrobbler.css";
this.injectHTML(userControls, "#mainBody");
this.injectStyles();
this.startElm = document.getElementById("autoScrobblerStart");
this.stopElm = document.getElementById("autoScrobblerStop");
this.loopUID = -1;
this.lastTrackUID = undefined;
this.scrobbled = 0;
this.countReport = document.getElementById("autoScrobblerTracksScrobbled");
this.evtInit(["addLatest", "loadThenAdd", "start", "stop"]);
this.listen("addLatest", this.reportScrobble);
this.setNativeListener(this.startElm, 'click', this.start);
this.setNativeListener(this.stopElm, 'click', this.stop);
this.start();

}

/** Inject the css stylesheet into the <head> of the page.
*/
AutoScrobbler.prototype.injectStyles = function() {

var styles = document.createElement('SCRIPT');
styles.type = 'text/javascript';
styles.src = this.stylesUrl;
document.getElementsByTagName('head')[0].appendChild(styles);

}

/** Hashing function for event listener naming. Similar implementation to
*   Java’s hashCode function. Hash collisions are possible.
*
*   @param toHash The entity to hash (the function will attempt to convert
*                 any variable type to a string before hashing)
*   @return A number up to 11 digits long identifying the entity.
*/
AutoScrobbler.prototype.hasher = function(toHash) {

var hash = 0;
toHash = "" + toHash;
for (var i = 0; i < toHash.length; i++)

hash = ((hash << 5) - hash) + hash.charCodeAt(i);

}

/** Custom event initiator for events in AutoScrobbler.
*
*   @param eventName The name of the event. This may be an array of names.
*/
AutoScrobbler.prototype.evtInit = function(eventName) {

//Initialise the evtLstnrs object and the event register if it doesn't exist.
if (typeof this.evtLstnrs == "undefined")

this.evtLstnrs = {"_EVTLST_reg": {}};

if (typeof eventName == "object") {

for (var i = 0; i < eventName.length; i++) {

var event = eventName[i];
this.evtLstnrs[""+event] = [];

}

} else

this.evtLstnrs[""+eventName] = [];

}

/** Custom event listener for events in AutoScrobbler.
*
*   @param toWhat A string specifying which event to listen to.
*   @param fcn A function to call when the event happens.
*   @return A boolean value, true if the listener was successfully set. False
*           otherwise.
*/
AutoScrobbler.prototype.listen = function(toWhat, fcn) {

//Initialise the function register if not done already
if (typeof this.evtLstnrs._EVTLST_reg == "undefined")

this.evtLstnrs._EVTLST_reg = {};


if (this.evtLstnrs.hasOwnProperty(toWhat)) {

//Naming the function so we can remove it if required. Uses hasher.
var fcnName = this.hasher(fcn);

//Add the function to the list.
var event = this.evtLstnrs[toWhat];
event[event.length] = fcn;
this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName] = event.length;
return true;

} else

return false;

}

/** Custom event listener trigger for events in AutoScrobbler
*
*   @param what Which event has happened.
*/
AutoScrobbler.prototype.trigger = function (what) {

if (this.evtLstnrs.hasOwnProperty(what)) {

var event = this.evtLstnrs[what];
for (var i = 0; i < event.length; i++)

event[i]();

}

}

/** Custom event listener removal for events in AutoScrobbler
*
*   @param toWhat A string to specify which event to stop listening to.
*   @param fcn The function which should no longer be called.
*   @return A boolean value, true if removal was successful, false otherwise.
*/
AutoScrobbler.prototype.unlisten = function(toWhat, fcn) {

var fcnName = this.hasher(fcn);
if (this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName) {

var event = this.evtLstnrs[toWhat];
var fcnPos = this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName];
event[fcnPos] = void(0);
delete this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName];

return true;

}

return false;

}

/** Set a native event listener (eventually regardless of the browser you're using).
*
*   @param eventElm The element to which the element to listen for, will involve.
*   @param eventType The type of event to listen for.
*   @param callback The function to call when the event happens.
*   @return True if listener was set successfully, false otherwise.
*/
AutoScrobbler.prototype.setNativeListener(eventElm, eventType, callback) {

eventElm.addEventListener(eventType, callback, false);

}

/** A function which will inject a piece of HTML wrapped in a
*   <div> within any node on the page.
*
*   @param code The HTML code to inject.
*   @param where The node to inject it within.
*   @param extraParams An object which allows optional parameters
*   @param extraParams.outerDivId The id to be given to the wrapping <div>
*   @param extraParams.outerDivClass The class to be given to the wrapping <div>
*   @param extraParams.insertBeforeElm An element within the element given
*                                      in where, to insert the code before.
*/
AutoScrobbler.prototype.injectHTML(code, where, extraParams) {

if (typeof extraParams) {

if (extraParams.hasOwnProperty("outerDivId"))

var divId = extraParams.outerDivId;

if (extraParams.hasOwnProperty("outerDivClass"))

var divClass = extraParams.outerDivClass;

var insBefElm = (extraParams.hasOwnProperty("insertBeforeElm")) ? extraParams.insertBeforeElm : null;

}

var node = document.querySelector(where);
var elm = document.createElement('DIV');

if (divId)

elm.id = divId;

if (divClass)

elm.className = divClass

elm.innerHTML = code;
node.insertBefore(elm, insBefElm);

}

/** Starts the auto-scrobbler, scrobbles immediately and schedules an update
*   every 5 minutes.
*/
AutoScrobbler.prototype.start = function() {

this.loadThenAdd();
autoScrobbler.loopUID = setInterval(this.loadThenAdd, 300000);
autoScrobbler.start.disabled = true;
autoScrobbler.stop.disabled = false;

}

/** Stops the auto-scrobbler, ends the recurring update and zeros the required
*   variables.
*/
AutoScrobbler.prototype.stop = function() {

clearInterval(this.loopUID);
this.lastTrackUID = undefined;
this.loopUID = -1;
this.stop.disabled = true;
this.start.disabled = false;

}

/** Loads the new track list using Universal Scrobbler and schedules a scrobble
*   of the latest tracks 30 seconds afterwards.
*/
AutoScrobbler.prototype.loadThenAdd = function() {

doRadioSearch();
setTimeout(this.addLatest, 30000);

}

/** Selects all the tracks which have not been seen before and scrobbles them
*   using Universal Scrobbler.
*/
AutoScrobbler.prototype.addLatest = function() {

var tracks = document.querySelectorAll(".userSong");
this.lastTrackUID = (typeof this.lastTrackUID == "undefined") ? tracks[1].querySelector("input").value : this.lastTrackUID;

//Check every checkbox until the last seen track is recognised.
for (var i = 0; i < tracks.length; i++) {

var item = tracks[i];
if (item.querySelector("input").value == this.lastTrackUID) {

i = tracks.length;
this.lastTrackUID = tracks[0].querySelector("input").value;

} else {

item.querySelector("input").checked = true;
this.scrobbled++;

}

}
doUserScrobble();
this.trigger("addLatest");

}

/** Updates the user interfaces to reflect new scrobbles.
*/
AutoScrobbler.prototype.reportScrobble = function() {

this.countReport.innerHTML = this.scrobbled;

}

// Create a new instance of the AutoScrobbler.
autoScrobbler = new AutoScrobbler();

That’s it for this article, next we’ll be looking at achieving as full as possible compatibility.

Advertisements

Refactoring Code in to an Object-Oriented Paradigm #4: Extendible, Readable, Descriptive

This is article number 4 of a series of article, which simply document my refactoring of some code I originally wrote (pretty poorly) last year. To get the jist of what’s going on. Check the original post – Refactoring Code in to an Object-Oriented Paradigm.

Making code Extensive, Readable and Descriptive

We’re nearly there with the Javascript, the worst has been cleaned up generally. Now we’re getting on to being developer-friendly, which is pretty important, you want people to extend your program. Even if you want to have control over the way they interface with your program, you still want people to interface with it somehow.

There are a few ways you can make things easier for developers, we’ve reorganised the code in the previous step, but it’s an important step, just by putting the constructor code up the top, you can suddenly look at the code and understand it just by scrolling down the page once. At least, that’s the idea, but sometimes it doesn’t hurt to actually explain what a method is doing, or what a specifically tricky part of the code does. This is why we use comments above each function, and in places actually within the code.

Finally, while it’s not quite keeping exactly the same functionality, we’re going to implement a system which allows other functions to listen for certain events. This means there’s a properly implemented way of extending our code. To ease our compatibility stage at the end, I’m going to use a custom function for custom event handling, that I’ve developed myself.

/** AutoScrobbler is a bookmarklet/plugin which extends the Universal Scrobbler
*   web application, allowing automatic scrobbling of frequently updating track
*   lists such as radio stations.
*
*   This is the constructor, injecting the user controls and starting the first
*   scrobble.
*/
function AutoScrobbler() {

var userControls = "<div id=\"autoScrobbler\" style=\"background: #FFFFFF; border-top: 1px solid #000000; border-left: 1px solid #000000; position: fixed; bottom: 0; height: 50px; width: inherit;\">"+
"<input id=\"autoScrobblerStart\" type=\"button\" value=\"Start auto-scrobbling\" onclick=\"autoScrobbler.start();\" /> | <input id=\"autoScrobblerStop\" type=\"button\" value=\"Stop auto-scrobbling\" onclick=\"autoScrobbler.stop();\" />"+
"<p><span id=\"autoScrobblerScrobbleCount\">0</span> tracks scrobbled</p>"+
"</div>";
document.querySelector("#disclaimersContainer").innerHTML += userControls;
this.startElm = document.getElementById("autoScrobblerStart");
this.stopElm = document.getElementById("autoScrobblerStop");
this.loopUID = -1;
this.lastTrackUID = undefined;
this.scrobbled = 0;
this.countReport = document.getElementById("autoScrobblerTracksScrobbled");
this.evtInit(["addLatest", "loadThenAdd", "start", "stop"]);
this.listen("addLatest", this.reportScrobble);
this.start();

}

/** Hashing function for event listener naming. Similar implementation to
*   Java's hashCode function. Hash collisions are possible.
*
*   @param toHash The entity to hash (the function will attempt to convert
*                 any variable type to a string before hashing)
*   @return A number up to 11 digits long identifying the entity.
*/
AutoScrobbler.prototype.hasher = function(toHash) {

var hash = 0;
toHash = "" + toHash;
for (var i = 0; i < toHash.length; i++)

hash = ((hash << 5) - hash) + hash.charCodeAt(i);

}

/** Custom event initiator for events in AutoScrobbler.
*
*   @param eventName The name of the event. This may be an array of names.
*/
AutoScrobbler.prototype.evtInit = function(eventName) {

//Initialise the evtLstnrs object and the event register if it doesn't exist.
if (typeof this.evtLstnrs == "undefined")

this.evtLstnrs = {"_EVTLST_reg": {}};

if (typeof eventName == "object") {

for (var i = 0; i < eventName.length; i++) {

var event = eventName[i];
this.evtLstnrs[""+event] = [];

}

} else

this.evtLstnrs[""+eventName] = [];

}

/** Custom event listener for events in AutoScrobbler.
*
*   @param toWhat A string specifying which event to listen to.
*   @param fcn A function to call when the event happens.
*   @return A boolean value, true if the listener was successfully set. False
*           otherwise.
*/
AutoScrobbler.prototype.listen = function(toWhat, fcn) {

if (this.evtLstnrs.hasOwnProperty(toWhat)) {

//Naming the function so we can remove it if required. Uses hasher.
var fcnName = this.hasher(fcn);

//Add the function to the list.
var event = this.evtLstnrs[toWhat];
event[event.length] = fcn;
this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName] = event.length;
return true;

} else

return false;

}

/** Custom event listener trigger for events in AutoScrobbler
*
*   @param what Which event has happened.
*/
AutoScrobbler.prototype.trigger = function (what) {

if (this.evtLstnrs.hasOwnProperty(what)) {

var event = this.evtLstnrs[what];
for (var i = 0; i < event.length; i++)

event[i]();

}

}

/** Custom event listener removal for events in AutoScrobbler
*
*   @param toWhat A string to specify which event to stop listening to.
*   @param fcn The function which should no longer be called.
*   @return A boolean value, true if removal was successful, false otherwise.
*/
AutoScrobbler.prototype.unlisten = function(toWhat, fcn) {

var fcnName = this.hasher(fcn);
if (this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName) {

var event = this.evtLstnrs[toWhat];
var fcnPos = this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName];
event[fcnPos] = void(0);
delete this.evtLstnrs._EVTLST_reg[toWhat+"->"+fcnName];

return true;

}

return false;

}

/** Starts the auto-scrobbler, scrobbles immediately and schedules an update
*   every 5 minutes.
*/
AutoScrobbler.prototype.start = function() {

this.loadThenAdd();
autoScrobbler.loopUID = setInterval(this.loadThenAdd, 300000);
autoScrobbler.start.disabled = true;
autoScrobbler.stop.disabled = false;

}

/** Stops the auto-scrobbler, ends the recurring update and zeros the required
*   variables.
*/
AutoScrobbler.prototype.stop = function() {

clearInterval(this.loopUID);
this.lastTrackUID = undefined;
this.loopUID = -1;
this.stop.disabled = true;
this.start.disabled = false;

}

/** Loads the new track list using Universal Scrobbler and schedules a scrobble
*   of the latest tracks 30 seconds afterwards.
*/
AutoScrobbler.prototype.loadThenAdd = function() {

doRadioSearch();
setTimeout(this.addLatest, 30000);

}

/** Selects all the tracks which have not been seen before and scrobbles them
*   using Universal Scrobbler.
*/
AutoScrobbler.prototype.addLatest = function() {

var tracks = document.querySelectorAll(".userSong");
this.lastTrackUID = (typeof this.lastTrackUID == "undefined") ? tracks[1].querySelector("input").value : this.lastTrackUID;

//Check every checkbox until the last seen track is recognised.
for (var i = 0; i < tracks.length; i++) {

var item = tracks[i];
if (item.querySelector("input").value == this.lastTrackUID) {

i = tracks.length;
this.lastTrackUID = tracks[0].querySelector("input").value;

} else {

item.querySelector("input").checked = true;
this.scrobbled++;

}

}
doUserScrobble();
this.trigger("addLatest");

}

/** Updates the user interfaces to reflect new scrobbles.
*/
AutoScrobbler.prototype.reportScrobble = function() {

this.countReport.innerHTML = this.scrobbled;

}

// Create a new instance of the AutoScrobbler.
autoScrobbler = new AutoScrobbler();

Reorganisation

There’s quite a few changes to the structure of the code here. You’ll notice that the code now reads in the same way as the code would be used. When you run the bookmarklet initially, you’d run the constructor code, which then calls start(), which in turn calls loadAndAdd() and so on.

The only exception to this is the stop() command, starting and finishing methods like these are often grouped at the top of the code, to make it clear to the programmer what the basic functions are. As stop() doesn’t use any method which hasn’t been seen further up the code, it also won’t confuse the programmer, as it’s clear how the method works, without knowing anymore about the code.

Comments

You’ll also notice that each method has a comment to go with it, and in certain places, helpful hints have been added in the code. This can be very important for your code, as your way of thinking about the architecture of the code is very rarely the same as someone else’s way of thinking. Comments communicate in natural language, what the function or method’s overall goal is, they should generally not dissect the code and describe each operation (this can be done within the code if really required).

The way that I’ve written them is one of several loose standards for commenting. This style is similar to the way comments are written in Java. It is often useful to write comments in a common standard, as developers will be able to understand them more immediately, but other programs may also be able to understand them. There are many programs which have been written to interpret comments like these into formatted technical documentation (The type that you’d see on the MDN, PHP.net or Oracle Docs), saving you a great deal of time and effort and being a neighbourly developer to anyone that wants to look, use or edit your code.

I’ve used a few of these programs myself, I’ve had most success with Natural Docs (for Javascript, though it has support for other languages), PHP Documentor (A bit of a fiddle, but made great PHP documentation) and Oracle’s own Javadoc (For Java of course).

Extensibility

You will also notice that I’ve added three new methods, two of which help me and other developers extend the code in a documented way. The alternative to this would be for other developers to rely on the variables and functions to remain the same in name and the action they perform, this is a bad idea. The two new functions (listen() and trigger()), allow any other functions to hook on to the function/event that they want to know about and have a function called when the function/event happens.

By using this system, events can be named independently of the internal workings of the code, which means more stability for us and other developers.

The third function is a demonstration of this implemented extensibility, in the constructor method, I start “listening” for the “addLatest” event with reportScrobble(), which for now just signifies that the addLatest() method has run and calls reportScrobble() to update any user interface. The event gets triggered simply by calling this.trigger(“addLatest”).

I haven’t included a “removeListen” function here, as it gets quite complicated in pure Javascript, but this would also be possible, and would allow code to stop listening to our events.

Note (07/02/2013)

I have since added a remove and a evtInit function to the event listener mechanism (which I have added above in the code). It has slightly complicated the implementation and you’ll notice a new hasher function which is used to name functions in a way that they can be identified later. I decided that it’d be better to have a custom implementation which was at least complete (often an issue with custom implementations). The rest of the code here is unchanged (except for the initiation of events), I have in fact refactored my own code, making sure that all existing functionality remains.

I have further researched native implementations for custom events, which I haven’t used because of issues with compatibility (the final article in the series will have more on this). If you are interested in using these native methods, Sitepoint has a pretty good article for the basics.

In terms of refactoring our Javascript, we’re just about done. We now have a far cleaner implementation, which is now a lot more developer friendly. But for all the code, there’s a user interface, and everything about it has been neglected thus far, so next we’ll be looking at Cleaning Up Your Messy User Controls.