DEV Community

Discussion on: Why do we write JavaScript like this?

Collapse
 
brandinchiu profile image
Brandin Chiu

In this particular case, the if is redundant, as the condition for your if is the same as your return content.

You could simply return everything between the brackets of your if instead of explicitly returning true.

The above is important because the way you've written actually makes your application harder to read because it increases what's called "Cyclomatic Complexity" by introducing a fork in your execution path.

Other than that, "the syntantic sugar" of Javascript (arrow functions and the like) are not something I like either. What you're seeing is the proloferarion of the "Code Golf" fallacy. While a fun thought experiment, it is often a measurably worse way to write software thay needs to be maintained by other people.

Thread Thread
 
anders profile image
Anders

I'm not entirely sure I follow, how would that look?

I do agree in principle though re: complexity. I could have spent some more time on my counter examples for sure.

Thread Thread
 
brandinchiu profile image
Brandin Chiu • Edited

Oh woops!

I've misinterpreted what this is doing; I thought I saw an else statement in your if.

That's definitely my bad -- ignore my comments about Cyclomatic Complexity. They don't apply here.

Thread Thread
 
anders profile image
Anders

Roger that, I thought you had some extra magic in store there and got way intrigued : )