I work with pedagogies, teach, write curricula, coach, manage, mentor, consult, speak publicly, polemicize, and sometimes work as a full-stack web developer, architect, ontologist, and more.
Number 3 is just plain wrong. const must be assigned when it is declared, so the example cannot occur in real code. Smart code avoids let as much as possible, and if necessary strives to assign at the same time as it is declared. So an actual example would be more like:
// BADexportconstfoo='some bit of text',bar='some more text',kip=666
This is difficult to read (worse if you try to do it on one line). So much cleaner to write:
// BETTERexportconstfoo='some bit of text'exportconstbar='some more text'exportconstkip=666
At a glance I can see exactly how many variables I've declared and assigned and that each is exported and each is not reassignable.
Shorter or terser is not the same thing as cleaner, more intuitive, or more readable. Sometimes duplication is better.
Number 4 is needlessly verbose and difficult to read. This:
// BADconst{['x']:a,['y']:b}=foo
Should be this:
// BETTERconst{x:a,y:b}=foo
Number 5 is OK, but number 6 is pointlessly mutable. Why? Instead of this:
The variables are positional, so no reason their names have to match the IDs. You can make them better. You need better variable (and ID) names in general. These are terrible examples.
Number 7: instead of this:
// BADfoo&&doSomething()
But the full if statement is more obviously a guard and more scannable. Don't make terseness the enemy of readability.
// BESTif(foo){doSomething()}
Number 8 is OK, but number 9 needs work. Who can read this?
// BADconstSALARY=15e7,TAX=15e6
How many people can do the math in their head? Few, I bet. But use the underscore and both the magnitude and the difference in magnitudes become clear:
Re number 10, assigning the value of one variable to another is generally something to avoid as it's not clear what you're assigning (unless you've named the variables very well). But even if you're assigning, say, a string, it is better to stack them. This:
// BADa=b=c='some text
Is easily missed and takes a moment to parse. And you probably have to keep reminding yourself that a, b, and c have the same value. (Why on Earth would you need 3 variables with identical values -- unless you're mutating them, which... just don't.)
The bonus is half a good idea, and half a bad one. Instead of this:
// BADconst{['log']:c}=consolec("something")
Do this:
// BETTERconstlog=console.loglog('something)
Now you know what it's doing.
I guess posts like this spur discussion, but for the unwary newbie they are strewn with land mines, doing them a disservice. So BEGINNERS BEWARE! Much of what you read online is written by people who haven't really done their homework. Don't let their mistakes be yours.
On looking deeper, I find that roughly half of these are plain wrong. It's not even a matter of opinion.
For number 2, this:
assigns 'apple' to
foo
orbar
depending on the values ofc
andd
. It leaves the other keys ofa
alone.This:
reassigns
a
(which means it is not aconst
), wiping out all previous key-value pairs. It's also pretty unreadable. Both mutatea
.They are not equivalent.
Better would be to use the spread operator:
Number 3 is just plain wrong.
const
must be assigned when it is declared, so the example cannot occur in real code. Smart code avoidslet
as much as possible, and if necessary strives to assign at the same time as it is declared. So an actual example would be more like:This is difficult to read (worse if you try to do it on one line). So much cleaner to write:
At a glance I can see exactly how many variables I've declared and assigned and that each is exported and each is not reassignable.
Shorter or terser is not the same thing as cleaner, more intuitive, or more readable. Sometimes duplication is better.
Number 4 is needlessly verbose and difficult to read. This:
Should be this:
Number 5 is OK, but number 6 is pointlessly mutable. Why? Instead of this:
Do this:
The variables are positional, so no reason their names have to match the IDs. You can make them better. You need better variable (and ID) names in general. These are terrible examples.
Number 7: instead of this:
But the full
if
statement is more obviously a guard and more scannable. Don't make terseness the enemy of readability.Number 8 is OK, but number 9 needs work. Who can read this?
How many people can do the math in their head? Few, I bet. But use the underscore and both the magnitude and the difference in magnitudes become clear:
Re number 10, assigning the value of one variable to another is generally something to avoid as it's not clear what you're assigning (unless you've named the variables very well). But even if you're assigning, say, a string, it is better to stack them. This:
Is easily missed and takes a moment to parse. And you probably have to keep reminding yourself that
a
,b
, andc
have the same value. (Why on Earth would you need 3 variables with identical values -- unless you're mutating them, which... just don't.)Much better:
The bonus is half a good idea, and half a bad one. Instead of this:
Do this:
Now you know what it's doing.
I guess posts like this spur discussion, but for the unwary newbie they are strewn with land mines, doing them a disservice. So BEGINNERS BEWARE! Much of what you read online is written by people who haven't really done their homework. Don't let their mistakes be yours.
That's the first thing I noticed when saw the code. Great explanation.