Re:Programming Patterns I Like
by quadcore on Saturday June 29, 2019 @03:03AM
(#20311207)
See, mine is already way more simple than yours. This is my point, if you force yourself not to use jumps, you will end up with simpler and better programs. Also the second example you wrote is expressive and more importantly you can move that code. Also note that in many other programming language like lisp and haskell, you would never need the intermediate variable as you can basically write return if...
[ Parent
| on HN ]
The comment twice stands for some complex logic with side-effects which calculates value. You shouldn't put that in a ternary if, in most languages you can't.
And even then I would reverse the order, placing the exceptional and short case first.
Moving the code: why? You have a perfectly encapsulated function. If you need to, you have more problems with shadowing variables, functions and with return types.
For functional languages: doesn't Haskell explicitly have guard pattern? I thought the general guidelines favored matching and guards over if statements.
But that requires you to have the output assigned to a variable even without valid input..