nooshu - Matt Hobbs' Web Development Blog

Kneeling on the shoulders of giants

Category: Ramblings

A random ramble from the large squidgy organ inside my skull.

Auto-spacing input field text

On a recent project, a set of wireframes landed on my desk which contained some “interesting” ideas, particularly when it came to form fields. One that really stood out was having a credit card number auto-space as the user types it into a single input box. This of course would depend completely on what type of card they are entering, as different cards have different layouts. American Express for example have a [4, 6, 5] layout, where as Visa use a [4, 4, 4, 4] number layout.

Thankfully this functionality was eventually removed from the UI specifications after concerns from myself and others were raised. Just to make this perfectly clear; I think this is a bad idea, and here is why:

  • Users are very wary when it comes entering their credit card details. Spaces magically appearing in their credit card number could freak a few of them out.
  • Most e-commerce website I’ve ever used ask for the credit card number as one long string of numbers with no spaces. Auto-spacing goes against this convention, so a user may actually go back and try to remove the spaces (I know I would).
  • Validation scripts for credit card numbers usually ask for the input to have no spaces, or will remove any spaces on submit.
  • If you do want to space out the credit card number, why not use different input boxes for each set of numbers? It looks cleaner and is simpler to implement. Just have your JavaScript automatically jump between boxes as the user types.

Even though this functionality was removed, I decided to build a prototype of it in action because… well because I can and I was bored. :) Please, (please!), whatever you do don’t use this code in production. I created it as an experiment and should be treated that way. It’s very simple to get around the auto-spacing and then all hell breaks loose! (not really, but it is easy to break).

The spacing is implemented by counting the length of the string inside the input field, but you could also monitor the current position of the cursor. Anyone wishing to do this see this excellent set of answers on Stackoverflow.

You can see the prototype here. Remember, don’t use it. It’s not big and it’s not clever :)

Sublime Text 2, a truly sublime text editor

I’m always on a quest to find that one perfect editor, I’ve been hunting for it for many years. Having been an UltraEdit, Notepad++, Eclipse, Aptana and Komoddo user in the past, they all had their pro’s and cons. Eclipse and Aptana (built on Eclipse) were very feature rich (and very slow!). UltraEdit and Notepad++ were quick but didn’t quite fulfil what I was looking for in an editor (maybe that has changed in newer versions). Komoddo I was very impressed with, but it had a few bugs that just became annoying after a while (like tabbing code blocks). None of them were quite right.

Upon starting my new job a couple of months ago, a colleague, Anton Mills, introduced me to Sublime Text 2 and I haven’t opened another editor since!

Sublime Text 2 editor in action with 2 columns.

Sublime Text 2 editor with its very handy 2 column layout.

There are a few key features I look for in an editor. They are:

  • Speed: It needs to be responsive and quick to load. There is nothing more frustrating than an editor that freezes all the time and takes 5 minutes to load.
  • Regular updates: If there is a bug it’s nice to know it will be fixed soon(ish).
  • Plugins: It may not be possible for an editor to have everything you need in the core, so a good plug-in interface is essential.
  • Community: The author may not be on hand to answer all questions so having a helpful community is a plus.

Thankfully, Sublime meets all of these criteria. Development builds are released every week or so and there is a vibrant community on hand to answer any questions. As I’m in a list making mood, here’s a few plug-ins I’d encourage any Front-end Developer install:

  • Package Control: Before you do anything, install this plug-in! It makes installing all other plug-ins a breeze.
  • SublimeCodeIntel: An excellent code auto-complete plug-in that’s been ported from Komoddo.
  • Zen Coding: If you haven’t heard of Zen Coding take a look at my previous blog post. You will wonder how you ever survived without it.
  • SFTP: Making changes and uploading them directly from the editor can be a godsend at times, but use with caution! Paid plug-in but you get a free trial.
  • DocBlockr: Makes documenting your code quick and easy (supports JavaScript and PHP).

A few of the key features to look out for when you try it are the full screen and distraction free mode, multi columned editor (see image above), multiple cursors (this one I love!), code mini-map and the command palette. Those are just a few I’ve noted but I’m discovering more every day. Checkout this informative post by Nettuts+ for more awesome features.

Now it’s not perfect, there are a cons. Automatic tag closing isn’t working for me at the moment and the project panel needs a little work (being able to drag and drop files would be nice), but the pros far outweigh the cons, so I’m willing to overlook them. If you have a suggestion or find a bug the place to report them is here. As you can see there are lots of feature requests already and the author actually implements (some) of them!

