3 ms·
return pos < this.length() ? Some(value) : Nope See, mine is already way more simple than yours. This is my point, if you force yourself not to use jumps, you
by quadcore 7y ago
return pos < this.length() ? Some(value) : Nope
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...
- epse 7y agoBut that requires you to have the output assigned to a variable even without valid input..
- quadcore 7y agoThis whole guard/validation thing is a fabrication of the mind. Because early returns do returns something on an invalid input. If anything, those validations should be asserts/exceptions.
- rrobukef 7y agoThe 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.