14

I am baffled by the difference in design between jQuery and Yahoo UI APIs. Disclaimer: I have a strong dislike of the jQuery api, but I am also a huge ignorant in web programming and javascript in general, so I could be dead wrong and come back here begging for redemption. so long...

The point of my question is the following. The two designs are different. jQuery puts the DOM at the center, and adorns the DOM by executing a "trigger" enhancer method on it. example

$("#flexigrid").flexigrid()

A requirement of jQuery is that you must, in some cases, follow a very specific conventional structure for your html beforehand. Example:

<div id="accordion">
    <h3><a href="#">First header</a></h3>
    <div>First content</div>
    <h3><a href="#">Second header</a></h3>
    <div>Second content</div>
</div>

and then

$("#accordion").accordion();

Moreover, the returned entity in general does not provide any mechanism to hide the DOM through a convenient programmatic method. To manipulate your jQuery entity you have to access the DOM via selectors, access that in some case is not guaranteed to be easy to know, like in the case of internally generated ids. Suppose that you want to swap the accordion programmatically, what you do is

$('#accordion').accordion('option', 'active', 2);

and not a more intuitive

myAccordion.setActiveTab(2);

On the other hand, yahoo ui focuses on javascript objects, you create them passing the DOM node selector (e.g. myDataTable = new YAHOO.widget.DataTable("container_id")) and then perform all manipulations through the object methods. Want to add a new row ? call myDataTable.addRow(). The DOM is hidden. You are not concerned with what's going on behind the scenes.

Now, my experience with Trolltech QT maps nicely to Yahoo UI. Clear, defined API of the widget objects, eventual freedom to reimplement part of them via inheritance, opaque rendering unless you want to open the box and get your hands dirty. QT is a winning API, works well, it's easy to use, and Yahoo UI is kind of similar in the design style. On the other hand, jQuery works in a counterintuitive (to me), very open box way, with reduced API on its objects.

Enough ranting. The point is that I assume I can be dead wrong on this, but I'd like to know why. What are the design advantages of having a jQuery-like interface (where the DOM is clearly exposed and you potentially have to hunt for stuff that jQuery plugins create automagically, so you can finally $(select) them and attach events or modify their content) instead of hiding everything behind an objects and commodity methods like YUI does ?

I'm not talking about speed, or code size, or amount of typing. I'm talking about design concepts like encapsulation, focus on interfaces, and ease of access. What design is better, in what situations, and why?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • 5
    "I strongly believe jQuery is horrible" ... even though it's an opinion, it's pretty strong. Wanna tone that down a level or two? – James Jan 04 '10 at 16:38
  • 2
    well, I wanted to be clear with a full disclosure, but if you think so, I'll do it. – Stefano Borini Jan 04 '10 at 16:39
  • 2
    Nah, bring it on! I personally believe the opposite. jQuery transformed my web development, making it much easier. Wish I had the time at the min to lay down a proper answer.. – Mongus Pong Jan 04 '10 at 16:40
  • 5
    Are you comparing jQuery or jQuery UI to Yahoo UI? I would think an apples to apples would be jQuery UI. – Robert Harvey Jan 04 '10 at 16:41
  • well, kind of. jQuery plugins actually. I take flexigrid against yahoo datatable because are the ones I am working with right now. – Stefano Borini Jan 04 '10 at 16:43
  • I use jQuery primarily as an abstract layer over the DOM. In that role it does a very capable job, especially of abstracting away functional differences between browsers. Yahoo UI includes a complete framework, whereas jQuery relies on plugins. I'm sure both are very good, and each has its proponents. So I say, whatever floats your boat. – Robert Harvey Jan 04 '10 at 16:47
  • added a bit more details for actual use cases. – Stefano Borini Jan 04 '10 at 16:48

6 Answers6

12

I don't think your argument is directed at jQuery, but more the APIs provided by plugin authors.

Unfortunately, no two plugin authors will create a plugin with the same API. The level of programmatic access is not limited by jQuery itself, but rather by the author/s of the plugin.

