125 lines
5.3 KiB
HTML
125 lines
5.3 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<title>Ruby if unless - lespiedsdanslecode.org</title>
|
||
<meta name="DC.title" content="lespiedsdanslecode.org, site de Yannick François aka yaf (ou ya_f)."/>
|
||
<meta name="description" content="Le site de Yannick François. Développeur Senior, Responsable Produit, Pédagogue. Je travail chez Pix"/>
|
||
<meta name="keywords" content="code, développement, programmation, apprentissage, apprendre, lean, agile, logiciel, tdd, objet, refactoring, libre, creative commons, linux, unix"/>
|
||
<meta name="author" content="Yannick François, https://lespiedsdanslecode.org"/>
|
||
<meta name="designer" content="Yannick François, https://lespiedsdanslecode.org"/>
|
||
<meta name="geo.placename" content="Le Faouët, Pontivy, Morbihan, Bretagne, France métropolitaine, 56320, France"/>
|
||
<meta name="robots" content="index,follow" />
|
||
<meta name="language" content="French" />
|
||
<meta name="HandheldFriendly" content="True" />
|
||
<meta name="MobileOptimized" content="320" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
|
||
<link rel="index" title="Yannick François / Développeur / Pédagogue" href="index.html" />
|
||
<link rel="alternate" type="application/rss" href="/feed.xml" title="lespiedsdanslecode.org/feed" />
|
||
<style>
|
||
body {
|
||
font-size:16pt;
|
||
max-width:42rem;
|
||
min-width: 18rem;
|
||
margin: 1em;
|
||
color: black;
|
||
background: white;
|
||
font-family: Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif;
|
||
}
|
||
a { color: darkred; }
|
||
a:hover { background: gold; }
|
||
a:visited, a:active { color: dimgrey; }
|
||
nav { text-align:center; font-size:smaller; padding: 0; margin: 0; }
|
||
nav li { list-style-type: none; }
|
||
h2 > a, h2 > a:active, h2 > a:visited { color: lightgrey; text-decoration:none;}
|
||
h2 > a:hover { color: black; text-decoration:underline }
|
||
|
||
@media (max-width: 1000px) { #portrait {display:none;} }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<article>
|
||
<h1>Ruby if unless</h1>
|
||
<p>Une des choses très agréable avec <a
|
||
href="http://ruby-lang.org">Ruby</a> c’est ça syntaxe. Un des mots
|
||
bien particulier en Ruby est <code>unless</code>.</p>
|
||
<p>Je partage tout à fait l’avis de ce billet <a
|
||
href="http://railstips.org/2008/12/1/unless-the-abused-ruby-conditional">Unless,
|
||
The Abused Ruby Conditional</a> <code>unless</code> c’est bien,
|
||
très bien même dans certain cas, mais en abuser c’est mal. Ce mot
|
||
peut rendre les choses plus lisible tout comme il pourrait les
|
||
compliquer.</p>
|
||
<p>Une condition doit représenter une intention, unless permet de
|
||
le faire, mais cela doit rester une intention.</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
xml.updated @items.first.updated_at.xmlschema unless @items.empty?
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Ce code, extrait de la classe feed.atom.builder de <a
|
||
href="http://typosphere.org">Typo</a> est une bonne utilisation de
|
||
<code>unless</code>. On évite ainsi le vilain:</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
xml.updated @items.first.updated_at.xmlschema if !@items.empty?
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Un peu comme, et dans la même classe, nous avons un peu plus
|
||
haut:</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
if(not this_blog.blog_subtitle.blank?)
|
||
xml.subtitle this_blog.blog_subtitle, "type"=>"html"
|
||
end
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Assez étrange, nous aurions pu avoir plutôt</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
unless this_blog.blog_subtitle.blank?
|
||
xml.subtitle this_blog.blog_subtitle, "type"=>"html"
|
||
end
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Voir</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
xml.subtitle this_blog.blog_subtitle, "type"=>"html" unless this_blog.blog_subtitle.blank?
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Il y a part contre dans la méthode <code>ping_article!</code>
|
||
du modèl <code>blog.rb</code> une mauvaise utilisation de
|
||
<code>unless</code> (à mon avis)</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
unless global_pings_enabled? && settings.has_key?(:url) && settings.has_key?(:article_id)
|
||
throw :error, "Invalid trackback or trackbacks not enabled"
|
||
end
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>et une bonne.</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
unless article.allow_pings?
|
||
throw :error, "Trackback not saved"
|
||
end
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Pour la bonne, rien à dire. Par contre, la première n’exprime
|
||
pas assez clairement l’intention.</p>
|
||
<pre>
|
||
{% highlight ruby %}
|
||
if !global_pings_enabled? || !settings.has_key?(:url) || !settings.has_key?(:article_id)
|
||
throw :error, "Invalid trackback or trackbacks not enabled"
|
||
end
|
||
{% endhighlight %}
|
||
</pre>
|
||
<p>Je trouve que là c’est plus clair, on comprend mieux que si
|
||
l’une des trois conditions n’est pas rempli, on lève un
|
||
exception.</p>
|
||
<p><em>Tout ceci est une histoire de gout peut-être, vous en
|
||
pensez quoi ?</em></p>
|
||
</article>
|
||
</body>
|
||
</html>
|