Wednesday, August 29th, 2007
Naming is hard. Lachlan Hunt took the naming of methods in the new W3C Selectors API very seriously and after choosing
selectElement() and selectAllElements() he wrote a bit of a tomb describing how he came up with the names:
Rationale for Naming Principles
A short name is important for several reasons. Since these methods are
designed, and behave, as a superset of the existing getElementsBy*
methods, it is likely that their use will significantly replace the use
of previous methods.
Based on evidence from many widely used JS libraries, and other
feedback, we know that authors generally prefer shorter names over
longer names. This is particularly true for methods that will be
Not only does a shorter name reduce the amount of typing, it can help to
improve the readability of the source code, which in turn makes code
easier to maintain. Given these reasons, I have concluded that, within
reason, short names are a fundamental requirement that must be adhered to.
Ideally, the chosen method names would be relatively clear and
unambiguous with regards to their purpose, usage and return value.
However, this must be put into perspective; the names do not need to
describe these aspects perfectly.
I am glad it isn’t
NOTE: What is the Selectors API?
Selectors, which are widely used in CSS, are patterns that match against elements in a tree structure. The Selectors API specification defines methods for retrieving Element nodes from the DOM by matching against a group of selectors. It is often desirable to perform DOM operations on a specific set of elements in a document. These methods simplify the process of aquiring specific elements, especially compared with the more verbose techniques defined and used in the past.
UPDATE: Dean has actually implemented the Selectors API in his base2.DOM library and has taken out his
getElementsByClassName implementation (as he can’t return a live node list). All you have to do is s/document.getElementsByClassName(“my-class”);/document.selectAllElements(“.my-class”);/ so to speak.
Posted by Dion Almaer at 6:49 am