Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error or undefined result from bad query #31

Closed
beatgammit opened this issue May 27, 2011 · 9 comments
Closed

Error or undefined result from bad query #31

beatgammit opened this issue May 27, 2011 · 9 comments

Comments

@beatgammit
Copy link

When selecting something that doesn't exist, why do I get back an object? Shouldn't I get back undefined or see an error?

For example, say I have an element:

<div id='awesome'></div>

I'll select it, but I accidentally mistype it:

var myDiv = qwery('#awesom');

It is really hard to debug this (especially in large applications), and this is one of the reasons why I switched from jQuery.

Errors are nice because they show line numbers and originating files. I think returning undefined in this case makes the most sense.

@ded
Copy link
Owner

ded commented May 27, 2011

it's best to keep the return types consistent. it would break a lot of applications if we returned undefined eg:

var results = qwery('#not-found')
for (var i = 0; i < results.length; i++) { // cannot access 'length' of undefined
  // do stuff
}

It's not say that null or undefined is a bad idea — but you're much more likely to cause a whole other slew of problems.

As it stands, you can always expect to receive an array. if you're concerned if an element exists, you could check the length property

@beatgammit
Copy link
Author

Maybe I'm just used to the MooTools way ($ vs $$), but I should know whether I expect a single item or several items. Selectors that search for ids should always return 1 or none, selectors that search for classes, attributes, element names, etc should always return an array.

For example:

document.getElementById('not-found') === null;

but

document.getElementsByClassName('not-found') === [];

That's what makes sense to me. When i moved from jQuery to MooTools, everything made more sense, and now it's feeling like I'm moving back to jQuery again.

I suppose I could make my own plugin, but qwery is such a good selector engine. It would be a shame to reinvent the wheel.

@ded
Copy link
Owner

ded commented May 27, 2011

hmm...

@beatgammit
Copy link
Author

Unless, of course, the goal was to be like jQuery, in which case it's doing a really good job. I'm just not a big jQuery fan, as you might have guessed.

@ded
Copy link
Owner

ded commented May 28, 2011

it has less to do with "being like jQuery" — but more a general good programming practice whereby you should always expect the same return type from the same method. If you just wanted something by id, and not an array but the element, you would use some other method. In Mootools, that is $ — the qwery $ is like Mootools $$: Thus, even doing this in Mootools

$$('#hello'); // [<div id="hello"/>]
$$('#not-found'); // []

@ded ded closed this as completed May 28, 2011
@ded
Copy link
Owner

ded commented May 28, 2011

btw, you could always implement a top level method:

$.ender({
  id: function (s) {
    return document.getElementById(s) || null
  }
})

$.id('hello'); // <div id="hello"/>
$.id('not-found'); // null

@beatgammit
Copy link
Author

I guess that makes sense. I might just have to do that. I hear there are MooTools plugins for Ender and that might be a better fit for me. I'm just not a big fan of this style of coding: $('#name')[0].

@ded
Copy link
Owner

ded commented Jun 1, 2011

btw, $.id() is available in Bonzo

@beatgammit
Copy link
Author

Cool, didn't know that. I guess that solves my question. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants