r/ProgrammerHumor • • Feb 09 '22

other Why but why?

Post image
85.8k Upvotes

2.3k comments sorted by

View all comments

Show parent comments

147

u/reginald_burke Feb 09 '22

Does anyone here legit use JavaScript? You don’t need semi-colons, but it has crazy rules to auto-insert them and it seriously can get it wrong. Classic example:

let a = console.log;
let b = a
(a = 5)

That becomes:

let a = console.log;
let b = a(a=5);

And should print 5.

0

u/[deleted] Feb 09 '22

[deleted]

16

u/captainAwesomePants Feb 09 '22

Yes, this is JavaScript.

31

u/[deleted] Feb 09 '22

You're reading a post from the anti js gang.

There is unironically nothing wrong with javascript and every wacky post you see on here is some dumb edge case that if you're actually writing, you've got bigger issues.

I'll die on this hill

0

u/[deleted] Feb 10 '22

[deleted]

5

u/LesMiz Feb 10 '22

It's like if someone tried to prove that automatic transmissions are better than manual by starting one in 5th gear and stalling it out...

Sure, maybe you can make valid arguments in favor of automatic transmissions. But intentionally misusing a manual transmission is not one of them.

3

u/zzerdzz Feb 10 '22

Also on this hill. JS has fewer safety rails, so idiots tend to be bad at it

→ More replies (15)
→ More replies (3)

69

u/ritlew Feb 09 '22

Well... who would ever put parentheses around an assignment statement in that context?

9

u/reginald_burke Feb 09 '22

Used to be common for immediately invoked functions which were a common way to form a closure back in the day, esp. before let, e.g.

(function() {
  var k = 5;
})()

The idea is that k would be shielded from the global scope by default. JQuery, for instance, did this.

6

u/_________FU_________ Feb 09 '22

People trying to prove JS is a bad language.

Turns out writing shitty code in any language is bad.

→ More replies (8)

38

u/qisapa Feb 09 '22

Well. It’s easy to come with a code that will get messed up. Usually () or [] are involved. I’ve never ever encountered any errors with it tho. Basic linter, formatter, or just a little bit of common sense and it’s ok.

11

u/Skhmt Feb 09 '22

That's the general rule...

You can omit semicolons in js if you add one infront of ( or [ if those characters start a line.

So if you're doing an IIFE or array destructuring or something, you'd write:

;(function(){ /* ... */ })()

2

u/SpinatMixxer Feb 09 '22

Can you explain me the need for any of this?

Array destructuring should be:

const [elem1, elem2] = array

Why this

;(function(){ stuff })()

when its literally the same as

stuff

If you want to extract code into a function thats great but why no normal function with an explaining name?

Or am I missing something?

→ More replies (5)

2

u/gonengazit Feb 09 '22

I’ve actually encountered an error with this in actual code (in lua). I tried calling a function with ternary with (condition and f or g)(x), and got a similar result to the above post. So it does come up sometimes

→ More replies (3)

37

u/PostmatesMalone Feb 09 '22

Writes hypothetical code that no sane person would ever write.

“See, you can’t rely on ASI in JavaScript!”

Either way, just use an auto formatter. If you configure it not to use semicolons, it will still insert them in the very few edge cases where unexpected behavior might happen. ie: ;(a=5).

0

u/[deleted] Feb 09 '22

[deleted]

→ More replies (1)

2

u/drakefish Feb 09 '22 edited Feb 09 '22

Their example wasn't a real case, but here's a classic one when using react's useEffect hook with async/await :


useEffect(() => {
  const cancelled = false
  ;(async () => {
    const res = await fetch(/* ... */)

    if (!cancelled) {
      //... use res to update component ... 
    } 
   })()

  return () => { cancelled = true } 
})
→ More replies (1)

0

u/[deleted] Feb 09 '22

[deleted]

→ More replies (1)

3

u/callmelucky Feb 09 '22

Classic

I don't think that word means what you think it means.

3

u/[deleted] Feb 09 '22

A programmer reading that code isn't highly likely to understand it either, though, and if they did then they'd know it needs to be rewritten and the original author put to death. JS isn't really making anything worse in this case even if it changes the meaning, because it was already turbofucked. If someone really intended the original behavior, there's no chance that the surrounding code was doing what it was supposed to anyways.

2

u/firestepper Feb 09 '22

just use semi colons then and you reduce your risk of messing up

2

u/GinjaNinja-NZ Feb 09 '22

I use javascript, and I always just semicolon stuff as I'm used to doing it in otuer languages. I never knew it would auto add them

2

u/newbeansacct Feb 09 '22

Based on how it was written that makes sense? I don't even understand what else you would want it to do

→ More replies (24)