JQuery Code Smells
james.padolsey.com
james.padolsey.com
So don't do it. It's not required.
> Concatenating a bunch of things together to make a selector
This is semi-valid and using the jQuery.filter() function helps a bit. Overall though, it's not a huge problem, IMHO. Just put together the selector incrementally. Likely you need bits of that selector elsewhere anyways.
> Going over the top with chaining
I've written some complex code using jQuery and none of it had a chain as long as your example. Still don't see how this is jQuery's fault.
> Not caching collections
Then cache them. How is jQuery preventing you from doing this?
> HTML
This is just runs faster. When performance is critical, I find it acceptable to do this.
That's exactly what I thought ... until I read the article. Your intent seems pretty clear to me; but if a lot of people are misunderstanding you, that's a good sign you need to change the message to address the widespread confusion.
The author is pointing out coding practices that cause him to suspect that others' jQuery code may not be very good.
1. Use a slow js library framework abstraction layer! yay!
2. Speed it back up a bit by using ugly messes of html stringsright now it reads like "jquery code is ugly. here's some examples of how ugly it is". and people are simply pointing out that the ugliness is purely self-inflicted. the code examples don't need/have to be written as such. you can classify them as stylistic choices. so its more like "my jquery code is ugly" or "i found these ugly snippets online".
just my $0.02.
Wikipedias definition of code smell:
"In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem."
I don't see the deeper problems with the smells the article brings up and if there are any the article could've done a better job explaining them.
People use $ within loops without a second thought. Why wouldn't they.
It makes for quite ugly inefficient code IMHO. Having said that, it does fill a niche for people who don't want to sit down and learn javascript.
Anywho, my major use of jQuery is for dom parsing and manipulation. It's just so darn comfy! I still need to know javascript for all the rest of the stuff the app is supposed to do though.
$('body') looks to the uninitiated to be a simple variable. Maybe they come from the PHP world, so they think "Ah! that's just the variable body from the DOM, ok great.
When in reality, $ is the name of a mammoth function that could take ages to execute. It'll also execute every time.
eg
for (var i=0;i<1000;i++) {
$('silly').doSomthing();
}
This is horrible horrible code. It's calling the function $ 1000 times. Why?$myDiv = $('#myDiv');
Now I know every time that $myDiv is already wrapped in jQ and I won't make the mistake of wrapping it again $($myDiv) in my code.
$myDiv = $('myDiv');
They'll simply use $('myDiv') all over the place which will be horrible.
But you seem to understand that point, so I'm confused why you said what you said.
You're saying using $ as notation might confuse people new to jQuery and presumably javascript, who might not realize that $ is a function. You're saying that they would or might therefore conflate $var with $('selector'), leading to inefficient code?
So, I should abandon a concise convention because a tiny subset of the programmer population might misinterpret a detail of my code, causing them to implement superficially similar code in an inefficient way? Is this a problem you run into a lot?
It looks cheap to use, but it's not. That's my point.
Same reason I hate those things in C# is it? where you can get some code to run each time a variable is read/written to.
Properties make for some mighty clean code though.
I guess you have a particular bias toward protecting systems from crap programmers, which may serve you well in your environment. In my environment being concise wins because no one is an amateur.