Embed Notice
HTML Code
Corresponding Notice
- Embed this notice@lanodan @ElDeadKennedy @Junes @SilverDeth @ceo_of_monoeye_dating @djsumdog @j @phnt I mean, not the postfixed condition but the structure.
def gcd x, y
if x < 0 || y < 0; raise ArgumentError, "gcd(#{x},#{y})"; end
if x == y; return x; end
if y == 0; return x; end
if y > x; return gcd(y, x); end
gcd(x-y, y)
end
It's that you can increase invariants as you progress down. You know x-y>=0 because x>y and both are positive by the time you get to the subtraction. You just eliminate case after case as you progress. You replicate that with single-return and you get nesting. (The example is deliberately contrived; it is just that it gives you easy list of conditions.)