-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Use destructuring for explanatory variables #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use destructuring for explanatory variables #37
Conversation
Is this syntax not more complicated to understand? |
There a learning curve for the destructuring in my opinion but this avoid to play with array indexes |
const match = cityStateRegex.match(cityStateRegex) | ||
const city = match[1]; | ||
const state = match[2]; | ||
const [, city, state] = cityStateRegex.match(cityStateRegex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find destructuring cleaner as well. For the record, that's my opinion and not Clean Code's.
This is a great fix all in all! Can you fix the regex above so that we don't have to omit the first item in the array? I should have used a better one to begin with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i will also add a string for the example because apply a regex on a regex is a non sense :-)
@SebastienElet Some corrections:
So the code should be this one: Bad: const address = 'One Infinite Loop, Cupertino 95014';
const cityStateRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
console.log(address.match(cityStateRegex)[1], address.match(cityStateRegex)[2]); Good: const address = 'One Infinite Loop, Cupertino 95014';
const cityStateRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
const [, city, state] = address.match(cityStateRegex);
console.log(city, state); |
cfe07d3
to
f446783
Compare
@vsemozhetbyt thanks a lot, this gives me ideas for my next PR :-) |
This has been a crappy lingering problem, thank you tons Sebastien for taking the lead and fixing this! |
This use the ES2015 way.
I removed the match because it's not used and we could also declare a const all to grab the first item of the match if needed.