From: Avdi Grimm Date: 2012-06-27T23:51:36+09:00 Subject: Re: Is this good code --f46d04428cc8ef659704c37559c3 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jun 27, 2012 at 10:23 AM, masta Blasta wrote: > end unless condition #->BOTHERS ME > ...more code > end #final end > Your instincts serve you well. The unless at the end is a surprise, a hidden gotcha sprung on the reader at the last moment that completely changes their understanding of the logic. With effectively two levels of conditionals, this code is a good candidate for a second method. In the extracted method, the extra condition can be converted to a guard clause: def the_method ...some code some_other_method end def some_other_method return unless condition if ...some code else ..some code end end -- Avdi --f46d04428cc8ef659704c37559c3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Wed, Jun 27, 2012 at 10:23 AM, masta Blasta <= span dir=3D"ltr"><lists@ruby-forum.com> wrote:
=A0 end unless condition #->BOTHERS ME
=A0 =A0...more code
=A0end #final end

Your instincts serve = you well. The unless at the end is a surprise, a hidden gotcha sprung on th= e reader at the last moment that completely changes their understanding of = the logic.

With effectively two levels of conditionals, this code = is a good candidate for a second method. In the extracted method, the extra= condition can be converted to a guard clause:

=A0 =A0= =A0def the_method=A0 =A0 =A0 =A0 ...some code=
=A0 =A0 =A0 =A0 some_other_method
=A0 =A0 end

= =A0 =A0 def some_other_method
=A0 =A0 = =A0 =A0return=A0unless condition
=A0 =A0 =A0 if
=A0 =A0 =A0 =A0 ...some code<= br style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px= ;background-color:rgb(255,255,255)"> =A0 =A0 =A0 else
=A0 =A0 =A0 =A0 ..some code =A0 =A0 =A0 =A0end
=A0 =A0 =A0end
--
Avdi
--f46d04428cc8ef659704c37559c3--