When is code "too clever" / how do you think about readability/cognitive load?

Frederik 👨‍💻➡️🌐 Creemers - Jun 16 '18 - - Dev Community

Usually, it's quite clear to me whether code I'm writing is clean and easy to understand, but lately, I find myself doing the following a lot in JavaScript:

When a function needs to do something only when a certain option is passed in, I do this:

function foo(shouldBar){
    //...

    shouldBar && bar();
}
Enter fullscreen mode Exit fullscreen mode

I find it quite clear, but I'm probably going to try to kick this habit because

if(shouldBar){
    bar();
}
Enter fullscreen mode Exit fullscreen mode
  • is more explicit,
  • is easier to understand or devs with less JavaScript experience.

How do you decide whether a particular line of code is "too clever"?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player