From: David Vallner Date: 2006-10-23T09:11:55+09:00 Subject: Re: Conditional modifier syntax --------------enig905A9A2FE0310179C2884608 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Pau Garcia i Quiles wrote: > Hello, >=20 > Am I the only one who finds beautiful this syntax for conditional modif= iers? >=20 > @playlist=3D source[:playlist] if source[:playlist].instance_of?(XSPF::= Playlist)=20 > else raise TypeError "The playlist must be an instance of XSPF::Playlis= t" >=20 > It reads like a book even if you don't know Ruby: "@playlist is X if th= is or Y=20 > if not" >=20 > PS: It's beautiful but it doesn't work :-/ >=20 I wouldn't like else in conditional modifiers for the same reason I don't like conditional modifiers after code blocks (the not-a-closure kin= d). I prefer to use them for trivial special case handling (early returns, loop cycle breaks) when it's more important to see what's happening than why. (I find it easier to visually parse past a block of "return nil if some_condition" on the beginning of a method to get to the main logic, for example.) A conditional modifier effectively "hides" the conditional; when used with a code block, it's cheating people reading your code into believing there's one level of logic flow nesting less (it's not immediately visible as indentation, for example). There's a word for this: obfuscatio= n. The above code snippet also doesn't read as you describe it, instead it reads "@playlist is X if X is a Playlist, fail if not." You place the failure in the source after the assignment, when in fact you're checking against a precondition. (Emphasis on pre-). The method fails -before- the assignment, placing the cause of the failure after it is misleading. I'd rewrite the code snippet as: raise TypeError "The playlist must be an instance of XSPF::Playlist" unless source[:playlist].is_a? XSPF::Playlist @playlist =3D source[:playlist] grouping the special cases (precondition checking) at the beginning of the method to ensure that any failure of those is clean and doesn't leave the object in an inconsistent state. David Vallner --------------enig905A9A2FE0310179C2884608 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFPAjWy6MhrS8astoRAiCyAJ9Om7B4l658jhJUvI1H4G2FnXmoCQCfV16o 2tHyBZAMi+pmxDMXoLXNoH0= =hkch -----END PGP SIGNATURE----- --------------enig905A9A2FE0310179C2884608--