text
stringlengths
1
372
the overall approach of specializing APIs to meet the developer’s
expectations is applied to more than just child models.
some rather trivial widgets exist specifically so that developers
will find them when looking for a solution to a problem. adding a
space to a row or column is easily done once one knows how, using
the expanded widget and a zero-sized SizedBox child, but discovering
that pattern is unnecessary because searching for space
uncovers the spacer widget, which uses expanded and SizedBox directly
to achieve the effect.
similarly, hiding a widget subtree is easily done by not including the
widget subtree in the build at all. however, developers typically expect
there to be a widget to do this, and so the visibility widget exists
to wrap this pattern in a trivial reusable widget.
<topic_end>
<topic_start>
explicit arguments
UI frameworks tend to have many properties, such that a developer is
rarely able to remember the semantic meaning of each constructor
argument of each class. as flutter uses the reactive paradigm,
it is common for build methods in flutter to have many calls to
constructors. by leveraging dart’s support for named arguments,
flutter’s API is able to keep such build methods clear and understandable.
this pattern is extended to any method with multiple arguments,
and in particular is extended to any boolean argument, so that isolated
true or false literals in method calls are always self-documenting.
furthermore, to avoid confusion commonly caused by double negatives
in APIs, boolean arguments and properties are always named in the
positive form (for example, enabled: true rather than disabled: false).
<topic_end>
<topic_start>
paving over pitfalls
a technique used in a number of places in the flutter framework is to
define the API such that error conditions don’t exist. this removes
entire classes of errors from consideration.
for example, interpolation functions allow one or both ends of the
interpolation to be null, instead of defining that as an error case:
interpolating between two null values is always null, and interpolating
from a null value or to a null value is the equivalent of interpolating
to the zero analog for the given type. this means that developers
who accidentally pass null to an interpolation function will not hit
an error case, but will instead get a reasonable result.
a more subtle example is in the flex layout algorithm. the concept of
this layout is that the space given to the flex render object is
divided among its children, so the size of the flex should be the
entirety of the available space. in the original design, providing
infinite space would fail: it would imply that the flex should be
infinitely sized, a useless layout configuration. instead, the API
was adjusted so that when infinite space is allocated to the flex
render object, the render object sizes itself to fit the desired
size of the children, reducing the possible number of error cases.
the approach is also used to avoid having constructors that allow
inconsistent data to be created. for instance, the PointerDownEvent
constructor does not allow the down property of PointerEvent to
be set to false (a situation that would be self-contradictory);
instead, the constructor does not have a parameter for the down
field and always sets it to true.
in general, the approach is to define valid interpretations for all
values in the input domain. the simplest example is the color constructor.
instead of taking four integers, one for red, one for green,
one for blue, and one for alpha, each of which could be out of range,
the default constructor takes a single integer value, and defines
the meaning of each bit (for example, the bottom eight bits define the
red component), so that any input value is a valid color value.
a more elaborate example is the paintImage() function. this function
takes eleven arguments, some with quite wide input domains, but they
have been carefully designed to be mostly orthogonal to each other,
such that there are very few invalid combinations.
<topic_end>
<topic_start>
reporting error cases aggressively
not all error conditions can be designed out. for those that remain,
in debug builds, flutter generally attempts to catch the errors very
early and immediately reports them. asserts are widely used.
constructor arguments are sanity checked in detail. lifecycles are
monitored and when inconsistencies are detected they immediately
cause an exception to be thrown.
in some cases, this is taken to extremes: for example, when running
unit tests, regardless of what else the test is doing, every RenderBox
subclass that is laid out aggressively inspects whether its intrinsic
sizing methods fulfill the intrinsic sizing contract. this helps catch
errors in APIs that might otherwise not be exercised.
when exceptions are thrown, they include as much information as
is available. some of flutter’s error messages proactively probe the
associated stack trace to determine the most likely location of the
actual bug. others walk the relevant trees to determine the source
of bad data. the most common errors include detailed instructions
including in some cases sample code for avoiding the error, or links
to further documentation.
<topic_end>
<topic_start>
reactive paradigm
mutable tree-based APIs suffer from a dichotomous access pattern:
creating the tree’s original state typically uses a very different
set of operations than subsequent updates. flutter’s rendering layer
uses this paradigm, as it is an effective way to maintain a persistent tree,
which is key for efficient layout and painting. however, it means
that direct interaction with the rendering layer is awkward at best
and bug-prone at worst.
flutter’s widget layer introduces a composition mechanism using the
reactive paradigm7 to manipulate the