News for hackers. Stuff that matters.
Re:JQuery Code Smells by pmichaud on Thursday January 07, 2010 @01:09PM (#1037595)
I don't see how prefixing names with $ is a "smell". It's just a convenient way to differentiate jQuery objects quickly. sort of like "strVar" or "intVar".
Re:JQuery Code Smells by axod on Thursday January 07, 2010 @01:13PM (#1037609)
Because it's dishonest, and makes for bad code.

$('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?
Re:JQuery Code Smells by buckwilson on Thursday January 07, 2010 @01:19PM (#1037632)
I think you kind of missed something. The $ by itself in a variable name doesn't do anything. It's simply a notation to keep your DOM elements (and other variables / objects) separated from your jQuery wrapped elements.

$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.

Re:JQuery Code Smells by axod on Thursday January 07, 2010 @01:26PM (#1037652)
Yes, I know. My point is that most people won't know/bother doing

$myDiv = $('myDiv');

They'll simply use $('myDiv') all over the place which will be horrible.

Re:JQuery Code Smells by noodle on Thursday January 07, 2010 @03:29PM (#1037962)
so, the complaint is that people who don't understand jquery use it incorrectly/inefficiently?
Re:JQuery Code Smells by axod on Thursday January 07, 2010 @05:05PM (#1038129)
meh. If you like it use it. Personally I find that code ugly, inefficient and error prone.
Re:JQuery Code Smells by pmichaud on Thursday January 07, 2010 @03:49PM (#1038006)
I'm a little confused here. First, let me echo my sibling comment by saying that, yes, $ is a function, but no, the name "$var" will not execute the $ function. To me it's a method of notation, as the sibling also said.

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?

Re:JQuery Code Smells by axod on Thursday January 07, 2010 @05:04PM (#1038127)
I'm saying that using $ to name a function, isn't a good idea.

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.

Re:JQuery Code Smells by pmichaud on Thursday January 07, 2010 @06:21PM (#1038313)
Properties.

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.

Re:JQuery Code Smells by axod on Thursday January 07, 2010 @06:46PM (#1038365)
Not really. I'm a sole developer at the moment, so I value efficient easy to read code. Mainly a matter of taste though I expect.