How to write Flutter code efficiently

When we start with any development we generally tend to write each code manually. That is good no doubt but once we are well versed with that technology, if we still follow that same rule it might…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




An Exercise in Syntactic Pedantry

A ternary is basically a shorthand if-else statement. Consider the following

There’s a bit of repetition and the if-else block can be a bit verbose. As a ternary we could write

With a ternary the if resolution comes after question mark whereas the fallback resolution (else) comes after the colon.

For a single ternary this reads fine. However if we’re trying to handle multiple logical statements in succession we need to chain ternaries together.

The above pattern with progressive indentation is fairly common. One problem I’ve long had with this is that the visual groupings make little relational sense. Look at any one block and see if you can discern any connection

Here 'Loading...' and data are visually grouped together which makes sense insofar that at this stage these are the two paths from the previous conditional. But in nested ternaries all but the last expression following a colon is the conditional for the following block. In the first example 'minor' and 'adult' had equal utility. 'Loading...' and data do not — one is an exit node, the other is a conditional.

The other common variant on nested ternaries removes the indentation altogether. But this for me is even harder to follow and relies on the reader’s ability to mentally toggle between even and odd rows and the role they place in the overall expression.

When you step back and think of the nested ternary expression holistically, it boils down to a series of conditional expressions and their associated values when true. There also needs to be a specified priority hierarchy and a default value should all conditionals fail.

We can do this with the current ternary syntax as follows:

Here the relation between each conditional and the resulting value is immediate and clear. We can add or reorder the conditions with ease while preserving alignment and readability. The default value feels a little bit like a sewn-on appendage, but given its role I think this is preferred.

For a deeper dive into why ternaries are great in the first place I highly recommend Eric Elliott on this (and most everything else).

Add a comment

Related posts:

Next Evolution of Db2u

Db2 Universal Container (Db2u) is a containerized solution that delivers a fast, consistent deployment of Db2 in any Kubernetes platform. The history of containerized Db2 has seen two major…

Free Resources for Developers

Hello everyone! As 2017 is coming to an end, kickstart your new year with these curated free and open source resources. Want to start 2018 off as a developer? Well check out DevFreeBooks! Here you…

17 lecciones de 2017

Contra las reglas es un espacio para reflexionar sobre la práctica diaria de ser humano con la particularidad de haber nacido en Venezuela y tener la convicción de que es posible emprender, crecer y…