js icon indicating copy to clipboard operation
js copied to clipboard

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

Open KingMario opened this issue 8 years ago • 4 comments

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 avatar Oct 21 '17 00:10 KingMario

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?

KingMario avatar Oct 21 '17 00:10 KingMario

yes u can

lalan-kumar-alt avatar Mar 12 '22 12:03 lalan-kumar-alt

Yes

SiyonaL avatar Sep 19 '22 07:09 SiyonaL

@lalan-kumar-alt @SiyonaL Thanks

KingMario avatar Sep 29 '22 01:09 KingMario