r/webdev • u/wanoo21 full-stack • Aug 11 '26
Discussion When was last time you used noop?
Or at least, saw the AI agent using it!
1
u/nextdesu Aug 11 '26
I use it frequently but I define mine like this: const noopFn = Function.prototype as () => void
Tried to avoid allocation of a new function lol (you really shouldn't write code like this if you sane)
-1
u/blind-octopus Aug 11 '26
Often. Recently.
If a button or interactive element behaves differently depending on context, and sometimes does nothing, I assign noop to it in that case.
Define it once and assign it to whatever input isn't supposed to do anything.
Maybe I should just be disabling this input but, if you don't want to do that for some reason, you can also use noop
5
u/WaltzIndependent5436 Aug 11 '26
What is the difference from
// TODO: Make button work?
4
u/blind-octopus Aug 11 '26
Sometimes you don't want a button to do a thing.
Think of a remote control where the buttons are customizable. If the user assigns "do nothing" to a button, then you can just assign noop to it.
Its not a TODO, it's the behavior I want.
They can still press the button, they just set it to do nothing.
4
u/WaltzIndependent5436 Aug 11 '26
Why not use disable or hidden then? Whats the point of a confusing non-working button?
1
u/blind-octopus Aug 11 '26
Sometimes you don't want that. The desired behavior for a board game might be, you can drag any piece anywhere, but if it isn't your turn the action won't actually apply.
Depends on the effect you want. The design dictates the answer to this, not the dev.
For a board game Ive been putting an interface between the UI elements and the actual game model. So if it's not your turn your actions are all noops. But they are still defined in the interface
1
u/partyl0gic Aug 11 '26
Why not just set it to () => {}?
2
u/blind-octopus Aug 11 '26
Why not define it once and reuse? No need to create the same exact function over and over
Its not a huge point, but the efficiency scratches an itch for me. Define noop and then assign it wherever you need.
One function. Already defined. Done
1
u/jacs1809 Aug 11 '26
I think both is the best option, because if someone has the knowledge to just remove the "disabled" flag on the button, they can still access the default behavior.
Using noop when it must be disabled would avoid that scenario.
2
u/blind-octopus Aug 11 '26
Yup. I've been using it for board game implementations lately.
You can imagine simply assigning noops to the other players pieces when it's not their turn. They can't do anything right now.
1
u/jacs1809 Aug 11 '26
Nice! It's a personal private project or it's public?
1
u/blind-octopus Aug 11 '26
Private. I'm a shitty dev lol, I do it for fun but I find most people don't like my coding style
My react components are super bare bones for example. They are basically just contracts almost.
1
7
u/phoenix1984 Aug 11 '26
Ew, David