Also, as you said, jQuery is all about the DOM -- I see this as a benefit because it means jQuery doesn't get all mixed up in the "logic" (eh, "business logic") of the application... It's quite fine on it's own level of abstraction -- it deals with the DOM, and that's all!

You can create an unlimited amount of data structures and additional APIs for your application. jQuery doesn't hinder you in this respect.


You've added more details to your question -- this 'edit' is in response to those details.

I think what you're experiencing is common when you reach a certain stage with jQuery... The API becomes insufficient. You don't want the DOM... You want a nice clean API for your module, whether it's an accordion or a data-grid.

Personally, I don't think that some things should be bundled into a "jQuery plugin" -- doing so normally means sacrificing the API -- or having to resort to jQuery's mechanisms such as psuedo-event triggering through "trigger":

var someModule = $('#contain').someCoolModule();
someModule.trigger('initiate');

I get what you're saying, and I think I agree, but I also think it's important to have jQuery on an entirely separate level -- forget about it -- only utilise it when you need to attack the DOM.

James
  • 109,676
  • 31
  • 162
  • 175
  • this is an interesting point. jQuery is perfect to manipulate the DOM, but how is this architecture philosophy when you move at a higher level ? – Stefano Borini Jan 04 '10 at 16:56
  • jQuery's API "philosophy" is by no means ideal, and it won't suit every situation. If you feel limited by the API, then stop using it; break out of jQuery -- still utilise it -- but don't get muddled up in the API. – James Jan 04 '10 at 17:02
  • It's *not* for higher level use. It's the middle-level, extremely powerful library much of the 'higher-level' toolkits are built on. But unless you compromise to fit the capabilities of your chosen toolkit, you usually have to resort to using it a lot. – Lilith River Jan 04 '10 at 22:32
10

jQuery doesn't require any kind of special markup - you can write a selector for any object. You can also use an existing DOM reference, and turn it into a jQuery object like so: $(domObject). Actually simpler and more capable than Yahoo UIs.

It's not required to know your dom selectors if you already have a DOM reference... That might be the source of your misunderstanding.

Having worked with both Yahoo UI and jQuery, let me tell you they are both great libraries. They're for different roles, but both have great approaches.

jQuery is kind of a wrapper, simplifying everything that has to do with the DOM, Ajax, selecting objects, doing graphics. It has a very concise and brilliantly simple API that abstracts all the browser compat nonsense away.

jQuery uses radically different design concepts than most newbie programmers are used to. It is really the poster child for how Javascript should be used. A few years back, there was a lot of ignorance about the power of Javascript. Mostly, because most of the javascript on the internet was terrible. Now, I think most people have realized that Javascript is one of the most capabable languages. It supports several paradigms: functional, imperative, object-oriented (prototype, not class-based), data literals....

jQuery uses Javascript to its full ability, using each aspect of its design to solve each problem in the most effective way.

I tell all people learning javascript to read the jQuery source over and over until they understand it.... It will be hard, but they will finish by being much better programmers, with a much larger assortment of tools in their toolbox.

It's kind of perpendicular to the Java/.NET brainwashing, which is to give every developer a screwdriver (OOP), and tell them it is the perfect soltion to every problem in programming and life.

Which, it's not. Every problem needs a different tool. OOP is good, but often a bad idea for some problems.

jQuery's mixin-style plugin architecture is really good. Different, but highly effective, fast, and easy to use.

jQuery is #1 for a reason - it's simple to use, and incredibly powerful.

Yahoo UI is a different approach, for a different problem. It's a UI toolkit that has very heavy abstraction from the DOM (vs. jQuery's lightweight approach). You'll find yourself speding a lot of time modifying it if you intend to something outside the norm. (That's the downside of the heavyweight approach).

It's not a framework for developing apps. It's a bunch of GUI widgets.

I've used both together. There's no reason you can't use both jQuery and Yahoo UI on the same page, they're two different tools for different problems.

I suggest including jQuery site/app-wide, then including jQuery UI plugins as needed. If you need heavyweight stuff, add Yahoo UI. But stick to jQuery for your plumbing code...

