ded / bonzo

library agnostic, extensible DOM utility
Other
1.32k stars 137 forks source link

return nothing rather than throwing if the element doesn't exist #42

Closed coolaj86 closed 12 years ago

coolaj86 commented 12 years ago

Sorry, that really needed some context:

$('.missing_element').val()

I suppose this could be considered a feature, but there are also instances where sometimes an element exists and sometimes it doesn't.

ded commented 12 years ago

there's a general problem for all things operating on this[0] and .val() is just one of them. i'll more than likely create a patch for the entire batch

coolaj86 commented 12 years ago

I take it back! I love this feature.

I guess I was just in a jQuery-drunken mindset when I filed this issue.

This has saved me so many hours of debugging because it's immediately obvious when I've misspelled something, etc.

Please don't fix!

rvagg commented 12 years ago

@ded, what's your opinion on this? I'm used to hard-fail, Prototype-style but am really beginning to like silent-fail and would prefer that methods don't throw just because you haven't selected anything. Certainly makes unit testing a whole lot easier--you don't need to supply whole chunks of DOM just to test small things.

ded commented 12 years ago

the hard fail shouldn't be in the library. i think if you deliberately (or accidentally) selected nothing, we should "do action on nothing" — not break. similar to regular JavaScript iterators:

// oops, i accidentally my array
[].map(function () {
  // never gets run
})

therefore, we really need to go in and check the existence of this[0] before taking action on undefined

rvagg commented 12 years ago

this can be closed now that #48 is in and cleans this whole area up