# Best practice for set-and-test idiom?

**URL:** <https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891>\
**Category:** Ruby\
**Created:** [August 14, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891 "2008-08-14T19:54:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 14, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/1 "2008-08-14T19:54:25Z")

</div>

I’ve been using Ruby/Rails for about about four months and wonder what  
the the Ruby alternative to the popular C/C++ idiom of setting and  
testing a variable in the condition of an ‘if’ statement. If C/C++ the  
usage is typically:

```
 if ((ptr = fcn_returning_ptr(...)) == NULL) {
   val = ptr->ref
 } else {
   val = DEFAULT
 }

```

or the compact version:

```
 val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT

```

Which, when I translate to Ruby yields (ActiveRecord example):

```
 unless (ar = Model.find(...)).nil?
    val = ar.attribute
 else
    val = DEFAULT

```

or the equivalent expression.

It seems to me that this is very unRuby-like and I don’t see any similar  
usages in any of the code-bases in which I have looked.

Will someone please provide a counter-example?

Thanks,

kevin nolan

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 14, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/2 "2008-08-14T20:05:43Z")

</div>

Kevin N. wrote:  
(snip)

> or the compact version:
> 
> ```
> val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT
> 
> ```
> 
> Which, when I translate to Ruby yields (ActiveRecord example):
> 
> ```
> unless (ar = Model.find(...)).nil?
> val = ar.attribute
> else
> val = DEFAULT
> 
> ```
> 
> or the equivalent expression.
> 
> It seems to me that this is very unRuby-like and I don’t see any similar  
> usages in any of the code-bases in which I have looked.
> 
> Will someone please provide a counter-example?

Ruby has the ternary ?: operator, too:

val = (ar = Model.find(…)) ? ar.attribute : DEFAULT

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 14, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/3 "2008-08-14T21:42:03Z")

</div>

Tim H. wrote:

> Kevin N. wrote:  
> (snip)
> 
> > or the compact version:
> > 
> > ```
> > val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT
> > 
> > ```
> > 
> > Which, when I translate to Ruby yields (ActiveRecord example):
> > 
> > ```
> > unless (ar = Model.find(...)).nil?
> > val = ar.attribute
> > else
> > val = DEFAULT
> > 
> > ```
> > 
> > or the equivalent expression.
> > 
> > It seems to me that this is very unRuby-like and I don’t see any similar  
> > usages in any of the code-bases in which I have looked.
> > 
> > Will someone please provide a counter-example?
> 
> Ruby has the ternary ?: operator, too:
> 
> val = (ar = Model.find(…)) ? ar.attribute : DEFAULT

Tim,

I know – I just omitted it for the sake of brevity. Do you have any  
thoughts about a better, or my ruby-like technique?

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 14, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/4 "2008-08-14T23:46:33Z")

</div>

Kevin N. wrote:  
(snip)

> I know – I just omitted it for the sake of brevity. Do you have any  
> thoughts about a better, or my ruby-like technique?

Actually I suck at these kinds of stylistic questions. If I want  
somebody else (where “somebody else” includes “me, after a few months  
when I’ve forgotten what I was doing”) to be able to easily understand  
my code I use the “long” version with no short-cuts. If it’s just for me  
I stick with the first thing that works.

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 15, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/5 "2008-08-15T17:22:32Z")

</div>

On Aug 15, 2008, at 8:12 AM, Gavin S. wrote:

> Does the Model.find(…) need to be buried in a test condition? If  
> it’s retrieving worthwhile data, it’s worth a line of its own.

spoken like someone who has debugged code from logs more than once 😉

> val =  
> case x  
> when 1: …  
> when 2: …  
> …  
> end

me too - the indent is a great indicator. sometimes i even do

val = (  
if condition then  
processing  
processing  
value1  
else  
processing  
value2  
end  
)

for more visual distinction

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 15, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/6 "2008-08-15T16:15:38Z")

</div>

On Aug 15, 3:51 am, Kevin N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Will someone please provide a counter-example?

Does the Model.find(…) need to be buried in a test condition? If  
it’s retrieving worthwhile data, it’s worth a line of its own.

model = Model.find(…)  
val = (model && model.attribute) || DEFAULT

The second line could be called idiomatic Ruby. Whether it appeals to  
you or not, I can’t predict 🙂

For situations where more complex processing is required before  
setting the variable, I am a _big_ fan of these:

val =  
if condition then  
processing  
processing  
value1  
else  
processing  
value2  
end

val =  
case x  
when 1: …  
when 2: …  
…  
end

Cheers,  
Gavin

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 15, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/7 "2008-08-15T17:41:16Z")

</div>

On Aug 15, 2008, at 9:32 AM, Phlip wrote:

> Can Ruby report an error’s column? Or only its line number?

the line

which.is.why.this.is.crazy.and.inhumane.to.operators

😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![cbx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@cbx](https://www.ruby-forum.com/u/cbx)\
**Post date:** [August 15, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/best-practice-for-set-and-test-idiom/145891/8 "2008-08-15T17:35:25Z")

</div>

ara.t.howard wrote:

> > Does the Model.find(…) need to be buried in a test condition? If  
> > it’s retrieving worthwhile data, it’s worth a line of its own.
> 
> spoken like someone who has debugged code from logs more than once 😉

Can Ruby report an error’s column? Or only its line number?
