Thursday, May 31, 2012

Is jQuery always the answer?


I've come across a couple questions, such as this one , and I really have to wonder why "Use jQuery" seems to be the answer when somebody asks how to do something in JavaScript. I understand that jQuery can save you a lot of time, and can help you out a lot, especially when you are doing a lot of fancy JavaScript in your site. However, in instances like this, and in many other instances, it seems like it's just jumping around the problem instead of answering the question.



I also feel like this builds too much dependency into libraries. I've seen way too many developers that simply rely too much on libraries, and if they encounter a situation where they didn't have the library, they would be completely unable to function. I feel like there are already enough developers who don't know JavaScript, without just telling everybody to not learn JavaScript, and use jQuery.



So, just to reiterate the question. Do you think there's too much of a tendency to use jQuery, for small pieces of JavaScript, when most of the functionality of jQuery isn't being used. Should developers be fluent in the use of bare JavaScript so they don't get too dependent on using libraries?



[Additional related conversation topic]



Does the existence of jQuery give too much slack to web browser developers who write the JavaScript engines? If we just have workarounds to cover all the inconsistencies in JavaScript, what pressure is there on browser makers to ensure that their JavaScript library works as it should. I feel like this extrapolates the same problem discussed in SO Podcast #36 of "be conservative in what you send, liberal in what you accept". By being so liberal with bad JavaScript engines, and using a common library to work around the flaws, we are promoting their use, and extending the problem.


Source: Tips4all

