aktxyz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-28 16:06 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. > > AlainTake 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Don''t know if it falls within your checking scope, but we have used http://rubyforge.org/projects/validator/ with good results /Rasmus --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---