The connected variable isn’t reused anywhere either so couldn’t you just do this?
Yep, absolutely, in this simple case, i wouldn’t mind either way. But splitting the calculation into its own variable, even though the variable is not reused anywhere else, has some benefits:
Each line is does a one simpler thing: first determine whether we’re connected using a random value, and then set the component state to that.
The code is more debugger-friendly. You can easily tell the value of the connected variable before setting the state by stepping between the two lines.
I’d use a ternary statement for setting the color too, but I know some people don’t like those.
Well, ifs in JavaScript are not expressions, so i’d use ?: too :)
I’d use a ternary statement for setting the color too, but I know some people don’t like those.
Do you know why ternary statements would be frowned upon? I don’t like nested ternaries, but other than that I find them extremely useful and they aren’t really that hard to use…
It’s because they’re often used with poor taste. I use them for one-line assignments all the time, but they’re sometimes used nested or with conditions/values complex enough that they span multiple lines.
I had a CS professor from Bulgaria in college that would always chastise me for using them in C telling me “You’re being clever; be clear, not clever.”
What? It’s difficult to take the article seriously after that. Seems like a purposely obfuscated example for saying:
And it’s not about not being fluent with JS either. This is just basic boolean stuff.
The connected variable isn’t reused anywhere either so couldn’t you just do this?
I’d use a ternary statement for setting the color too, but I know some people don’t like those.
Yep, absolutely, in this simple case, i wouldn’t mind either way. But splitting the calculation into its own variable, even though the variable is not reused anywhere else, has some benefits:
Each line is does a one simpler thing: first determine whether we’re connected using a random value, and then set the component state to that.
The code is more debugger-friendly. You can easily tell the value of the
connectedvariable before setting the state by stepping between the two lines.Well,
ifs in JavaScript are not expressions, so i’d use?:too :)Do you know why ternary statements would be frowned upon? I don’t like nested ternaries, but other than that I find them extremely useful and they aren’t really that hard to use…
It’s because they’re often used with poor taste. I use them for one-line assignments all the time, but they’re sometimes used nested or with conditions/values complex enough that they span multiple lines.
The new do expression in the early parts of the standardization process will make this process much nicer if it makes it.
I had a CS professor from Bulgaria in college that would always chastise me for using them in C telling me “You’re being clever; be clear, not clever.”
To me, any C code is clever, not clear.
You’re just too much of a hacker. Not everybody can write this kind of code.