Myself, I would call most of these "less code" examples, not necessarily "clean code". Imo, clean code is legible code. A lot of these examples require the developer more mental fatigue. Some of these I do, but others I definitely avoid, especially #10.
I don't think #10 is all that bad. It's a bit confusing to figure out which variable gets assigned to the others, but that's easily figured out to be the right-most variable. Logically speaking, it does make sense: all those variables end up being equal to each other.
It's a bit confusing to figure out which variable gets assigned to the others
That’s exactly my point. It causes mental fatigue. If you have to stop and think about how something as simple as this is constructed then it reduces the legibility of the code. It’s not that hard to figure out, but it still isn’t absolutely clear and imo it’s unnecessary.
The "incorrect" version is definitely less cognitive load. I also prefer individual const definitions as well for that extra level of legibility when scanning the code.
Myself, I would call most of these "less code" examples, not necessarily "clean code". Imo, clean code is legible code. A lot of these examples require the developer more mental fatigue. Some of these I do, but others I definitely avoid, especially #10.
Agreed with you, Code Readability should trump trying to shave off lines of code.
Thank you. I should probably work on my article title naming 😅
Considering all the feedback, I do believe this article should be renamed to avoid misleading to learners, seriously.
Nah..😄. I think the feedback will help people understand better what clean code is and isn't.
I don't think #10 is all that bad. It's a bit confusing to figure out which variable gets assigned to the others, but that's easily figured out to be the right-most variable. Logically speaking, it does make sense: all those variables end up being equal to each other.
That’s exactly my point. It causes mental fatigue. If you have to stop and think about how something as simple as this is constructed then it reduces the legibility of the code. It’s not that hard to figure out, but it still isn’t absolutely clear and imo it’s unnecessary.
10 could be really bad in a language like c++ with operator overloading. But, yeah, the "incorrect" version is much clearer, regardless.
what about
const [a, b, c] = [d, d, d];
?☝💯
A bunch of these seem more clever than clean.
I consider the #1 priority of the code to easily communicate what is happening.
consider these two examples
❌
const a = foo.x, b = foo.y;
✔️
const { ['x']: a, ['y']: b } = foo;
I don't know if I'm "over-indexing" on my experience here, but I'd say the "incorrect" example is easier to comprehend.
The "incorrect" version is definitely less cognitive load. I also prefer individual
const
definitions as well for that extra level of legibility when scanning the code.#4 could also be written much simpler like this:
const { x: a, y: b } = foo;
But there is no b. keys are different and value is same (here a).
const { ['x']: a, ['y']: a } = foo;
will not be possibleIt's re-assigning the value.
['y']: b
assigns the value ofy
tob
so you can now access it asb
.