Conditional modifier syntax

  • Thread starter Pau Garcia i Quiles
  • Start date
P

Pau Garcia i Quiles

--nextPart4600703.AyXPV3I928
Content-Type: text/plain;
charset="iso-8859-15"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello,

Am I the only one who finds beautiful this syntax for conditional modifiers?

@playlist=3D source[:playlist] if source[:playlist].instance_of?(XSPF::play=
list)=20
else raise TypeError "The playlist must be an instance of XSPF::playlist"

It reads like a book even if you don't know Ruby: "@playlist is X if this o=
r Y=20
if not"

PS: It's beautiful but it doesn't work :-/

=2D-=20
Pau Garcia i Quiles
http://www.elpauer.org
(Due to the amount of work, I usually need 10 days to answer)

--nextPart4600703.AyXPV3I928
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQBFOpCI/DzYv9iGJzsRAkH2AKCxQn31cRODF6v9I8jvh49nGv8b1QCfXNwr
kJVsIsXJZPHZDTtkbzOx0Mg=
=yaZe
-----END PGP SIGNATURE-----

--nextPart4600703.AyXPV3I928--
 
V

Vincent Fourmond

Pau said:
Hello,

Am I the only one who finds beautiful this syntax for conditional modifiers?

@playlist= source[:playlist] if source[:playlist].instance_of?(XSPF::playlist)
else raise TypeError "The playlist must be an instance of XSPF::playlist"

PS: It's beautiful but it doesn't work :-/

Well, you can use if in two different ways:

a if b

or

if a
b
end

Only in the second case you can use else. I would write it this way:
(in two lines, in case that doesn't show in the mail)

raise TypeError "The playlist must be an instance of XSPF::playlist"
unless source[:playlist].instance_of?(XSPF::playlist)
@playlist= source[:playlist]

I tend to find that more clear: you first leave out the case that
doesn't interest you.

Just my opinion ;-) !

Vince
 
D

David Vallner

--------------enig905A9A2FE0310179C2884608
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
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

<rant>

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.

</rant>

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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top