Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

#1 I agree, making stuff optional was a bad design decision. It encourages people to write code in different ways and makes for unreadable messes if coworkers aren't religious about how they format things or they do things differently than you do.

#2 has definitely bitten me, but I see that less of a failure of CS and more of a failure of the design of $.each(). The case is pretty well documented in CS though... the last code line is 'return'ed... if your code unexpectedly returns false, then really you are just bitten by the design of $.each().



Your only other options for early termination of $.each are pretty cumbersome. On the one hand you can bend over backwards to pass in the "should continue" flag "by reference". In C you might pass in bool * should_continue, then do *should_continue = false. In JavaScript you'd have to wrap that in another variable, like flags.should_continue = false, which is less elegant in my opinion. Alternatively, I've seen libraries abuse throw to achieve this, which I also don't like (and I believe has pretty bad performance characteristics, but I can't remember for sure).


... or you can write it in CS with a for loop which cleans up the syntax quite a bit... or it might be possible to reduce your input first so that you are only iterating over the things you actually want to iterate over...


"...or you can write it in CS with a for loop which cleans up the syntax quite a bit"

For each loops are great, but there is certainly value in having $.each be a function as well as a statement. For example, if you want to pass it to another function in more functional style programming: do_something($.each);

"or it might be possible to reduce your input first so that you are only iterating over the things you actually want to iterate over..."

It seems you don't understand the point of returning false from $.each, it is not to filter, it is to stop early. For example, if you are doing a linear search for something, it is reasonable to want to stop after you find it. There is no way to "iterate over the things you actually want to iterate over".


I think you're stretching here. Passing $.each to a function makes no sense when it is effectively a global anyway. Please give me a real world example of why you would do something like that (not in the functional aspect, that I understand... I am talking specifically about $.each).

I also wouldn't use something called $.each to do a linear search. I'd use underscore.js _.find(), which has a sane api, you return true when you want to stop. This thing I'm looking for has been found.

Array.forEach and _.each() chose not to support returning false. I can only assume that is @jashkenas being wise.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: