This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking for postive length of jQuery wrapped set before calling methods on set

 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've come across some jQuery code that checks the length of the wrapped set before performing certain "getting rid of" methods, such as hide() or remove(). Presumably, the coder's intent is only to call hide() or remove() if there's actually something matched to hide or remove.



Elsewhere in the same code (presumably written by the same person, or team), there's no length check before other operations:



Are such length checks necessary? If so, why?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessary. If there's nothing in the set, the methods will be a noop.

Building the wrapped set twice is also an unnecessary performance hit.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Not necessary. If there's nothing in the set, the methods will be a noop.


That's what I was thinking. I wondered if it stemmed from a fear of calling a method on a null, but a set is returned, albeit empty.

Bear Bibeault wrote:Building the wrapped set twice is also an unnecessary performance hit.


Yes! In this example, I'm guessing the set is not a very expensive one to build, but in the general case, why would you do it twice.

Thanks for the confirmation.

Mike "off to fight crime" Matola
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Matola wrote:Mike "off to fight crime" Matola

What color is your cape?
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Invisible.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, The Stealth Superhero.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic