Fluent If-Else statements?

Some showerthoughts for the day:

I think it would have been nice if languages support fluent conditional statements.

So instead of

if(condition) {
    someFunction();
} else if (anotherCondition) {
    anotherFunction()
} else {
    doSomething()
}

Enter fullscreen mode Exit fullscreen mode

Maybe have something like

if(condition, someFunction)
    .elseIf(anotherCondition, anotherFunction)
    .else(doSomething)

Enter fullscreen mode Exit fullscreen mode

or go further and make it return a value

var someVar = if(condition, someFunction)
                    .elseIf(anotherCondition, anotherFunction)
                    .else(defaultValue)

Enter fullscreen mode Exit fullscreen mode

Pros:

  • you get a more succinct and arguably more expressive statement
  • if used properly, it may read like a prose

Cons:

  • it’s probably unnatural or unintuitive to some people; more cognitive load perhaps?

I’m not actually sure if this already exists elsewhere and I’m too lazy to find out if it does.
But what do you guys think? Is this useful or am I just overthinking it?

原文链接:Fluent If-Else statements?

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容