I would prefer using ternary, and no optional syntax characters such as parenthesis or curly braces:
let multipliedByTwo = arr.map(el => el % 2 === 0 ? el * 2 : el + 1)
It may seem less approachable for someone who is not used to ternay, arrow functions and the absence of unnecessary syntax characters, but when used to it, it's actually better to have such a one line operation rather than 8 line of basic if...else.
In other words, I prefer to write a code that requires the maintainer to raise his level of JavaScript understanding but enables code to be shorter, rather than making super simple but verbose JavaScript (I hate to search for bits of code in hundred-lines-long files).
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
Developers in general agree with you. Most one liners beyond a standard if/else ternary are caught by default linting configs. If someone has to translate the code you write, it's meant for a machine. Given that this already happens once it hits an engine, it is generally not wise to write code in this manner.
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
I think it's important not to simplify the code at a point where you avoid using functionalities which are useful but less readable for a newbie.
For example instead of the following:
I would prefer using ternary, and no optional syntax characters such as parenthesis or curly braces:
It may seem less approachable for someone who is not used to ternay, arrow functions and the absence of unnecessary syntax characters, but when used to it, it's actually better to have such a one line operation rather than 8 line of basic if...else.
In other words, I prefer to write a code that requires the maintainer to raise his level of JavaScript understanding but enables code to be shorter, rather than making super simple but verbose JavaScript (I hate to search for bits of code in hundred-lines-long files).
Interesting. I tend to think of one liners as less readable in most cases. But it’s an interesting perspective!
Developers in general agree with you. Most one liners beyond a standard if/else ternary are caught by default linting configs. If someone has to translate the code you write, it's meant for a machine. Given that this already happens once it hits an engine, it is generally not wise to write code in this manner.
“Code is for humans to read and machines to interpret”! Still don’t know who said it first, but it’s a great quote.
Yeah - I wouldn't want to have to figure out what goes wrong in the middle of the night when they call with production issues.
And it is not unnecessary syntax characters except for the computer - for humans, it adds readability and understanding.
And your stuff will break down when the new guy is covering everything while you are on vacation.