The Label Element

December 11th, 2009

Dear Web Developers,

The following mark-up is commonly used to put text adjacent to an input element such as a radio button or check box:

<input name="foo" type="checkbox" value="bar" /> Click this checkbox.

DO NOT DO THIS!

Instead wrap put the text next to the check box inside the label element and give the input element an ID the label element can reference. e.g.

<input id="foo" name="foo" type="checkbox" value="bar" />
<label for="foo">Click this checkbox.</label>

This allows for the user to click on the text to interact with the input element as well as clicking just on the input element itself increasing usability of your sites and applications and subduing my rage.

UDK experience so far

December 7th, 2009

I’ve started to watch the, not so small, library of UDK video tutorials recently put out by Epic. After watching the Skeletal Mesh pipeline which includes things like character animation, model morphing, skeletal meshes and mirrors, textures, etc. I am beginning to think that procedural synthesis is going to take a rather venerable hold in the industry in the years to come; due to storage or throughput limitations of distribution mediums if nothing else. I for one welcome our new algorithmically generated game asset overlords.

Word Clouds in Resumes

September 4th, 2009

Trying out a new thing with the ol’ resume. I didn’t want to clutter it up too much, so the word cloud is only at the top, but eventually I’d like to find a way to make word clouds out of techs used during individual tenures. Perhaps making it some sort of interactive site and then throwing in some print CSS would make more sense than leaving it on paper. Not entirely sure. Either way, updated resume with the word cloud is on the about page.

Things I hate…

August 15th, 2009

Server side pagination.

Code Style is not Subjective p.2

August 10th, 2009

Now for a bit about JavaScript and C-style code. First and foremost, if you are unfamiliar or think you are familiar, but really aren’t, (because of say, the welcoming C-style syntax) with JavaScript consider reading JavaScript: The Good Parts. It is a quick, light read that provides invaluable insight into the language. JS is an immensely expressive language but some of the baggage surrounding it can make it seem like little more than a toy language that is intolerable to code in and should generally be avoided at all costs. This simply isn’t true, so go on, have a read.

Update: Indentation and brace style should follow the 1TBS/OTBS style.

When programming in JS always keep in mind that JS doesn’t come with a linker, so everything is put in the global namespace unless you explicitly state not to. All program variables should be kept either within a lambda scope e.g.

(function () {
var a = ‘foo’,
     b = function () {},
     c = ‘bar’, …
)());

or within a namespace e.g.

MYPROGRAM = window.MYPROGRAM || {};
MYPROGRAM.a = ‘foo’;
MYPROGRAM.b = function b () {};
MYPROGRAM.c = ‘bar’;

-or-

MYPROGRAM = window.MYPROGRAM || {
    a: ‘foo’,
    b: function b () {},
    c: ‘bar’
};

Also, all variables should be declared with the var keyword, otherwise the JS interpreter will automatically link it to the global object. Further, unlike many other C-style languages JS does not have block scope (neither does PHP for that matter). In languages with block scope it is suggested to not declare variables until they are used. Since both PHP and JS only have functional scope, variables should be declared at the top of the function in which they are used rather than right before they are used.

Since JS provides no linkeranything can overwrite anything else, and no runtime warning or errors are provided when such an event occurs. A lot of code in JS seems to be copied and pasted from elsewhere and this code could very well have strange interactions with your programs if the original author forgot to properly declare a variable or named one of his functions the same as yours. Always encapsulate your program variables and keep them out of the global namespace. When using a namespace, keep the namespace as all caps, this is to differentiate it from other methods or functions.

Generally, when naming function or methods, the name should start with a lower case letter, unless it is a constructor function. If it is a constructor function it should start with an upper case letter. e.g.

function Foo (bar) {
   this.hello = function () {
       alert(bar);
   }
}

var myFoo = new Foo("bar");
myFoo.hello(); //"bar"

Since we are primarily dealing with web languages it is best not to mix up code style between different C-style languages such as JS and PHP, as during the course of a day you will most likely be working in many languages. In either language either of the following are acceptable:

if (foo) {
   bar;
}

if (foo)
{
   bar;
}

if (foo)
   bar;

However, the first is the preferred way of writing it. This is due to a limitation in JS. JS implicitly inserts end of statement characters on new lines. So something like:

return
{
   foo: ‘bar’
};

may look like it is returning a new object with the property foo with the value ‘bar.’ However, it is actually returning undefined and the object literal statement is never reached. What is really meant is:

return {
    foo: ‘bar’
};

Moreover, it is very easy to make a mistake such as:

if (foo)
   bar();
   baz();

which was meant to mean:

if (foo) {
   bar();
   baz();
}

but really means:

if (foo) {
   bar();
}
baz();