22 comments:

  1. Do you think there's too much of a
    tendency to use JQuery, for smal
    pieces of javascript, when most of the
    functionality of JQuery isn't being
    used. Should developers be fluent in
    the use of bare Javascript so they
    don't get too dependant on using
    libraries?


    No.

    JQuery, Prototype, Dojo and countless other frameworks provide a baseline of how JavaScript and specifically DOM manipulation should work. They allow users to focus on solving business problems at hand instead of fiddling around with browser quirks.

    You may as well ask if we're too dependent on java.util or java.lang.

    ReplyDelete
  2. There are two rules to follow if you want to get your answers voted up on SO.

    1/ Answer every web question with "jQuery".

    2/ Answer every C++ question with "Boost".

    3/ Answer every optimisation question with "premature" and "evil"

    4/ Learn to count properly.

    Boom boom, I'm here all week, you've been a great audience...

    ReplyDelete
  3. Yes and No.

    Developers should always be able to 'rough it' if need be. But in a corporate environment where your customer wants more cowbell yesterday, you need to be able to give it to them.

    Why re-invent the wheel?

    ReplyDelete
  4. Do you think there's too much of a tendency to use JQuery


    To use JQuery? No. It's a reasonable JS library with many purposes, in real life.

    OTOH to answer SO questions, sure. It's the stock answer for anyone who doesn't know JavaScript, or doesn't even know JQuery, but answers "use JQuery" as a knee-jerk response that earns points.

    I see the JQuery mafia have already voted your question down several points - congratulations. Me, I'm automatically voting down "use JQuery!" answers to questions that ask for a JavaScript solution. SO should be about learning.

    ReplyDelete
  5. Frameworks are awesome; I love 'em. Unfortunately they're no substitute for understanding the technologies upon which they're built, and upon which those technologies are built, and so on down the line. I'm all for progress in software, all for libraries and frameworks and how much more they let me do in so much less time than ever, but way more than all that, I'm dead-against being okay with not understanding exactly what it is the tools, the frameworks, are doing for you, how they work, and how to live without them.

    A few years ago, I worked with a bunch of ColdFusion developers. ColdFusion is the red-headed stepchild of Web-app frameworks; it gets no respect, which is frankly a shame, because as strange as it is, and it is strange, it's actually a decent commercial product; I even use it myself from time to time. Anyway these developers had built for their employer a fairly massive little online enterprise; so successful was their collective effort that it began, as usage grew, to buckle and collapse under its own weight; every day, at peak hours, the site would slow down, then crawl, then hang, then time out like crazy, and once enough customers called to complain, the owner would rush into the tech lead's office demanding that something be done. The tech lead, a good guy, but alas one with no formal CS training and knowledge only of CF, could only respond by bouncing all the machines, declaring the problem "fixed," and sit back and wait, until it happened again. Which it did, every day, with increasing catastrophe, for years.

    In the end, we discovered the problem was being caused by the obscure, counterintuitive and undocumented way ColdFusion handles connection pooling. The tech lead could've had no idea this was the case, though, because he didn't know what connection pooling was -- nor did he suspect even for a moment the config settings he'd specified himself, in earnest attempt to improve the performance problems, actually made them horribly worse. All he knew was that the ColdFusion Administrator gave him a couple of radio buttons, a textbox and a submit button; he didn't know what that radio button did, or what it actually meant to set timeouts to sixty seconds instead of ten. Ultimately, all it took was a five second config tweak, a JVM restart, and that was it -- the problem literally went away immediately, for good.

    Frameworks are awesome. JQuery's awesome! But it's better to know JavaScript cold, know the DOM, know Ajax design patterns, know the intricacies of the language, know how to program asynchronously, etc., than to be a JQuery super-ninja -- because eventually, you'll run into situations JQuery wasn't designed for, or doesn't do quite right, or the project manager simply won't allow it, and you'll have to figure things out for yourself. To the OP, I say your instinct is right on the money -- JQuery is often the answer, but the real answer is not to have to rely on it in the first place.

    So in other words, No. :)

    ReplyDelete
  6. Short Answer: Yes

    Myself I try to stay away from frameworks because I think they create to much magic.

    I don't mind using a framework like jQuery, but only after I learned how to build the code myself.

    ReplyDelete
  7. I think the libraries are great.
    I use them all the time.
    I also have my own small utilities library for when I don't need a Beowulf cluster to count to 10.

    It seems a lot of JavaScript developers are really jQuery button clickers, judging from some of the questions I've seen. -- like 'how can you tell which option has been selected in jQuery?' and the like -- This is very basic, core JavaScript and developers should get to know this stuff.

    I say use the libraries BUT take the time to learn the core it'll make you a better JavaScript developer.

    ReplyDelete
  8. It reminds me of how some people always reply to computer/OS problems with "Just buy a Mac".

    There's a touch of zealotry in there.

    ReplyDelete
  9. There are a lot of questions within this question, but I'll answer the primary one based upon a lot of experience with the library itself and JavaScript: jQuery is always the answer. I don't mean to say Prototype or Mootools or Dojo can't be the answer, but I can tell you that even if you are doing simple JavaScript, then you should start with a library. For the last year, every project big or small that I work on, I always start out by saying to myself, "Oh, I just need a few selectors, or maybe an event model." Before I know it, I'm at 100+ lines of utility functions to handle that and the cross-browser issues. Given the efficiencies you gain and the ability to compress/cache the library, it really does makes sense.

    The one caveat I will put to this is that you can't build a full-fledged Web application without being proficient in custom JavaScript. The library is a tool, not an architecture. It can't be the whole solution to your problem. It does not answer any questions you have related to the problem domain -- it just helps you solve the problem quicker once you know the solution.

    ReplyDelete
  10. Unlike some other Javascripot libraries, jQuery doesn't alter Javascript in any way. jQuery provides a series of convenience methods which make common and repetitive DOM manipulation tasks easier, and in doing so insulates you from some of the tedious DOM details and cross browser quirks. I don't think you can work effectively with jQuery without knowing Javascript, but you could still use it without a thorough knowledge of the DOM.

    Is jQuery used too much? I think it depends on the situation, if all you're using it for is trivial DOM manipulation (e.g. showing/hiding form fields depending on checkbox values), I'd question whether the "weight" of the jQuery library is worth it. If on the other hand you're already using jQuery for other stuff on the same page (or can reasonably expect users to have it cached from another page on the site), I don't see a hassle with using it for "trivial" stuff as well.

    Would I prefer a programmer on my team to know how to code against the DOM without jQuery? Absolutely. Would I want a programmer on my team used jQuery for any moderately complex Javascript task rather than deal with cross browser and other random issues? Definately.

    ReplyDelete
  11. No. Improvements in software development rely heavily on better and better abstraction layers. Back in the day, people argued everyone should write everything in Machine Language. They were wrong.

    jQuery is a very useful abstraction layer that makes it much faster and easier to use Javascipt. It should be embraced fully. As people get more proficient in jQuery, they'll also grow in their Javascript understanding.

    ReplyDelete
  12. No.

    I love jQuery since it allows me to get things done. I hate JavaScript as it just seems like you need to know so many quirks to make it work in all browsers and then the language itself is so ugly and hard to debug.

    However these days people expect web sites to be super fancy and they should never leave the page and jQuery allows us developers to give this stuff and have some sort of life.

    Like I know some people here have posted it is the easy way out and you have no will to learn the hard way. I disagree with this so much.

    First if doing it the hard way is just so great why are we using computers? I thought it was to make our life easier? Why don't we just do everything by hand and forget using computers at least where not taking the hard way out.

    Second it is still extremely hard to be a developer and we need tools to make our lives easier because people expect more and more. Back in the 90's when websites just first came out all they where static pages with some images on them.

    Now web sites need to be tied to databases, use ajax, possibly fancy things going on with flash, nice web site designs and etc. This is what people expect. In the 90's all you needed was just plain old notepad that you could get the job done now if you did everything from hand in note pad you would not get anywhere.

    It still is not easy being a programmer(if it was then we would not be needed). I am currently making a web site and to make it I am using jQuery, C#, asp.net MVC, linq to sql, ms sql database.

    That is a ton to learn and lets face it if you get a job 90% of the time you have to know more then one thing. I can't just go I am going to learn C# and that's it your going to have a hard time finding a job since they will want on top of it .net, php or whatever. So we need libraries to speed up learning and hide us from stupid things like browser compatibility otherwise there is no way to learn it all.

    "I also feel like this builds too much dependency into libraries" - Original Post

    Oh I agree with this I don't know where I would be without libraries and I really don't care. Thats just how life is you get dependencies on stuff.

    How many people cook these days? Most people buy fast food or premade stuff not many people cook from scratch what happens if that disappears over night what would people do? How about your car do you the in and outs of your car?

    Thats why we live in a society, so we all can build off other people strengths and we don't have to do it all ourselves.

    Finally in the end does anyone care if you use stuff to make things faster. I think your boss cares if you tell them that your not going to use this library and do it the "hard way" and cost the company 100's or 1000's of dollars more then if you just used the library. I think they will fire your ass so fast you won't know what hit you.

    ReplyDelete
  13. I have X number of hours I can be effective working a day and Y number of hours worth of stuff I need to get done each day. Unfortunately I'm usually in a position of Y > X. If jQuery (or any other library) can change that equation in a way without accruing technical debt then I am thankful for the library creators efforts.

    ReplyDelete
  14. I think this article really brings it home:


    The Burden of Popularity

    It’s only natural that, once you reach a certain – for lack of better
    words – popularity, you open yourself to incredible amounts of
    scrutiny. Everyone has an opinion about the spotlight, it seems. For
    example, you’ve no doubt heard endless criticism of Catholicism. “They
    worship statues.” Is that the only religious body with questionable
    history? Surely not; but it’s the largest. The United States is one of
    the most powerful countries in the world. Naturally, everyone has an
    opinion. “Americans are ignorant and fat.” Ignore the good, and
    spotlight the bad.

    jQuery is the most popular JavaScript library, by a landslide. Again,
    everyone has an opinion. I’ve read countless criticisms – everything
    from what it doesn’t do, to the structure of the code base. However,
    perhaps the biggest criticism of jQuery comes from a simple truth: its
    users, as an average, are less experienced with vanilla JavaScript,
    when compared to, say, Mootools. While some consider this to be a
    downside, I honestly view it as a strength – in an odd way.


    ....


    Compare this to simple Math. When you first learned how to add 2 + 2,
    did you do it the official way? I sure didn’t; I used my fingers. I
    bet you did too. Though it’s a huge simplification of the issue, is
    jQuery not the same way? It provides new JavaScript users with a
    simple and exciting entry point. Do you honestly expect them to learn
    about closures and objects and global variables before they even learn
    how to do something fun? Come, come, now.

    The strict, “Learn it the right way, or you’re a fool” viewpoint is
    naive, and doesn’t take into account the various learning styles that
    we all have.


    ...


    It’s undeniable that many of us feel a need to be trail blazers.
    Remember when parents began signing up for Facebook? Critics widely
    declared that the end of Facebook was near. Once you sacrifice
    exclusivity for wide appeal, people instinctively begin searching for
    the next thing. But that didn’t happen. The same is true for jQuery.
    Sure, some users have moved on to more comprehensive frameworks, like
    Dojo. But that’s to be expected, and should be a badge of honor for
    jQuery. For many, the learning cycle goes like this:


    Learn jQuery; get excited.
    Realize that you have no clue what this
    refers to in different situations. Learn JavaScript, and incrementally
    improve your old jQuery code.
    (Optional) Advance your skills to the
    point where you need a more comprehensive framework for building large
    applications. Begin reviewing additional tools, such as Dojo.

    ReplyDelete
  15. Kind of.

    I understand why jQuery answers are so common -- it's a good, capable library that helps minimize the code you have to write to accomplish something.

    But, I do think it gets abused. sometimes it really feels like a cop out. When it's a 1-line answer saying Use jQuery, then it's definitely ridiculous. But, of course, this depends a lot on the author.

    On the other hand, it does help to know the core of JavaScript and DOM manipulation so you can understand why jQuery is so helpful. No library should ever become a dependency or bandwagon.

    ReplyDelete
  16. "Does the existence of jQuery give too much slack to web browser developers who write the JavaScript engines?"

    That's absurd. The JS engine developers write things differently because they think their way is better. Engines continued to be developed with different standards for years and years before jQuery or any of the other major frameworks ever existed. There's no way that jQuery would be the sole thing keeping them from changing their ways, especially when jQuery developers can simply adapt the framework to new engine code.

    Sure, it'd be great if all the JS engine developers agreed to use one engine, but it's a difference between what's ideal and what's realistic.

    Personally, I'd rather more people use jQuery. The Internet's filled with small sites created by designers/developers that know JS just well enough to get their script working in one browser, but it breaks in the others. If those same people simply used jQuery to eliminate a good chunk of cross-browser problems, it would be a far user-friendlier place.

    ReplyDelete
  17. Using frameworks is an absolutely great (if not required) safety net in this cross browser slush we find ourselves in.

    jQuery is not the only option. It depends on what you are doing. jQuery focuses on manipulation of the DOM. If your project is not doing a lot of that, another framework may be a better choice. It depends on that strengths you need.

    However, understanding the underlying technology and how it functions is vital to writing code which property takes advantages of browser strengths, and steps around their weaknesses.

    ReplyDelete
  18. I still remembered I got jQuery and UI for just a colorpicker and some sliders (for a little drawing app).
    I had many copies of jQuery and UI on my github JavaScript projects.
    For the long term, I should include jQuery because my app gets complicated over time.
    (is it a hotlink to link jQuery from Google AJAX APIs?)

    Sometimes the majority decides if a browser is "compatible", if every used jQuery, then every browsers will eventually use jQuery as the JavaScript/DOM specification.

    When answering a question, if you are temped to answer "jQuery!", provide a rational explanation to it and how the workaround works.

    ReplyDelete
  19. I'm pretty sure that if we jump in our time machine and go back to the 1960s and 1970s you'll find programmers arguing that FORTRAN and other HLLs are not the answer and that programmers need to know how to write code that runs on bare metal, and so on, and so forth.

    Some programmers do need to know how the CPU instructions work. Most don't. The same is true of Javascript and jQuery. If you know enough Javascript that you technically don't need jQuery, I admire you. You're a better programmer than someone who can't code without jQuery. But that doesn't mean he isn't a programmer or that he can't get things done or that jQuery isn't the answer for him just because you don't need it.

    ReplyDelete
  20. For every project I work on depending on what needs to be done, I either use JQuery or ExtJS. ExtJS has the benefit of well standardized components and a large community which creates plugins for them.

    ReplyDelete
  21. jQuery should replace Javascript in the browser engines, imo.

    Heck, I don't even know HTML because I know JSF.

    This is why programmers don't use assembler anymore. We use high-level languages that let us focus on the big problems.

    ReplyDelete