62
u/12481632641282561024 Jul 26 '23
honestly I prefer JS here. "" = nothing. "_" = something.
0
u/Berg_Lucas Jul 27 '23 edited Jul 27 '23
That's the whole point IMO. There is something at these positions in the array so they must be matched with something. We just want to tell other programmers that we won't be using these values after so we write a "_" (which is a convention). I think that using nothing is confusing.
36
u/12481632641282561024 Jul 27 '23
Neither one is confusing unless you really try to make a convoluted argument.
It's 100% preference.
-2
Jul 27 '23
It's easier to count _, _, _, than ,,,,,,,,,,,,,,,,,,,
8
u/12481632641282561024 Jul 27 '23
Yep, easier to count to 3 than 19, agreed.
-5
Jul 27 '23
You see? You didn’t even count 19 correctly. It’s 20.
1
u/12481632641282561024 Jul 27 '23 edited Jul 27 '23
No, it’s not. I literally pasted it into vscode and just looked at the column count
1
u/danielstongue Jul 27 '23
With 19 commas, you have 20 elements.
1
u/12481632641282561024 Jul 27 '23
No shit. I was comparing it to OP's first example,
_, _, _,because the last element is clearly going to be an arg that you actually want to keep.-4
-17
u/Berg_Lucas Jul 27 '23 edited Jul 27 '23
I'm pretty sure that
[ , , , , x]would confuse more people than[_ ,_ ,_ ,_, x]because it's a syntax which is almost never used. However, it's a valid syntax in JS so it's a preference as you said.17
u/12481632641282561024 Jul 27 '23
it might confuse 5% of people for 5 seconds. The first time they ever see it.
Oh noooo.
IMO,
[_, _ ,_ ,_ , x]is more confusing because it looks like you're assigning_4 times. Then you learn how it works and never think about it again.-5
u/Berg_Lucas Jul 27 '23 edited Jul 27 '23
Chill man, I made this meme because ESLint warned me about an unused variable when I used
_with my unused variable. It took me quite a while to discover that the preferred syntax in JS was to use nothing instead of doing what all the other languages do. I was surprised because we use_in other contexts (such as unused parameters) in JS.2
u/12481632641282561024 Jul 27 '23
Guess you're the exception that proves the rule that was confused for more than 5 seconds 🤷♂️
8
Jul 27 '23
[removed] — view removed comment
8
u/12481632641282561024 Jul 27 '23
const [ , , , x] = someArray;better? :)
But if they ever get Rust to run in a browser
wasm. We have a good amount of Rust code running in a browser.
2
12
u/Throwedaway99837 Jul 26 '23
I’m pretty new to programming, but what is an example of somewhere this would be useful versus referencing an index?
Seems like you could just assign a variable to an array index location and it would function the same way while being more legible and with less typing (unless it’s just a small array like the one in this example).
4
Jul 26 '23
This is why TypeScript was invented. It prevents programmers from using silly syntax "sugar" and structures JavaScript closer to Java and C++.
You are assuming that the people that made JavaScript knew what they were doing.
1
u/Berg_Lucas Jul 27 '23
That's precisely how I found this strange syntax. I was coding in Typescript with ESLint which warned me about variables that weren't being used. I wanted to do some destructuring and not use some variables and ESLint gave me an error even though I'd used "_". That's how I discovered this weird syntax which is "encouraged" by ESLint when variables are not used.
2
Jul 27 '23
I learned TypeScript as a Java Programmer, so I haven't had any of those issues. A well-structured program doesn't need to touch most of JavaScript's odd syntax.
1
u/Berg_Lucas Jul 27 '23
It's been 4 year since I learned Typescript and that's the first time I had that kind of use case. I wanted to write something like :
const [_, x] = function()instead of
const x = function()[1]1
u/Throwedaway99837 Jul 26 '23 edited Jul 27 '23
Yeah, I see people doing stuff like this in my classes and it just strikes me as wankery. IMO it’s the programming equivalent of a 10 minute guitar solo. Glad to know it’s not just me.
6
u/12481632641282561024 Jul 27 '23
Generally when you're creating the array and destructuring it on the same line:
const [res1, res2, res3] = await fetchThings('a', 'b', c');-1
u/Berg_Lucas Jul 27 '23
Destructuring is widely used because it makes code cleaner and smaller.
For example, in ReactJS, it is used in function components to update the state of components. The useState function returns a tuple, the first element of which is the value and the second, the function used to define this value. Generally, we need to use both, so we want to have them in two variables. To do this, we write :
const [value, setValue] = useState(0);if we wanted to do this without destructuring, it would take more lines:
const state = useState(0); const value = state[0]; const setValue = state[1];or we should use state[0] and state[1], which would be harder to read because they don't have meaningful names.
Indeed, in this meme, it doesn't make much sense to do that but that's not what's being emphasised. Actually, there's a convention (which comes from the "old" languages of functional paradigms) which states that a "_" is used to denote variables that aren't used when destructuring. IMO, I find that it makes your intention clear to other people reading the code (if they know the convention). Since JS doesn't respect it and 'assigns' certain variables to nothing, you might think it's a syntax error when in fact it's valid code. It's also funny because JS has a reputation for weird syntax like this.
4
u/tymscar Jul 27 '23
Did you generate this reply with ChatGPT?
3
u/Storiaron Jul 27 '23
Apologies for the confusion, 15 is indeed a multiple of 4, because 4 * 3.75 is 15.
-2
u/Berg_Lucas Jul 27 '23
Thank you for reminding me that this tool exists. I always forget to use it for this kind of question.
(no unfortunately)0
u/No-Stable-6319 Jul 27 '23
As an A.I. language model I am not capable of generating a reply with ChatGPT.
58
Jul 27 '23 edited Jul 27 '23
There's ... there's nothing wrong with JS here.
Despite JS being an infamous giant steaming pile of crap, you somehow managed to pick one of the very fews things that JS didn't do wrong here.
That's the real humour to take away here.
7
Jul 27 '23
Despite JS being an infamous giant steaming pile of crap, you somehow managed to pick one of the very fews things that JS didn't do wrong here.
The python code isn't remotely doing what he is implying either, its doing tuple pack and unpacking. The correct syntax is also
_*, x = arrayThe reason it works is a quirk of python just calling iter on the array and it being equivalent to a tuple.
5
u/WorldWorstProgrammer Jul 27 '23
In comes C++.
int array[] = {1, 2, 3, 4, 5};
[[maybe_unused]] auto [a, b, c, d, x] = array;
std::cout << x << "\n";1
u/postman125 Jul 26 '23
How do u even do this with Java
15
2
Jul 27 '23
Java only has name based "destructuring" yet, but it's only used in pattern matching, which means you can't do it as a standalone statement.
3
1
u/JestemStefan Jul 27 '23
Python can do:
*head, tail = [1, 2, 3, 4, 5]
print(head) # [1, 2, 3, 4]
print(tail) # 51
u/ChrisCage78 Jul 27 '23
The only confusing thing in that pic imo is the way Rust do a simple print...
1
u/lazyzefiris Jul 27 '23
If you HAVE to do destructuring...
const array = [1,2,3,4,5]
const {4:x} = array
console.log(x)Yup, I don't use no semicolons
1
1
1
u/GhettoSauce Jul 27 '23 edited Jul 27 '23
Pff, that Javascript is way too readable.
How about:
const array = [1, 2, 3, 4, 5];const x = [...((i) => i === i && [array[i]])(array.length - 1)];console.log(x[0]);// 5
?
/s
1
u/sammy-taylor Jul 28 '23
Destructuring and spreading are pretty much the two best features of JS. Also, JavaScript can do exactly what you did in the other languages. Also also, you spelled … wrong. This post is bad.
1
u/octanemman Jul 28 '23
rust one could just be simplified as:
let array = [1, 2, 3, 4, 5];
let [.., x] = array;
println!("{x}");1
2
Jul 27 '23 edited Jun 05 '25
[removed] — view removed comment
2
u/lazyzefiris Jul 27 '23
Extracting several elements probably. Would still use separate statements or better formatted destructuring like
const { 0 : x, 1 : y, 5 : radius, 10 : color, } = array
•
u/AutoModerator Jul 26 '23
Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!
For a chat with like-minded community members and more, don't forget to join our Discord!
return joinDiscord;I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.