News for hackers. Stuff that matters.
Re:Sugar is a new Javascript library for working with native... by geuis on Wednesday July 20, 2011 @10:06AM (#2785558)
This bit I find concerning, "Sugar is more concerned with convenience and intuitive syntax, whereas Underscore.js is more oriented toward performance".

There are many libraries that enhance aspects of writing js apps that dont sacrifice performance.

Additionally, and I may be wrong here, but in my experience any lib that modifies native objects tends to be a bad idea.

Re:Sugar is a new Javascript library for working with native... by Bowser on Wednesday July 20, 2011 @10:23AM (#2785647)
To be clear, "sacrificing" performance is not a goal (obviously) nor should it need to be. The only point is that the ultimate goal is intuitive syntax, and that performance will always take a second place to that. That doesn't mean that I'm not thinking about performance, and it will be a goal of mine over the coming days to improve weak spots and tighten up performance, as that is a priority too. But I really wanted to nail the syntax first to the greatest extent possible.
Re:Sugar is a new Javascript library for working with native... by geuis on Thursday July 21, 2011 @06:35PM (#2791606)
That's a fair rebuttal. What's your opinion about my second statement, where the common wisdom that modifying native objects is a bad idea?
Re:Sugar is a new Javascript library for working with native... by Bowser on Thursday July 21, 2011 @11:20PM (#2792279)
Honestly, I find it to be more the common superstition rather than the common wisdom. That is not to say there's no truth to it, but I find that I hear ppl repeating it religiously without really knowing why. The main danger comes from extending Object.prototype, which Sugar will never do. In response to a lot of people with the same concerns as you, I've put up a page explaining this in more detail (http://sugarjs.com/native). I think the idea of extending other objects may have gotten a bad rap due to this, but the fact is there is a marked difference, and frameworks like Prototype have known this and found their niche here for many years.

Now of course there are other considerations like collisions with Prototype itself or other libraries. But in the end, any library that modifies these native types is generally speaking meant to exist on it's own, without having to co-exist with other libraries of similar type. Now I realize that can't ever be 100% guaranteed, but if that limits the environments where Sugar can be included, then so be it. Now that having been said I am still going to great lengths whenever possible to minimize these collisions and in more than a few places have actually re-aligned Sugar syntax to match Prototype, so long as it doesn't violate my first rule of having intuitive syntax that "reads". Additionally it's worth restating that Sugar never overwrites methods that are already defined, and so any page that includes Prototype will always get Prototype methods in the case of a collision. I'm currently looking into an elegant way of having devs manually override Prototype methods/classes if they so desire. I also believe that the danger of modifying native objects can be greatly mitigated by extensive unit testing, and this was the main reason I've taken the unit tests as far as I have (to be honest I've gone a bit nuts with them). To summarize, there is a certain extent to which I disagree with the statement that "modifying native objects is bad" and a certain extent to which I agree. To the extent that I agree I think that making good design decisions, mitigating collisions, and having proper unit tests to tie it all together can more than make up for anything that might be considered "bad".

Lastly I want to mention that I just released v0.9.1, which uses Object.defineProperty to map these methods to the prototype. It's a long story (check out MozDev for more info) but the result is that in modern browsers including IE9, modifying the prototype object no longer breaks the for..in loop. I actually had to put a new "warning" into the unit tests that include Prototype because I wanted to put in a unit test assuring this, but Prototype breaks it!

Re:Sugar is a new Javascript library for working with native... by hubriz on Wednesday July 20, 2011 @10:17AM (#2785624)
Writing in JS is a sacrifice of performance in the first place. Besides even the smartest, most performant library in the hands of inept programmer will produce a subpar result. If you're smart, you can write quality, performant code with both _ as well as Sugar, I bet. Besides sugar is a much more diverse set of functions as far as I can tell. I like the idea and are willing to try it out. :)
Re:Sugar is a new Javascript library for working with native... by Bowser on Wednesday July 20, 2011 @10:30AM (#2785684)
Thank you much for your support of the concept. I have a great respect for _ and love what it does. We definitely have the same goal in mind, with different ideas of how to get there. I'm going to try to be humble and learn as much as I can from great code like theirs while at the same time trying to keep some hard lines about Sugar's commitment to intuitive and easy to understand syntax.