On 18 Jun 2008, at 20:26, Joshua Muheim wrote:
> I'm working on a little Rails application and have a general coding
> question.
>
> So far my looks like the following:
>
> ---
> music_artist_id = params[:booking_ticket_music_artist_id]
>
> if music_artist_id and MusicArtist.exists?(music_artist_id)
>  @music_artist = MusicArtist.find(music_artist_id)
>  page[:mini_profile].show.reload
> else
>  page[:mini_profile].hide
> end
> ---
>
> On the first line I'm checking whether an optional parameter is  
> given or
> not. This works OK, but I wonder whether it is "better code" doing the
> following?
>
> ---
> begin
>  @music_artist =
> MusicArtist.find(params[:booking_ticket_music_artist_id])
>  page[:mini_profile].show.reload
> rescue RecordNotFound
>  page[:mini_profile].hide
> end
> ---
>
> When looking at it, the second one looks cleaner to me, but what would
> you say? Maybe the first one is quite faster than the second one?

I'd tend to use the second version but conceivably performance gains  
would accrue with the former approach as you avoid a whole class of  
unnecessary database lookups. As to whether or not that matters  
depends on the application...


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason