Please stop using features by testing for browser

An issue that I encountered and why it shouldn't have been

Today I had to debug some code that up until fairly recently was working 100%.

The issue was the code (some jQuery) was checking to see if the browser was Firefox and if it was it should use some Firefox specific code;

if ($.browser.mozilla) { e.mozSlice(a,b) }

Up until version 30 of Firefox this code would have worked, but mozSlice hasn’t been needed for some time and was finally removed as of Firefox 30.

Now, there are a few ways around this, a really naive way would be to add in some version checking as well, so if the browser is Firefox and the version is < 30 use mozSlice, else use slice.

These type of checks are really redundant and in 6 months time you will revisit the code and wonder what was so special about version 30, instead you can make your code much more clear by testing instead for the feature itself.

if (undefined !== e.mozSlice) { e.mozSlice(a, b) }

This code says, if we have access to mozSlice, lets use it.

This means that first of all if Internet Explorer decided to implement the mozSlice function instead of Slice this would work, it also means that if Firefox decided to drop mozSlice (which they have) nothing would break.

Recent posts View all


Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.