From: Josef 'Jupp' Schugt Date: 2006-10-28T05:48:14+09:00 Subject: Re: simple math question -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Robert Oliver wrote: | On 10/26/06, Wilson Bilkovich wrote: |> |> class Fixnum |> def even? |> (self % 2).zero? |> end |> def odd? |> !even? |> end |> end |> |> | I normally do that, but I remember somewhere reading that it was | bad form to not include the return statement. I'm not sure where | this was. Is anyone familiar with this syntax quasi-rule? It is a programming-paradigm-dependent semantics rule. If one follows the functional paradigm, one would write leap? in this way: class Fixnum ~ def leap? ~ if (self % 400).zero? ~ true ~ elsif (self % 100).zero? ~ false ~ elsif (self % 4).zero? ~ true ~ else ~ false ~ end ~ end end if one follows the proceural paradigm one would rather write class Fixnum ~ def leap? ~ if (self % 400).zero? ~ return true ~ elsif (self % 100).zero? ~ return false ~ elsif (self % 4).zero? ~ return true ~ else ~ return false ~ end ~ end end Bad style came into play if one would write something like class Fixnum ~ def leap? ~ if (self % 400).zero? ~ return true ~ elsif (self % 100).zero? ~ return false ~ elsif (self % 4).zero? ~ return true ~ end ~ false ~ end end because in this case one starts with the procedural paradigm and switches to the functional paradigm. An example of bad style were: class Fixnum ~ def leap? ~ return true if (self % 400).zero? ~ return false if (self % 100).zero? ~ return true if (self % 4).zero? ~ false ~ end end While not being necessary it improves code to add a return in order to stick to the procedural paradigm. class Fixnum ~ def leap? ~ return true if (self % 400).zero? ~ return false if (self % 100).zero? ~ return true if (self % 4).zero? ~ return false ~ end end Some rules of thumb: ~ - Use or avoid return consistently. ~ - Make your code reflect your intention to the maximum extend ~ permited by the language you use - if you want a procedure to ~ return some value use "return", if you want a function to evaluate ~ to some value don't. HTH, Jupp -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQFFQnB6rhv7B2zGV08RAoiyAJ9dbts+7x2jfwjNWGYuoQiWhO7bUwCg3b4D H5CZe2xIBqcXalxCX1br7Xg= =jdH6 -----END PGP SIGNATURE-----