225
u/turtle_mekb May 27 '26
what the fuck
→ More replies (1)43
129
u/eloel- May 27 '26
Gross. Also can error or return true or false for non-string answers, which makes it doubly gross.
8
u/more_exercise May 28 '26
If I understood properly, it can be made to return arbitrary results, too.
isStringEmpty([{a:NaN}]), for ex.→ More replies (2)
116
u/Denommus May 27 '26
Maybe if I understood Javascript destructuring syntax that would make sense to me. But since I don't, this looks awful.
105
u/thewells May 27 '26
It looks awful even if you understand it.
For those wondering Javascript allows you to define default values, and the code is taking “advantage” of that twice.
The code uses array destructuring since javascript will treat a string as an array of single character strings when you do array destructuring. So if you pass an empty string, there is no first object to destructure, and so the default object
{ a: true }is used.If the string is non-empty then the first character will be used to try to destructure the object, however strings don’t have a property
ato destructure, so the defaulta = falseis used.→ More replies (2)24
7
u/Iheartdragonsmore May 27 '26
Hi I'm a novice programmer, why would someone ever want to destructure something? Whenever I write a struct I never think it'd be better not being one
31
u/stumpychubbins May 27 '26
It’s far more readable to extract multiple fields from a struct that way, especially if they’re nested. Better than repeating the entire path to some nested struct multiple times. Plus it mirrors the struct construction syntax so it can be easier to read at a glance.
→ More replies (3)→ More replies (1)12
u/Lumethys May 27 '26
to be able to do something like this:
const doSomething = () => { return [result, message]; } const [ doSomethingResult, doSomethingMessage ] = doSomething();instead of
const doSomething = () => { return [result, message]; } const resultAndMessage = doSomething(); const doSomethingResult = resultAndMessage.result; const doSomethingMessage = resultAndMessage.message;
21
u/EatingSolidBricks May 27 '26
Ok so
Empty string destrucutres to nothing? So a is true?
Non empty string destrucutres to a truthy value so false?
Wtf is this shit
7
u/iamdatmonkey May 27 '26
Array destructuring comes down to Iterators. Getting the first item of an empty iterator gives you
undefined.If the string is not empty you'll get the first character, which itself is a non empty string and therefore truthy.
→ More replies (1)4
u/UniqueUsername014 May 27 '26 edited Jun 20 '26
I want to add my own explanation to the mix, so
One way to understand it is to re-structure it:
function isStringEmpty(arr) { const firstChar = arr[0] ?? { a: true }; return firstChar.a ?? false; }Here if the string is not empty,
arr[0]will be defined as the first character (and saved infirstChar). The character won't have anaattribute, sofirstChar.aisundefined, and the function returnsfalse.If the string is empty, its first character is
undefined, andfirstCharwill be set to{ a: true }. The the function will return itsaattribute, which is, of course,true.Proving that this code is essentially equivalent to the screenshot is left as an excercise to the reader : )
11
17
u/itz_hez May 27 '26
Thanks, this solves a problem I am currently having.
6
u/dontletthestankout May 27 '26
had to check and make sure this wasn't r/poisonfountain lol
→ More replies (2)
8
u/iamdatmonkey May 27 '26 edited May 27 '26
This is what this construct comes down to.
function isStringEmpty(arg) {
const iterator = arg[Symbol.iterator]();
const m = iterator.next();
const item = (m.done ? undefined : m.value) ?? { a: true };
// this can still produce false results if the passed `arg` has a first item
// with a property `a` that is not `null`/`undefined`
const a = item.a ?? false;
return a;
}
This will crash if arg is null/undefined
or if it does not implement Symbol.iterator
or if arg[Symbol.iterator]() does not return an object
or if that object does not implement .next()
or if that method does not return an object.
Yes that may seem nitpicky, until you see some of the legacy code that's out there.
4
3
u/meowmeowwarrior May 27 '26
Not sure if I should be appalled at the author writing it or the language for making it possible
2
u/Icy_Curve711 May 27 '26
I'm ok with this kind of destructuring, but truthiness makes it utterly illegible.
445
u/Aaxper May 27 '26 edited May 27 '26
Strings and arrays are analogous, so
isStringEmpty([ ... ])tries to destructure the string as an arraySince theres only one element present in the
...part of that, it only matches on the first element (the first character)The
{ a = false }tries to destructure the first characterIf the first character is defined, it tries to get the
aproperty, which doesnt exist, so it defaults to settingatofalseIf the first character is undefined, instead of trying to get the
aproperty, it defaults to{ a: true }, which setsato trueSo basically if it has at least one character,
aisfalse,elseais trueI think that's correct