DEV Community

Discussion on: short-circuits && clean code

Collapse
 
craser profile image
Chris Raser

I'm with Evan Oman on this one: kill that sample snippet with fire.

And I suspect you already have an opinion of your own, or this idiom wouldn't have prompted you to post the question in the first place. ;)

But I think there's something going on here that's more interesting than arguing about which syntax is "best practice."

When writing your code, think about who will be reading it. Do you work on a team that's growing, or are you trying to attract contributors to your open source project? Then adopting highly idiomatic code will be counterproductive. It makes it harder for newcomers to get up to speed.

There's an old Brian Kernighan quote about debugging:

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

I think the same principle applies to readability. The hard part of coding isn't the syntax, and it isn't reading individual lines or expressions. So being "clever" in how you write individual expressions doesn't achieve very much. It makes the code harder to read, and does nothing for the overall structure of the project.