- expression-oriented programming one of the great advances of FP
- expressions plug together like legos, making more malleable programming experience in-the-small
Write in an expression-oriented style, scoping variables as locally as possible:
let x = do {
let tmp = f();
tmp * tmp + 1
};Use conditional statements as expressions, instead of awkward nested ternaries:
let x = do {
if (foo()) { f() }
else if (bar()) { g() }
else { h() }
};Especially nice for templating languages like JSX:
return (
<nav>
<Home />
{
do {
if (loggedIn) {
<LogoutButton />
} else {
<LoginButton />
}
}
}
</nav>
)- key refactoring principles:
do { <expr>; }equivalent to<expr>(do { <stmt> };)equivalent to{ <stmt> }
- this semantic transparency is demonstrated by the semantics:
- Return the result of evaluating Body.
How to avoid either parsing conflict in statement context with do-while, or dangling-else type of ambiguity:
do do f(); while (x);I have several alternatives I intend to explore here.
@dherman or anyone: I have a question about whether it is valid to use the
doexpression for anif statementWITHOUT ANelseCONDITION. When I use adoexpression withoutelse, I get aneslinterror forno-unused-expressions. I am using this code in my JSX for a React component, something like this (illustrative purposes only):I prefer to use the
doconstruct over the unnatural, confusing usage of&&, as suggested by Facebook for React rendering. Is this a valid use case?Note that babel/eslint-plugin-babel#13 may be related to this issue, but I don't know for sure yet until I understand more about the design of the
doexpression.Thank you.