bevacqua / js

:art: A JavaScript Quality Guide
https://ponyfoo.com
MIT License
2.92k stars 497 forks source link

[discussing] can ternary operators be used instead of if/else? #27

Open KingMario opened 6 years ago

KingMario commented 6 years ago

In a reused form component for both the record creation and the record modification, is it acceptable to use ternary operators instead of if/else like this?

this.formAction === 'edit' && this.recordValue ?
    this.recordForm.setValue(this.recordValue) :
    this.recordForm.reset();

It's a clear-cut case. With code-formatting of new-line and indent, it's quite readable. And in the foreseeable future, for each condition of the formAction, the business logic is unlikely to be changed.

KingMario commented 6 years ago

What I'm asking is that, does this case prove confusing or not.

And what's more, since it's not an assignment, is it acceptable?

lalan-kumar-alt commented 2 years ago

yes u can

SiyonaL commented 2 years ago

Yes

KingMario commented 2 years ago

@lalan-kumar-alt @SiyonaL Thanks