loverajoel / jstips

This is about useful JS tips!
http://jstips.co
GNU General Public License v3.0
12.5k stars 803 forks source link

New post on ~~ antipattern #304

Closed gromgit closed 8 years ago

gromgit commented 8 years ago

Antipattern: ~~

TL;DR;

Don't say ~~ when you mean Math.floor()

Username

https://github.com/gromgit

Extra

Inspired by a recent update PR to Tip 49

kurtextrem commented 8 years ago

Heads up, we have a tip about them. Options:

  1. Remove the tip?
  2. Ask the Express team why they use this
  3. Stick with using only when (?) (you want to save bytes?)
gromgit commented 8 years ago

@kurtextrem Whoops, missed Tip 18 somehow. :grimacing:

I dunno, it seems that tip doesn't go far enough in pointing out the really serious problems with using ~~, even if you Know What You're Doing (and to be honest, I'm also pulling some of my own punches). Unlike !!, the behavior of ~~ is not obvious in so many cases that even the most experienced developers are likely to get bit sooner or later.

Would you guys accept:

  1. Enhance this PR with further examples of where ~~ could cause developers to tear their hair out, and position it as a counter to Tip 18 (so "Tip 18a"?)
gromgit commented 8 years ago

Or:

  1. Issue an update PR on Tip 18 with a whole bunch of d'oh examples, possibly doubling or tripling its length?
kurtextrem commented 8 years ago

Revisiting seems good, but we should mark the "update" as new tip so that people read it (practically: add the d'ohs to tip 18 and send out the new part as 18a (which is not there in reality but in 18 included?))

gromgit commented 8 years ago

OK, I'll work on an update PR to Tip 18 then.

sjfkai commented 8 years ago

:+1: Thanks for your work.

loverajoel commented 8 years ago

:clap: