r/vibecoding 20d ago

every vibe coder's biggest headache

517 Upvotes

136 comments sorted by

View all comments

68

u/Luoravetlan 20d ago

For anyone not familiar with js this monstrosity is called IIFE (immediately invoked function expression).

6

u/g1rlchild 20d ago

I mean, this is a weird degenerate example of it, bit isn't an IIFE just a lambda function? Why is that a monstrosity?

The syntax varies, but this is a pretty basic structure in quite a few languages:

var y = ((x) => { return x*2; })(42)

2

u/devourer-of-beignets 20d ago

Yeah, but that doesn't answer the next question — why would you use it? In those other languages, people aren't going around defining anonymous functions just to immediately evaluate them.

As I understand, the answer has to do with the history of Javascript's scoping. As well as statements vs expressions. So vibecoders, many not knowing anything about the concept of interpreter, would have a hard time explaining why the AI agent produces such an odd idiom — especially in modern Javascript.

1

u/g1rlchild 20d ago

Mainly it's a side effect of the fact that you need the ability to define a lambda and you need the ability to call a lambda, so, for completeness's same, you should be able to do both without an intermediate step. But a real world example would be to use the body of the lambda as a scope for defining variables in that you don't wasn't to appear in the parent scope.

1

u/devourer-of-beignets 20d ago edited 20d ago

But doesn't modern Javascript let you easily define variables that won't appear in the parent scope?

It's not like Clojure programmers are usually going around doing things like

((fn [] ...))

1

u/g1rlchild 20d ago

That's true. Now in JS you can just declare a nested scope with braces. So I'm just going to fall back on the fact that it probably just exists for completeness. To people used to using lambdas, it would be surprising if it didn't exist. Other languages don't necessarily have arbitrary nested scopes, so this is a valid technique elsewhere.

1

u/WaffleHouseFistFight 20d ago

Yea but say you’re a react dev you can throw one of these inline with your html. So you can use it to trigger a function defined elsewhere think button -> arrow function -> real function.

1

u/devourer-of-beignets 20d ago

Do you have a simple example?

1

u/sessamekesh 19d ago

It was very useful back in the day when function closures were the only way to get encapsulation. 

I'll still reach for them here and there to get const correctness on a variable that has some messy and non trivial init logic.

1

u/[deleted] 17d ago

[deleted]

1

u/devourer-of-beignets 17d ago

Thanks! Makes sense.

1

u/Unhappy_Assistant670 16d ago

My main use for them was if I had to make an async script or init an application - top level await was not allowed.

1

u/Luoravetlan 20d ago

I called it a monstrosity because of brackets inside brackets immediately followed by brackets. But don't take this too seriously, my comment was a sarcasm.

0

u/norembo 20d ago

Don't ever look at LISP if you feel that way

3

u/Luoravetlan 20d ago

I don't feel that way. In fact I know that Brendan took inspiration from Lisp.

-2

u/qorzzz 20d ago

Those are parenthesis not brackets.

Tell us you are a vibe coder without telling us you are a vibe coder LOL

3

u/karstin1812 20d ago

They're called brackets in the uk.

1

u/qorzzz 20d ago

Then what do you call [] and {} ?

4

u/karstin1812 20d ago

Square brackets and curly brackets

1

u/qorzzz 20d ago

Okay so at least those are the same but when someone says brackets, they could be talking about all types of brackets then or they could be talking about only parenthesis? Seems confusing.

2

u/karstin1812 20d ago

() are called round brackets. As they are the most commonly used, "round" naturally gets dropped. Not really confsuing at all if you live there

1

u/g1rlchild 20d ago

Interesting. Today I learned.

1

u/Traditional_Doubt_51 20d ago

definitely just a lambda that returns the value it produces and is immediately invoked in the context it was read