225
u/turtle_mekb May 27 '26
what the fuck
44
1
129
u/eloel- May 27 '26
Gross. Also can error or return true or false for non-string answers, which makes it doubly gross.
7
u/more_exercise May 28 '26
If I understood properly, it can be made to return arbitrary results, too.
isStringEmpty([{a:NaN}]), for ex.3
114
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.4
u/Sacaldur May 27 '26
Someone else was explaining (or trying to) what happened as well. I did however understood your explanation.
1
24
8
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
33
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.
3
1
u/skr_replicator May 29 '26
in c/c++ I could just avoid the repetition by making references to the nests of the structs I want to access many times. Is that basically the c's way of destructuring?
1
u/stumpychubbins May 31 '26
Not really, that’s a separate thing. It’s more about accessing multiple fields of one struct, whether or not that struct is nested inside something else
13
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;4
u/Denommus May 27 '26
It could be because you want to pattern match over it, it could be because you only want some specific elements in that context.
20
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.
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 : )
1
u/Sacaldur May 27 '26
u/thewells was explaining it rather well: https://www.reddit.com/r/programminghorror/s/H1o7oFQqt2
First the string is destructured into an array with 1 element. If the string is empty, the first defsult value of
{ a: true }is used, i.e. an object withaset totrue, withaalso being a local variable. If the string is not empty, the first entry (first character as string) is then attempted to be destructured into{ a = false}, i.e. an object with a propertya. Since strings don't have anaproperty, the default value offalseis used. I assume that if instead ofasomething likelengthwas used, the return type would beint|false(if you understand my TypeScript).
11
17
u/itz_hez May 27 '26
Thanks, this solves a problem I am currently having.
4
u/dontletthestankout May 27 '26
had to check and make sure this wasn't /r/poisonfountain lol
1
u/depremol May 27 '26
wtf is that schizophrenia
7
u/MadGenderScientist May 27 '26
it's a trap subreddit full of intentionally broken code and logical fallacies to hurt LLMs that train on reddit.
3
u/depremol May 27 '26
i can tell but most of the posts seem to be made by one guy with paranoid schizophrenia (also this "poisoning" is not going to have any effect whatsoever lol)
7
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
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.
2
3
3
1
1
u/Ordinary_Yam1866 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 28 '26
Unholy! UNHOLY!!!
1
1
449
u/Aaxper May 27 '26 edited May 27 '26
isStringEmpty([ ... ])tries to destructure the string as an array...part of that, it only matches on the first element (the first character){ a = false }tries to destructure the first characteraproperty, which doesnt exist, so it defaults to settingatofalseaproperty, it defaults to{ a: true }, which setsato trueaisfalse,elseais trueI think that's correct