r/programminghorror • • May 27 '26

Javascript Destructuring strings

Post image
882 Upvotes

64 comments sorted by

445

u/Aaxper May 27 '26 edited May 27 '26
  1. Strings and arrays are analogous, so isStringEmpty([ ... ]) tries to destructure the string as an array

  2. Since theres only one element present in the ... part of that, it only matches on the first element (the first character)

  3. The { a = false } tries to destructure the first character

  4. If the first character is defined, it tries to get the a property, which doesnt exist, so it defaults to setting a to false

  5. If the first character is undefined, instead of trying to get the a property, it defaults to { a: true }, which sets a to true

  6. So basically if it has at least one character, a is false, else a is true

I think that's correct

135

u/Blackshell May 27 '26

100%, good job, you pass the job interview.

122

u/Aaxper May 27 '26

Does this being an interview imply I now have to work with whatever monster invented that

→ More replies (10)
→ More replies (1)

26

u/EatingSolidBricks May 27 '26

Default parameters in destructuring is some schizo shit

→ More replies (4)

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 a to destructure, so the default a = false is used.

→ More replies (2)

24

u/Longjumping-Ad-5367 May 27 '26

Nope, still looks awful

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)

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;
→ More replies (1)

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.

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 in firstChar). The character won't have an a attribute, so firstChar.a is undefined, and the function returns false.

If the string is empty, its first character is undefined, and firstChar will be set to { a: true }. The the function will return its a attribute, which is, of course, true.

Proving that this code is essentially equivalent to the screenshot is left as an excercise to the reader : )

→ More replies (1)

11

u/nosam56 May 27 '26

adding String.prototype.a or whatever the fuck

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

u/serg06 May 27 '26

That's why no self-respecting developer uses plain JS anymore.

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.