Big thank you to the author Jon Skinner, a fellow Sydney resident for developing such an excellent editor! Download it here.

New country, new job!

Boy, it’s been a while since my last blog post! Over the past two months I’ve discovered how hard it can be to get a broadband connection in Australia! I’d been “refused” by three different companies before I was finally connected on Saturday. Lesson to be learned: check which company owns your local exchange; If you don’t you will be refused (but they won’t really tell you why!).

I’ve also started a new job at an excellent Australian digital agency called Visual Jazz Isobar (I know, it’s a bit long-winded isn’t it). VJ have an excellent set of clients and the agency is producing some outstanding work. We’ve also just become part of the Isobar group which have offices all over the globe. With over 2500+ employees, the group is producing some world-class work. It’s a very exciting time to be at Visual Jazz!

My Visual Jazz avatar.

My Visual Jazz avatar: that's me holding Nigel Mansell's crash helmet!

Above you can see my custom VJ avatar. Everyone at VJ gets one and, as I’m a huge F1 fan I decided to incorporate it into my design. The crash helmet I’m holding is that of my childhood hero Nigel Mansell; winner of the 1992 F1 Season. I remember being on Club Corner, Silverstone in 1991 as a 10 year old, watching Mansell giving Senna a lift back to the pits as his McLaren had run out of fuel. Ahh the good old days!

Writing efficient CSS selectors

With modern browsers getting quicker with every new version number it’s easy to fall into the trap of writing inefficient code. A page will run super quick on the latest version on Chrome or Firefox, but you also have to consider older browsers and mobile devices. That shiny new web application that uses some super fancy CSS selectors may be unusable on certain devices due to its limited hardware. That’s not to say you shouldn’t be using super fancy selectors; you just have to be careful to consider your target audience, and use them in the most efficient way possible.

The first thing to note about CSS selectors is they don’t work in the way you’d expect. In the west we read a page from left to right. Reading a CSS selector, you’d expect that’s what the browser does as well. Wrong! The browser actually reads a selector from right to left (in Mozilla’s case anyway, and most likely in others too). So take the following CSS as an example:

1
2
3
4
5
body #wrapper .article ul.meta li a {
    font-weight: 700;
    text-decoration: none;
    font-family: Arial;
}

The browser first looks for all the anchor tags (called the “key” as it’s the rightmost selector), then looks at the list items, it evaluates those and throws away the results that don’t match. Next the browser moves onto elements with a class of “meta”, throwing out results that don’t match and so on… you get the idea! There’s so much redundancy in the above selector, it could easily be cut down to:

1
2
3
4
5
.meta a {
    font-weight: 700;
    text-decoration: none;
    font-family: Arial;
}

This example is much more efficient. There are less rules for the browser to evaluate, it’s much easier to read and if you apply minimal selectors across your whole stylesheet you will notice a big difference in file size. The key to writing efficient selectors is to be as specific as possible. Whatever you do don’t write this:

1
2
3
4
body * {
    margin: 0;
    padding: 0;
}

The universal selector(*) is bad (even body isn’t needed)! You are targeting every single element in the DOM and setting it’s padding and margin to zero. For a large page that could easily be thousands of elements!

I actually inspected the stylesheet for this website and went over it with a fine tooth comb. I found many additional selectors that just weren’t needed. The size of my stylesheet went from 18.3kB down to 16.5kB, a saving of 1.8kB. It doesn’t sound a lot in terms of file size, but that’s a whole lot of selectors the browser no longer has to evaluate to render the page.

Luckily there are tools available that can help you make your CSS more efficient, as well as many other areas of your website too. The first tool I’d recommend is called Page Speed, created by Google. The Page Speed extension is available on both Chrome and Firefox. Once installed you have the option to run it on any page; it will give you an overall score for that page and recommendations on how to improve it.

Google Page Speed results for Nooshu.com.

Page Speed results for this site. Note the "Use efficient CSS selectors" drop down.

The second tool I’d recommend is Opera’s Dragonfly. Dragonfly (similar to Firebug, in name at least) is Opera’s developer toolkit, much like Web Inspector for Chrome. An awesome feature that Dragonfly has, that other toolkits don’t is “style recalculation”. Style recalculation gives you a breakdown of all the selectors that were run on the page, how long they took to evaluate and how many elements they hit along the way (hits).

Opera's Dragonfly debugger in action on nooshu.com.

