# Question on the "common way" to code

**URL:** <https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054>\
**Category:** Rails\
**Created:** [July 22, 2007, 5:36am UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054 "2007-07-22T05:36:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemesis256](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba9def/32.png) [@nemesis256](https://www.ruby-forum.com/u/nemesis256)\
**Post date:** [July 22, 2007, 5:36am UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054/1 "2007-07-22T05:36:01Z")

</div>

So I posted last week asking about new learning resources. I watched  
the [lynda.com](http://lynda.com) RoR videos, and now I just started reading the Ruby for  
Rails book. Both of these sources code in different ways, and I was  
wondering which way is most common. In other words, is one of these  
ways frowned upon in the Rails community? Keep in mind that these are  
rather specific examples and not necessarily complete lines of code.

1. First is how things are read from a database, or more specifically,  
sorting. One does this  
@albums = Album.find(:all, :order =\> “title ASC”)  
while the other does this  
@composers = Composer.find(:all).sort\_by{|c| [c.last\_name,  
c.first\_name]}

2. Looping.  
\<% for composer in @composers -%\>  
and  
@composers.each do |composer|

3. dynamic links  
link\_to(composer.first\_name + " " + composer.last\_name  
and  
link\_to(“#{composer.first\_name} #{composer.last\_name}”

In all cases I prefer the first ([lynda.com](http://lynda.com)) method, either because  
it’s shorter or because it requires less symbols. The Ruby for Rails  
method seems to be a lot more object focused. So which of these do  
you see more often or is the common way? Or does it not matter?

---

<div class="post-metadata">

**Author:** ![nemesis256](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba9def/32.png) [@nemesis256](https://www.ruby-forum.com/u/nemesis256)\
**Post date:** [July 22, 2007, 8:45am UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054/2 "2007-07-22T08:45:33Z")

</div>

(1) It depends. The first allows the database to do the sorting,  
which is almost always a good thing. The second performs the sorting  
in memory, and is thus likely to be slower. I’ve seldom seen the  
second method employed in real code because databases are so good at  
sorting.

(2) In RHTML, many people use \<% for composer in @composers -%\> I  
don’t. I don’t use for in Ruby. So my looping code is usually \<%  
@composers.each do |composer| -%\>. That said, it is often cleaner to  
render collections in partials. That would look like this: \<%=  
render :partiai =\> ‘composer’, :collection =\> @composers %\>.

(3) Whatever works. Either works fine, both are ugly but necessary.  
Better, in your model do this:

def full\_name  
first\_name + last\_name  
end

Then in your view, you can just write:

\<%= link\_to composer.full\_name, composers\_url(composer) %\>

Just some thoughts.

---

<div class="post-metadata">

**Author:** ![nemesis256](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba9def/32.png) [@nemesis256](https://www.ruby-forum.com/u/nemesis256)\
**Post date:** [July 22, 2007, 11:25am UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054/3 "2007-07-22T11:25:54Z")

</div>

s.ross wrote:

> (3) Whatever works. Either works fine, both are ugly but necessary.  
> Better, in your model do this:
> 
> def full\_name  
> first\_name + last\_name  
> end

The question was “…” vs. model.method + " " + model.method. I agree you  
should put these kinds of function in the model or in a helper.

> > 1. dynamic links  
> > link\_to(composer.first\_name + " " + composer.last\_name  
> > and  
> > link\_to("#{composer.first\_name} #{composer.last\_name}"

Personally I usually prefer the second alternative as I find it easier  
to read. But that’s just a matter of taste.

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![nemesis256](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba9def/32.png) [@nemesis256](https://www.ruby-forum.com/u/nemesis256)\
**Post date:** [July 22, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054/4 "2007-07-22T12:15:06Z")

</div>

Hi –

On Sat, 21 Jul 2007, nemesis256 wrote:

> @albums = Album.find(:all, :order =\> “title ASC”)  
> link\_to(composer.first\_name + " " + composer.last\_name  
> and  
> link\_to(“#{composer.first\_name} #{composer.last\_name}”
> 
> In all cases I prefer the first ([lynda.com](http://lynda.com)) method, either because  
> it’s shorter or because it requires less symbols. The Ruby for Rails  
> method seems to be a lot more object focused. So which of these do  
> you see more often or is the common way? Or does it not matter?

Going from bottom to top:

Using string interpolation is slightly more efficient because it  
avoids creating a lot of intermediate string objects.

“each” is generally considered more idiomatic Ruby than “for”. for is  
really just sugar-coating for each:

irb(main):018:0\> for a in nil; end  
NoMethodError: undefined method `each’ for nil:NilClass  
^^^^

Queries are a little more complicated. There are, as you point out,  
examples in my book where you could use :order (and probably other  
things) – and I certainly wouldn’t argue against it, though I’ve also  
seen people devote a lot of development time to fine-tuning big SQL  
statements when a bit of Ruby would have done the same thing, even if  
at some point down the road it ended up getting optimized into SQL.  
So you can be creative about what role(s) you want the Ruby techniques  
to play (or not play).

One rule of thumb that I try to follow is never to take out too many  
records, just for the sake of making it easy to manipulate them in  
Ruby. So, for example, I’d avoid this:

Item.find(:all).select {|i| i.number \< 100 }

since that’s liable to pull out some huge number of records and then  
throw most of them away. (I can’t remember if there are examples like  
that in my book… but never mind 🙂 But I would certain do things  
like this:

Item.find(:all, :conditions =\> “number \< 100”).sort\_by {|item|  
item.description.sub(/^(a|the) /i, “”)  
}

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![nemesis256](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba9def/32.png) [@nemesis256](https://www.ruby-forum.com/u/nemesis256)\
**Post date:** [July 22, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054/5 "2007-07-22T12:27:44Z")

</div>

nemesis256 wrote:

> 1. dynamic links  
> link\_to(composer.first\_name + " " + composer.last\_name  
> and  
> link\_to("#{composer.first\_name} #{composer.last\_name}"

To my knowledge, there is a difference between those two expressions :  
the second one requires more processing than the first one, because of  
the double quotes.

#{} evalutes what’s inside of the brackets and the result is transformed  
into a string. Because #{var} does not return “the content of var”, but  
“the result of the expression var transformed into a string”.  
So, if composer.first\_name returns a string already, you don’t have to  
transform it again in a string.

Another example. If you define number = 5, you can use  
“#{number} blabla”  
as well as  
number.to\_s + ‘blabla’  
There you can see where lies the difference. For numbers, you have to  
use .to\_s if you use concatenate (+), a transformation that isn’t needed  
for your composer.first\_name (apparently).

Besides, double quotes means that the whole expression will be parsed to  
look not only for #{}, but also for other escape caracters like “\n”.  
This means more processing.

So, the first solution is less redundant (with maybe some performance  
effects?), and the second is more readable. Make your choice.

Please, do correct me if I’m wrong, since I started RoR less than a week  
ago 🙂

---

<div class="post-metadata">

**Author:** ![nemesis256](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba9def/32.png) [@nemesis256](https://www.ruby-forum.com/u/nemesis256)\
**Post date:** [July 22, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/question-on-the-common-way-to-code/110054/6 "2007-07-22T12:23:17Z")

</div>

Hi –

On Sat, 21 Jul 2007, s.ross wrote:

> (3) Whatever works. Either works fine, both are ugly but necessary.  
> Better, in your model do this:
> 
> def full\_name  
> first\_name + last\_name  
> end
> 
> Then in your view, you can just write:
> 
> \<%= link\_to composer.full\_name, composers\_url(composer) %\>

That still leaves the question of how to define full\_name (with + or  
with string interpolation), though. Some informal benchmarking  
suggests, not too surprisingly, that if you’re adding just two  
strings, the adding might be faster, but if you’re adding repeatedly:

title + " " + first\_name + middle\_name + last\_name

then there’s a tipping point at which interpolation is faster.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