It is preferred that when declaring arrays or objects that their literal forms are used. So instead of:

a = new Object();
b = new Array();

write:

a = {};
b = [];

The use of whitespace is paramount when writing code. It increases legibility significantly. The suggested use of whitespace is as follows:

  • Use a space before and after any operator e.g. /, +, *, -, &&, etc.
  • Use a space after a function declaration and the beginning and end of the argument definition e.g. function foo (a, b, c) { }. This helps differentiate between a function definition and a function invocation.
  • Use a space after semi-colons

When testing for values in JS always use === or !== operators. Their == and != counterparts do not do what you might think they do and should generally be avoided.

If a function requires many arguments consider putting the arguments in an options object and passing the object into the function instead of having a long list of arguments which may or may not be optional. This alleviates the need to have to remember the order of all the arguments and allows for more verbose code, too. For example:

function foo (bar, baz, qoz, …) {};
foo(1, 2, 3);

can be rewritten as

function foo (args) {};
foo({
   bar: 1,
   qoz: 2,
   baz: 3
});

More to come later…

Code Style is not Subjective p.1

August 10th, 2009

Over the course of my, so-far brief, career in software development I have seen a plethora of different coding styles fully blanketing the spectrum from awful to outstanding. Software is not a write-only medium and is read (both by humans and machines alike) many more times than it is inked (or typed, punched, plugged, etc). As such, unless participating in some sort of obfuscated code competition, all code should be written with readability in mind. Over the course of the next few days I will attempt to define basic coding styles for C-style languages (like PHP, Java, C#, and JavaScript), SGML style languages (like XHTML, HTML, ColdFusion, and XML), and SQL as well as some best practices.

To begin, I will start with some basic principles that are applicable to the programming field in general.

First and foremost, in order to attain a higher degree of readability the DRY principle should always be kept in mind. Further, code should generally be as verbose as possible. Verbosity allows writing of self documenting code and keeps comments (which aren’t necessarily updated as often as the code is, which is a bad thing indeed) to a minimum. It is very easy to write terse code using quasi-cryptic operators such as ++ or — and short variable names such as r instead of row. However, while this practice could be considered elegant it can often be difficult to read and can be subject to bug introduction during maintenance (or even development).

Update: After some input from co-workers I thought it best to amend the statement discouraging the use of terse operators like ++ and –. While I personally feel it best to not use them, I can also understand that to some, it is much more readable to see i++/i– over i = i + 1 and i = i -1. If you are unfamiliar with all aspects of these operators than you should avoid using them and instead use the more verbose notation. However, if you choose to use these operators, it may be in your best interest to leave a comment or two about what these operators do if your code is to be maintained by persons without in-depth knowledge of them.

For example, consider the following piece of JS that determines if the two given arrays are equivalent:

(function (a1,a2){   
    for (var i = 0, var j = 0; i < a1.length && j < a2.length; i++, j++)
       if (a1[i] !== a2[j]) return false;
    return a1[i] === a2[j];
}([1,2,3],[1,2])); //false

This code is a bit cryptic to read and is generally poorly written for a number of reasons, some of which won’t be discussed until the JS portion of this series. It would be much easier to read and maintain if it were written as:

function arrayCompare (array1, array2) {
   var length1 = array1.length,
        length2 = array2.length,
        i = 0, j = 0;
   while (i < length1 && j < length2) {
      if (array1[i] !== array2[j]) {
         return false;
      }
      i = i + 1;
      j = j + 1;
   }
   return array1[i] === array2[j];
}
arrayCompare([1, 2, 3], [1, 2]); //false

The code is much more verbose and it is clear what each variable represents. Since there using a for loop here would make the code much more terse using a while loop makes it much more readable. It is also easy to see that both i and j are incremented once through each iteration of the loop and it is clear what the body of the while and if statements are.

All code should adhere to strict indentation and other white-space schemas. Lines of code should also fit in your IDE (or text) editor’s window without the need for scrolling or window resizing. Unfortunately, this is a little bit subjective as not all monitors are created equally and the size of your window may differ from the size of your fellow developers window, or perhaps the size of your window on another machine. Therefore, I recommend adhering to 79 characters max per line. This will almost guarantee the code will display inside of the editor window without wrapping or the need for scrolling or resizing. 79 characters is the default max of dumb terminal windows.

Such a small character limit may seem like a large restriction, but for comparison, my Eclipse IDE with the navigator and outline windows in view leaves only 101 characters for the text editor at a 1600×1200 resolution. It is possible to be very expressive within 79 characters and anything beyond that should go to a new line and be indented appropriately.

I think that’s a good start. More to come later.

One Bug to Rule Them All

July 17th, 2009

Just read an interesting article about a buffer overflow bug dealing with the select method. Interestingly, the proof of concept choose the max signed 32-bit integer of 2147483647 to cause the bug. This does indeed crash IE8, but does not crash FF3.5. I didn’t personally test any other browser. What’s even more interesting about this though is that this shouldn’t overflow in JS, as all numbers in JS are internally represented as 64-bit floats. Moreover, even if JS had a 32-bit integer number that number shouldn’t overflow at all. Given that the bug deals with the select element, my suspicion is that it is a DOM related issue that is being misrepresented as an ECMA script related issue.

Please don’t destroy browser functionality

July 12th, 2009

Dear web developers,

Please consider browser functionality when designing your sites. More and more “Web 2.0″ sites are taking away my browser features. The biggest culprit so far are sites with JS that like to make links which redirect you to a different page via JS or pop-up a modal, or other similar dialog which then has a link that takes you to a different page. Both of these prevent me from being able to open a link in a new tab, which is more than annoying. My university and several large shopping sites still do this. Please stop.

If I middle click a link which, if I left click it takes me to, or has another link which takes me to, another page, that action should open that page up in another tab. Repeat: that action should not do nothing, but open the page up in a new tab! It also should open the link and not something like the same page with a # appended to it or some Javascript in the address bar.

The danger of using frameworks that augment default objects

July 6th, 2009

After a bit of thought it occurred to me just how dangerous using frameworks, like Prototype, which augment built-in objects in large-scale production projects.  As many avid JS developers are most likely aware, the prototype inheritance model of the language allows for the extension of virtually any object in the language, user defined or otherwise. However, this can also have severe side effects if a browser developer decides to include a method of the same name as your framework of choice added to a built-in object some years back.

Take, for example, the venerable each function. Prototype extends enumerable objects such as arrays with this function in its prototype. e.g.

Array.prototype.each = function(f){

   var i = 0;

   for(; i < this.length; i = i + 1){

      f(this[i],i);

   }

}

Now imagine this code is called in thousands of places in your project. Let’s also assume before extending the object prototype such a library would also check for the existence of said function as to not overwrite default behavior e.g.:

if(!Array.each) {

   …

}

Now imagine an A-grade browser’s newest version now includes the each method on collection objects by default but with either an argument omitted, or the argument order swapped. All of the code that you had written that depended on the augmented each function instead of the built-in each function will now no longer work in that browser, and you can’t very well force users to not use the latest version of IE, Safari, Firefox, or Opera because a decision was made some years back to use a framework which augments built-in objects.

This decision may now cost many thousands of dollars, if not more. Other JS frameworks such as YUI or jQuery seem like a much more sound choice in this respect. In my opinion, YUI is probably the better of the two choices as it is far easier to pick-up and master by developers of other, not necessarily web-centric languages. Yet in the scope of this rant, both are equally more sound than using something which could very easily and most likely inevitably break a few years down the road, costing many unnecessary man-hours, and perhaps dollars, to fix.

Why do I have to fill out the exact the form…

March 15th, 2009

This is the 21st century. Virtually everybody has some sort of computrized database for storing contact or other meta-personal information and yet anytime I change my address (e.g. physical, e-mail, IM, phone number, etc.) or some other piece of information (e.g. credit card, insurance carrier, etc.) I have to send out  e-mails, make phone calls, fill out a change of address form at the post office, call creditors and banks, inform family and friends, or something equally painful and mind-numbing about a few dozen times. This is bizzare.

It would be much better if we had something like a DNS system, but for people instead of IP addresses. This type of system can work transparently on top of already existing systems. I haven’t really hashed out all the details of this but it would work something like this:

You give whoever you want to be able to contact you or know some information about you some identifier. They then use this identifier for everything related to you. So when they want to write you an e-mail they put in this identifier and their e-mail client requests your e-mail address from the system given that identifier and then sends the e-mail to that address. If you change your address at a later time, then the next time that identifier is used it will automatically follow you. The same goes for phone systems, chat systems, physical addresses etc.

Although it would make usability a bit clumsy a security layer could be added using public-key cryptography. So when you give out your identifier they also give out theirs. Then you log-in to a central system and authorize their identifier for only certain parts of your data, or perhaps slightly different data than you give out to others (e.g. you authorize a shady site for the e-mail address myspam@mydomain.com instead of myemailaddress@mydomain.com that everyone else gets). That way you can only give out the information you want to give out. Then at any time you can change or update your information and authorize/deauthorize keys to access that information.

Essentially this kind of system would remove the need for filling out multiple change of information forms and making multiple calls, e-mails, IMs, etc. You would just go into the system, update your info, and it would get propogated out the next time it is requested. No more change of address forms, everything just follows you if you want it to, and stops when you don’t.