Hi there,
If you ask me (and for the sake of discussion I'll act like you did) I'd say;
I don't really get why all code should be "sexy" and "elegant" were not talking about women here, were talking about JavaScript.
In my opinion "sexy" and "elegant" are too subjective to take into account when looking at code.
The most important thing is efficiency and legibility, not only for yourself but also for other people who read your code, be it in a team of coders or somewhere else.
Checks to find out if an element exist are available in vanilla JavaScript for ages, why not use those?
They're native so they're extremely fast, they are legible and everyone who reads your code (should) know(s) what it means.
Given the circumstance where you would like to see if a certain id is in the DOM just use
- if (document.getElementById("isItInTeDOM")) {
- dostuff...
- }
I know its longer to write than
- if ($("#isItInTheDOM").length) {
- dostuff...
- }
but its a hell of a lot faster to execute (some tests of mine show a 60% speed increase).
And when you pass this code through Closure Compiler and use certain coding standards like :
- (function (window, document, $, undefined) {
- execute all your code here
- })(window, document, jQuery)
the document gets shortened to (probably) a single char var.
So that kinda levels the playing field download size wise.
The letters you write are not the only thing that matter when it comes to JavaScript, it's also a lot about what the framework has to write to let you write it the way you would want it to be written.
All with its own overhead.
Best practices, ok. but when you start to value code on the "sexyness" of it I think your making it harder for everyone.
Besides, one of the things I value greatly about jQuery is the fact that its not bloated with stuff you can do easily with vanilla JavaScript, especially since vanilla (when its cross browser compatible) is always faster than framework code.
This in my opinion only bloats the jQuery code-base.
Just my two cents...
Superficial.