How to avoid problems with single line if statements
medium.com
medium.com
In Clojure:
(if (is-of-age?)
(give-beer))
or (if (is-of-age?)
(do (give-beer)
(allow gambling)))
which is the same as: (when (is-of-age?)
(give-beer)
(allow gambling)) if age>ageLowLimit then
givebeer
end if
is equivalent to: if age>ageLowLimit then givebeer
and you can even mix both styles: if age>ageLowLimit then
givebeer
allowGambling
else: giveSoda
endif
Altough I use it when it improves readability (for instance, with short tests and instructions), I don't like that you seem to open the code block with the "then", but you don't close it.But yeah - "dense" is an adjective that I try to avoid when coding. Code should be clean, simple, and easy to read.
if (test)
{
body;
}
if (...
So, 5 lines each. Not 6. I miscounted. if (test1) body1;
if (test2) body2;
if (test3) body3;
if (test4) body4;
if (test5) body2;
Between the two formats, I greatly prefer the second. It is understandable at a glance and makes the possible error in the last line stand out much more. In practice, I usually encounter the first style -even in cases as trivial as single-function-call-per-if.