views:

3535

answers:

9

So I have been exploring different methods to clean up and test my JavaScript. I figured just like any other language one way to get better is to read good code. jQuery is very popular so it must have a certain degree of good coding.

So why when I run jQuery through JSLint's validation it gives me this message:

Error:

Problem at line 18 character 5: Expected an identifier and instead saw 'undefined' (a reserved word).

undefined,

Problem at line 24 character 27: Missing semicolon.

jQuery = window.jQuery = window.$ = function( selector, context ) {

Problem at line 24 character 28: Expected an identifier and instead saw '='.

jQuery = window.jQuery = window.$ = function( selector, context ) {

Problem at line 24 character 28: Stopping, unable to continue. (0% scanned).

This was done using JSLint and jquery-1.3.1.js

+43  A: 

JSLint tests one particular person's (Douglas Crockford) opinions regarding what makes good JavaScript code. Crockford is very good, but some of his opinions are anal retentive at best, like the underscore rule, or the use of the increment/decrement operators.

Many of the issues being tagged by JSLint in the above output are issues that Crockford feels leads to difficult to maintain code.

There are some things Crockford identifies as errors that I agree with though, like the missing semicolons thing. Dropping semicolons forces the browser to guess where to insert the end-of-statement token, and that can sometimes be dangerous (it's always slower). And several of those errors are related to JSLint not expecting or supporting multiple assignments like jQuery does on line 24.

If you've got a question about a JSLint error, e-mail Crockford, he's really good about replying, and with his reply, you'll at least know why JSLint was implemented that way.

Oh, and just because a library is popular doesn't mean it's code is any good. JQuery is popular because it's a relatively fast, easy to use library. That it's well implemented is rather inconsequential to it's popularity among many. However, you should certainly be reading more code, we all should.

JSLint can be very helpful in identifying problems with the code, even if JQuery doesn't pass the standards it desires.

foxxtrot
all points well taken, and good to point out that popular doesn't equal good
Bobby Borszich
JQery actually tries to be jslint compatible now! Look here ->http://forum.jquery.com/topic/jquery-1-4-2-jslint-report
Paul Weber
+4  A: 

JSLint helps you catch problems, it isn't a test of validity or a replacement for thinking. jQuery is pretty advanced as js goes, which makes such a result understandable. I mean the first couple of lines are speed hacks, no wonder the most rigid js parser is going have a couple of errors.

In any case, the assumption that popular code is perfectly correct code or even 'good' is flawed. JQuery code is good, and you can learn a lot of from reading it. You should still run your stuff through JSLint, if only because it's good to hear another opinion on what you've written.

From JSLint's description:

JSLint takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate location within the source. The problem is not necessarily a syntax error, although it often is. JSLint looks at some style conventions as well as structural problems. It does not prove that your program is correct. It just provides another set of eyes to help spot problems.

JSLint defines a professional subset of JavaScript, a stricter language than that defined by Edition 3 of the ECMAScript Language Specification. The subset is related to recommendations found in Code Conventions for the JavaScript Programming Language.

jacobangel
great explanation - good addition of link form JSLint site
Bobby Borszich
+2  A: 

If you're not actively developing jQuery itself, why even run JSLint over it at all? If it works, it works, and you don't have to worry about it.

nickf
I understand what you are saying, but how would an explanation like this help our fellow coders?
Bobby Borszich
It's hopefully getting people to think about what's relevant and what's not worth spending time worrying about?
nickf
+2  A: 

The jQuery developers' goals are not the same as your goals. jQuery is built for speed and compactness and achieving those goals trumps readability and maintainability.

Crockford's tests in JSLint have more to do with achieving something that he would feel comfortable taking home to meet his mother, which is a valid concern if you will be married to your code for some time.

Prestaul
+3  A: 

The purpose of JsLint is clearly stated in the FAQ [1]:

"JSLint defines a professional subset of JavaScript, a stricter language than that defined by Edition 3 of the ECMAScript Language Specification. The subset is related to recommendations found in Code Conventions for the JavaScript Programming Language."

Now if you are confused: ECMA3 is already a subset of the JS capabilities provided by any of todays JS interpreters (see [2] for an overview of the relation between JavasScript and ECMAScript versions)

To answer the quesition "what good is JSlint": * use JsLint to verify you are using a "safe" subset of Javascript that is unlikly to break accross JS implementations. * use Jslint to verify you followed the crockford code conventions [4]

oberhamsi
+7  A: 

"jQuery is very popular so it must have a certain degree of good coding."

One would like to hope this is the case with jQuery, but unfortunately it's not really true. jQuery is useful and popular, but it is not a well written JavaScript library. David Mark recently posted a scathing critique of jQuery in comp.lang.javascript that examines a large number of examples of the poor code found in jQuery:

http://groups.google.com/group/comp.lang.javascript/msg/37cb11852d7ca75c?hl=en&

vsedach
I just read that entire thread, and learned that David Mark is a dick, jQuery sucks, all JS libraries suck, anything you don't write yourself sucks, and even if you do write it, it still sucks. What I didn't find were any alternatives to jQuery, or really any helpful suggestions at all. jQuery it is
Joel Mueller
"Alternatives to jQuery"? Depends what you want to use if for. jQuery has so many features, nothing good will have them all. Small libs have small featuresets so you can use the ones you need.
singpolyma
Apparently the one that is supposed to suck least is http://forkjavascript.org/I'm going to try it for my next project.
vsedach
A: 

I've found one case where JSLint is very, very useful: when you grab one of those big-arse libraries that float around the 'Net, then another, then again one other, you soon find yourself loading 50k of Javascript on every new page load (caching may help, but it's not a cure-all solution).

What would you do then? Compress those libraries. But your host doesn't do compression for non-html file! So what? You use a Javascript compressor.

The best I've found is Dean Edward's; I used it to compress John Fraser's Showdown (a Markdown for Javascript library), but unfortunately, the compression broke the code. Since Showdown isn't supported anymore, I had to correct it myself - and JSlint was invaluable for that.

In short, JSlint is useful to prepare your JS code for heavy duty compression.

Mitch 74
A: 

If you like to daisy-chain methods like jQuery allows you, you might appreciate YUI3.

Marco Mariani
A: 

JQuery is of course not the best thing in the world. That's already clear when you look at the notation. The dollar parentheses combination is really bad for your eyes. Programming should be clear and simple. JQuery is far from that. That reason is enough for me not to use it. That it's not properly written doesn't surprise me and only underscores my thoughts on this JavaScript library.

mike