Learn from jQuery. Understand the power of array programming, callbacks, data literals, treating code as data, and keeping things concise. And that using the right tool for each problem means much shorter, simpler code.

Lilith River
  • 16,204
  • 2
  • 44
  • 76
  • 3
    Your prose has many truisms, but to address the specific question, `$('#accordion').accordion('option', 'active', 2)` is a shining example of how *not* to write a JavaScript api IMO. It's ugly, unnecessary (what's wrong with a simple "method" call, eg `myAccordian.activeTab(2)` ??), and frankly needs to GTFO. *@J-P* has it spot on. – Crescent Fresh Jan 05 '10 at 17:55
1

In my opinion, YUI is weaker in DOM manipulation, but is way ahead in design.

JQuery is designed for those with little or no JavaScript (or general coding) experience. Its very easy to get an application up and running.

YUI is more readable, and any programmer can pick it up and go very quickly because of the wide use of best-practice methodologies.

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77
  • 2
    Designed for those with little or no general coding experience? What? Yes, jQuery is simple and straightforward... But is also a shining example of how to use Javascript *well* and powerfully.... My experience is that YUI requires a lot more study because there is so much more boilerplate code. The phrase "Best-practice methodologies" also annoys... It's usually quoted in lieu of an actual reason, or when trying to pursuade someone to hammer nails with a screwdriver because it will keep the soul cleaner somehow. – Lilith River Jan 04 '10 at 22:29
1

jQuery is a great library for DOM manipulations, and centering it's API around selectors is one of the reasons why it's so popular today. The thing is, jQuery wouldn't be needed if the browsers DOM APIs were more consistent and easier to use. And I agree with Robert Harvey (commented above) that as an abstract layer over the DOM jQuery does a very capable job.

But as I understand, you dislike jQuery plugin system and the jQuery UI, not the core library itself. Personally, I prefer a YUI style API for components and widgets, because at the higher abstraction level DOM elements become less important. I think the reason why jQuery UI authors chose this design is to make API more consistent with their main product, jQuery library. But I don't agree that this was a good decision.

0
  1. jQuery is designed to work with the DOM (i.e. it's a language highly optimised around web pages).

  2. you potentially have to hunt for stuff that jQuery plugins create automagically

    Presumably if it's appropriate, a plugin returns a jQuery object that you can manipulate, unless it's badly written. In which case that's obviously the plugin's fault.

  3. If you're using a method that autogenerates IDs then jQuery may not be for you. However for example I've used jQuery with Google Maps without too much trouble.

  4. If you wanted to add a row to a table automatically, I'm sure there's a plugin out there. If not, it wouldn't take much to write one.

Rob Grant
  • 7,239
  • 4
  • 41
  • 61
  • 1
    The fact is that most of the plugins I've seen do not export any function to perform manipulation as yahoo ui does so strongly. Most of them use this trick of calling the plugin initializer again with string parameters, mocking method calls. this is kind of puzzling to me... – Stefano Borini Jan 04 '10 at 16:55
  • 1
    I think that's possibly an overuse of the plugin architecture, or a symptom of trying to do everything in jQuery. Making it useful for everything would also ruin its elegance. – Rob Grant Jan 04 '10 at 17:04
0

I agree with Computer Linguist. OOP is not a perfect fit for every possible for problem. In case of web development one first has to decide upon the kind of solution that one has to design. Is the solution essentially a web page with interactive widgets embedded at places that enhance the interactivity or is it a full fledged RIA. JQuery,I believe is fit for the first case and it is basically targeted towards ease of handling of the DOM. In case of RIA applications, toolkits working at a higher level of abstraction are prefered because the developer in this case is dealing with widgets and layouts rather than the HTML and css that lies underneath. In such case using Object Oriented Toolkits like YUI, Dojo or ExtJS are more convenient as they bring the desktop application development approach (with its associated advantages) to the web domain.

lorefnon
  • 12,875
  • 6
  • 61
  • 93