TIL about Code Golf
POSTED ON:
TAGS: coding abstraction
My blood level rises every time I see code that tries to reduce character count.
// 31 characters 😞
Math.floor(Math.random() * 100)
// 21 characters 🙂
~~(Math.random()*100)
// 19 characters 😄
Math.random()*100|0
This is known as code golf.
Here's what the problems are:
- The code is less readable.
- This adds more room for typos/errors/bugs.
- Who is this optimizing for?
Written code is for humans, compiled code is for computers.
Maybe there are shorter ways to write a block of code. But it's up to the developer team to make that decision.
My rule of thumb? If you can't understand the code the next day, re-write it. Your future self will thank you.
Code via https://understandlegacycode.com/blog/refactoring-rule-of-three/
Related TILs
Tagged: coding