• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript style!!

 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1) What are folks thoughts on using the double NOT operator in JavaScript (or in any other languages where it's useful) to convert falsy values to false? ("True" false.) :P
(2) Do you expect all JavaScript programmers to be familiar with this idiom?

For example, variable "a" could likely be true, false, or null. Would something like this,

return !!a;

whose intent is to return either true or false (by "converting" null to false), cause conniptions for you or your team?

(3) Does your answer depend on the likely specific falsy value(s) you're converting from? For example, it's OK for null -> false, but not for other things like empty string, zero, undefined, or NaN?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) No problem.
2) No. But they should.
3) No, depends on context (does it make sense where it is being used)

(P.S. I gave you a cow not only for an interesting topic, but for your clever subject)
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm disappointed, I expected song, dance and music.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a dancing ninja:

 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:(P.S. I gave you a cow not only for an interesting topic, but for your clever subject)



Mooo. (Thanks.)

Bear Bibeault wrote:3) No, depends on context (does it make sense where it is being used)



I've used !! for such purposes twice in the past week, and in both cases I think it makes the code clearer. I just needed to step back for a minute and make sure I'm not in toddler-with-a-hammer mode.

I'll mention my code at our next dev team meeting and will be prepared to fight off charges of "excessive cleverness" or the like.

While I was thinking about this kind of stuff yesterday, I went poking around jQuery source and found the following:



So at first glance I thought this was pretty clever stuff (and possibly an argument in my favor regarding the use of !!): the "getter" function fired always returns true or false, despite the underlying value only ever being undefined or true (in the current code base).

But then I started to wonder, in this case, why not just initialize fired to false when declaring it and avoid the !!. The only code I see testing the value of fired tests it as "!fired" -- which would return the same value (true) whether fired is undefined or false. Is there something going on here I'm not understanding, or should I just chalk it all up to a style preference?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it just comes down to a style pref.

I see JavaScript written by people coming from Java that is stylistically different from those coming from a different background (jQuery committers tend to be from the Ruby rather than Java world).

A Java person might use the initialized variable approach; a non-Java person might not. Neither is wrong, but double-bang is a common idiom that all JavaScript authors should become familiar with.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic