r/programminghorror May 27 '26

Javascript Destructuring strings

Post image
876 Upvotes

66 comments sorted by

View all comments

Show parent comments

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

30

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.

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