Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upConsider alternative style for ternary operators #598
Comments
This comment has been minimized.
This comment has been minimized.
|
I don't think that it looks that readable, have you considered the following style? var location = env.development
? 'localhost'
: 'www.api.com' |
This comment has been minimized.
This comment has been minimized.
|
Well proposed style is also more compatible with ternaries as arguments, while currently recommended option is not. |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the suggestion, but I think the current style is clearest. Also, there's not even a way to configure ESLint to allow the style you've suggested, since it's pretty unique (I've never seen it before) |
feross
closed this
Aug 19, 2016
feross
added
the
question
label
Aug 19, 2016
This comment has been minimized.
This comment has been minimized.
|
@feross mind clearing up how following examples should be formatted then ? export const clip =
(min:number, max:number, n:number):number =>
( n < min
? min
: n > max
? max
: n
)Or case with multiple branches: const thingy =
( message.type === "ForFoo"
? ( model.kind === "Foo"
? doSomething(model, message.payload)
: panic(model, message)
)
: message.type === "ForBar"
? ( model.kind === "Bar"
? doOtherThingk(model, message.payload)
: panic(model, message)
)
: panic(model, message)
)Thanks P.S.: Just trying to figure out how to reformat our current code base to standard & run into things that look little odd with standardjs |
This comment has been minimized.
This comment has been minimized.
|
@Gozala first time I'm seeing code like you wrote it. Perhaps this valid standard syntax might be closer to liking: const thingy = (message.type === 'ForFoo')
? (model.kind === 'Foo')
? doSomething(model, message.payload)
: panic(model, message)
: (message.type === 'ForBar')
? (model.kind === 'Bar')
? doOtherThingk(model, message.payload)
: panic(model, message)
: panic(model, message) |
This comment has been minimized.
This comment has been minimized.
|
Multi-ternary expressions. Mmmm. |
Gozala commentedAug 19, 2016
Is there a chance I could persuade you to adopt alternative style for ternary operators:
It has better readability than case where first condition is placed on the same line as assignment, especially if many ternaries are chained.
Note that this is almost accepted by standard it just does not like space after
(:Thanks for taking time to read & consider.