The dollar function must die
19 February 2009
A number of JavaScript libraries have a function called $
and I’m irritated by it for a number of reasons:
The dollar sign is intended for use only in mechanically generated code.
… which the libraries are not.
This part of the spec was removed for version 5. I wonder if the authors decided it wasn't a good idea after all, or just gave up in response to the number of people ignoring it.
Purity of code is all very well, but on to more practical matters…
The name doesn’t give a clue as to what the function does or where maintainers should look for documentation.
Different libraries use the $
function to do different things.
In Prototype.js it means “Get the element with this id”, but in
jQuery it means “Get all the elements that match this CSS selector”
(except when it means “Don't run this function until you've finished
parsing the document” — yes, it does different, and completely unrelated,
things depending on what arguments you give it!).
Aren't we supposed to embrace self-documenting code?
If you have a library with a $ function, please don’t use it.
Half-decent ones will give you an alternative (such as jQuery with
jQuery()
) or don’t create the function at all (such as
YUI).