DEV Community

Discussion on: Why You Should Drop The 'Else' Keyword When Writing Conditionals

Collapse
 
chrisgreening profile image
Chris Greening

Thanks so much for sharing!

I really like your comment on how unnecessary else blocking introduces nonlinearity into your code. I thought I was writing clean code prior but leveraging better if placement has been a huge improvement for me over the last year or so

On top of limiting my use of else blocks I've also found (mostly) eliminating loops and opting for filter, map, and reduce operations instead has also been a huge improvement - I didn't realize how much of an implicit spaghetti-mess loops can be compared to their more explicit functional counterparts

Small improvements like this build up into some really elegant code - thanks again for sharing! :D

Collapse
 
chinoms profile image
Chinoms

Thank you very much, @chrisgreening. I'm glad you took the time to read my little article and even found something valuable in it.
You mentioned replacing loops with filter and map. Do you have any pointers? I'd be glad to read up.

Collapse
 
chrisgreening profile image
Chris Greening

I'm glad you took the time to write and share :D really valuable insights, keep it up!!

Here is a really great article on mapping instead of looping:

Basically I've found using operations like this:

  • introduces immutability (easier to ensure the state of objects, variables, etc. aren't changing in unexpected ways between operations)
  • reduces variable scope and side effects (and thus less cognitive overhead on the developer/maintainer)
  • explicitly states your intentions (instead of having to read through a for loop and understand what it's trying to accomplish, a map, filter, reduce or similar operation immediately tells you why the iteration is taking place and what the expected output will look like, no guessing games)

All these together sum up to some really beautiful code that chains together cleanly and reduces unexpected behaviors significantly improving readability

Thread Thread
 
chinoms profile image
Chinoms

Thanks a lot. I'll definitely read up.

Thread Thread
 
chrisgreening profile image
Chris Greening

Cheers! Keep up the great work, can't wait to read your next blog posts :~)