Dragonfly gives you an extremely useful breakdown of what selectors were used and how long they took to evaluate.

If you look closely at the results in the image above, you will see that the selectors with the most number of hits are the ones involving the universal selector(*), as you would expect. You may also notice that most of the timings say 0.0ms which isn’t very helpful. This is due to the fact that the size of the DOM being tested is very small, and timing to 1 decimal place isn’t accurate enough to show the actual time it took to evaluate. If you were to run this test on a huge page, say the HTML5 Specification for example, you would really be able see the difference in CSS selector efficiency.

This feature of Dragonfly is very new and is still in testing. There are a few issues still to be ironed out in future releases but it’s definitely a tool to keep in your bookmarks.

A word of warning when it comes to writing efficient selectors. I found myself trying to make the selectors so efficient it was becoming quite hard to pin-point where exactly on the site they were being used. If you have a very specific area of a site you are trying to target, it is easier to read if you have the selectors starting with an ID reflecting that area. You then know for certain that the changes you make won’t affect other parts of the site. Also remember that removing “unused” selectors will affect the specificity of the rule. You could end up breaking something, as what you thought was an unused selector was actually used in overriding another rule.

Having too many descendent selectors is something that Page Speed frowns upon, but as with all things in life it’s a case of finding that happy medium. In this case it’s between efficient selectors and CSS that is easy to read and maintain (for you and other developers).

Embracing Git for version control

For many years I’ve been using Subversion (SVN) as my version control of choice. It’s been a part of my deployment process, all of my work is in Subversion, even the small demos I’ve created are in a repository. It’s a good feeling to know that if your laptop dies (or is stolen), all your work is backed up on a remote server.

Recently I’ve heard many developers raving about Git, and I’ve looked at libraries and code snippets that are using it, so I thought I’d check it out and see what all the fuss is about. After a couple of hours reading up and watching a few tutorials I’m genuinely excited about using it in future projects. It really is that good! So, what’s so good about it then you may ask? Well here are a few points that stood out for me:

  • Easy to install and start using (Windows has a simple installer)
  • Distributed version control, so no need for a central server
  • Runs on your local machine, no need to connected to the internet to commit(!)
  • Very clean, it only creates one git directory for the whole repository (no hidden .svn’s everywhere)
  • Incredibly easy to branch and merge your code (this is a big plus!)
  • It’s simple to use Git with SVN, no need to abandon your SVN repositories. Git can pull & push directly into an existing SVN repository!

I’d heard developers saying how easy it was to branch and merge your code using Git, I assumed they were exaggerating. But no, it really is simple:

1
2
3
4
5
6
7
8
#create a new branch in my repository
git branch my_new_branch

#move to the new branch for commits etc
git checkout my_new_branch

#finished with the branch, so lets merge it back into master
git merge my_new_branch

One of the most amazing parts of Git that blew me away: when you jump between branches it automatically updates your file structure accordingly! So lets say you have a new set of files in a new branch, and you need to jump back to master (trunk) to make some changes. Simply run ‘git checkout master’, the new branch files will be ‘removed’ and stored away until you are back on the new branch where they were added. Amazing!

The feature that really sold Git to me was the stash command. So many times I’ve been working on a project and got half way through some changes, only to have to fix a bug in the original version. So you copy the modified files somewhere, undo all your changes, fix the bug, copy the files over and start where you left off. Not fun! Git and the stash command come to the rescue:

1
2
3
4
5
6
#store your current changes in a 'clipboard' so they can be seen again later
git stash

#you are now working on the unmodified version of the branch
#after you've fixed the issue, start from where you left off by applying the stash
git stash apply

Another huge advantage Git has is how simple it is to share code between developers. It only takes a couple of commands to clone another repository. Once you have a local copy (clone), you can change whatever you like. Make the project better (or break it horribly, it’s up to you). For an example of how powerful social coding is take a look at Github. The 3D JavaScript library three.js for example has over 4000 people watching and 400+ people have forked (cloned) the repository. If your version adds a cool new feature or fixes a bug, it can easily be merged back into the original project!

If you are interested in learning how to use Git there are some superb resources available. For people who like screencasts I highly recommend watching the one created by Peepcode. It’s only $9 (US), 1 hour-long and will get you up and running with Git in no time at all! Here are some other resources I found useful:

Right, I’m off to start committing to a repository while travelling on a train to work when I don’t have an internet connection (warm fuzzy feeling enabled)!

Currently on page 1 of 1112345Last »