Exiting a method early from another method

14 views
Skip to first unread message

Randy Schmidt

unread,
Oct 20, 2012, 5:04:31 PM10/20/12
to Philly.rb
Hi there,

I'm working through some refactoring and part of that is pulling logic
out into small methods. One of these areas is used to exit early from
the method and I would like to pull it out into its own method as
well, however, then I can't use it to exit early.

Any ideas how I can do this?

Here is the code with a lot of the cruft removed:

https://gist.github.com/3924791

Thank you!
--
Randy Schmidt

Randy Schmidt

unread,
Oct 20, 2012, 5:10:29 PM10/20/12
to Philly.rb
On Sat, Oct 20, 2012 at 5:04 PM, Randy Schmidt <m...@r38y.com> wrote:
> Hi there,
>
> I'm working through some refactoring and part of that is pulling logic
> out into small methods. One of these areas is used to exit early from
> the method and I would like to pull it out into its own method as
> well, however, then I can't use it to exit early.
>
> Any ideas how I can do this?

Hey Randy, since this is an error that should not happen under normal
circumstances, why don't you raise an exception?

Sorry to waste your time,
--
Randy Schmidt

Justin Campbell

unread,
Oct 20, 2012, 5:11:44 PM10/20/12
to phil...@googlegroups.com
I'd rename the method non_checkable and say `return if non_checkable`.

-Justin

Sent from my iPhone
> --
> You received this message because you are subscribed to the Google Groups "Philly.rb" group.
> To post to this group, send email to phil...@googlegroups.com.
> To unsubscribe from this group, send email to phillyrb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/phillyrb?hl=en.
>

Randy Schmidt

unread,
Oct 20, 2012, 5:15:23 PM10/20/12
to phil...@googlegroups.com
On Sat, Oct 20, 2012 at 5:11 PM, Justin Campbell
<jus...@justincampbell.me> wrote:
> I'd rename the method non_checkable and say `return if non_checkable`.

Great idea, thank you! Sometimes I feel stupid :P

Thank you,
--
Randy Schmidt

Justin Campbell

unread,
Oct 20, 2012, 5:30:06 PM10/20/12
to phil...@googlegroups.com
I like the exception idea too fwiw

-Justin

Sent from my iPhone

Reply all
Reply to author
Forward
0 new messages