whats best practice, param checking

I have habit of checking parameters that are passed into my funcions,
like so

Class Account

  def process_widgets(widget)

    return if widget == nil || !widget.is_a?(Widget)

    #do my stuff here

  end

end

Is it better to just let the function wipe out with an exception ?

-- THanks

aktxyz

  def process_widgets(widget)
    return if widget == nil || !widget.is_a?(Widget)

    #do my stuff here
  end
Is it better to just let the function wipe out with an exception ?

Most of the time, if you are testing for gross programmation bugs, not
valid usage edge cases, just let it explode. The stacktrace will tell
where the function was incorrectly called.
If the error is hard to guess this way, or if it's an API or a
reusable library, then nicer error recovery/messages could be useful.

Alain

Alain Ravet wrote:

aktxyz

> def process_widgets(widget)
> return if widget == nil || !widget.is_a?(Widget)
>
> #do my stuff here
> end
> Is it better to just let the function wipe out with an exception ?

Most of the time, if you are testing for gross programmation bugs, not
valid usage edge cases, just let it explode. The stacktrace will tell
where the function was incorrectly called.
If the error is hard to guess this way, or if it's an API or a
reusable library, then nicer error recovery/messages could be useful.

Alain

Take a look at the 'verify' method. This is probably only useful for
common patterns. For example, I do this in some of my controllers..

verify :only => [ 'show', 'edit', 'destroy' ],
         :params => :id,
         :add_flash => { :notice => 'Missing company ID.' },
         :redirect_to => { :action => 'list' }

This redirects to the list action if no :id is passed.

_Kevin

Don't know if it falls within your checking scope, but we have used
http://rubyforge.org/projects/validator/ with good results

/Rasmus