Question on Extract Method

I need some education on how Refactor -> Extract Method is supposed to work.

If I select the following code and chose Extract Method, why can't I specify
a return value?

            if (hours < 0)
            {
                throw new ArgumentException("Hours must be greater than or
equal to zero",
                                            hours.ToString());
            }
            if (hours > MAXHOURS)
            {
                throw new ArgumentException("Hours above company limit!",
hours.ToString());
            }

I'm trying to pull out these two blocks of code to a boolean method, but
the Return Value portion of the Extract Method dialog isn't available.  What
am I missing?


5 comments
Comment actions Permalink

Hello Jim,

I'm trying to pull out these two blocks of code to a boolean method,
but the Return Value portion of the Extract Method dialog isn't
available.  What am I missing?

I guess, you don't miss anything. The return type should be void. How do
you suggest the resulting code (with boolean return type) might look like?

Regrads,
Maxim


0
Comment actions Permalink

Hello Maxim,

I guess, you don't miss anything. The return type should be void. How
do you suggest the resulting code (with boolean return type) might
look like?


I'd like the ability to force a return type in the signature.  Obviously,
the code I selected there isn't complete enough for a return value.  What
I'm trying to do is extract the initial part of a method, the code I initially
posted:

            if (hours < 0)
            {
                throw new ArgumentException("Hours must be greater than or
equal to zero",
                                            hours.ToString());
            }
            if (hours > MAXHOURS)
            {
                throw new ArgumentException("Hours above company limit!",
hours.ToString());
            }

and pull that to a method which I would then finish adapting, something along
the lines of:

        private static bool IsInsideValidHours(double hours)
        {
            if (hours < 0)
            {
                throw new ArgumentException("Hours must be greater than or
equal to zero",
                                            hours.ToString());
            }
            if (hours > MAXHOURS)
            {
                throw new ArgumentException("Hours above company limit!",
hours.ToString());
            }
           
            return true;
        }

I'm trying to save myself the small but annoying hassle of having to change

        private static void IsInsideValidHours(double hours)

to        

       private static bool IsInsideValidHours(double hours)


Does that make any sense?


0
Comment actions Permalink

Hello Jim,

and pull that to a method which I would then finish adapting,
something along the lines of:

Your return value is always true. Can't get the point.

I'm trying to save myself the small but annoying hassle of having to
change

You can use 'Change Signature Refactoring' if you don't want to change the
type hy hand.

Anyway, this does not seem (at least for me) to be a part of Extract Method
Refactoring. And given the ability to specify you return true who will provide
Reshaper with the return value?

Regrads,
Maxim


0
Comment actions Permalink

You can use 'Change Signature Refactoring' if you don't want to change
the type hy hand.


That sounds more like what I'm looking for.

Anyway, this does not seem (at least for me) to be a part of Extract
Method Refactoring. And given the ability to specify you return true
who will provide Reshaper with the return value?


I'm new to ReSharper and am trying to figure out how Extract Method should
work -- and there's no documentation yet, so I'm using this the wrong way.
Things will be clearer once the documentation's out, I'm sure.


0
Comment actions Permalink

You can use 'Change Signature Refactoring' if you don't want to change
the type hy hand.


That sounds more like what I'm looking for.

Anyway, this does not seem (at least for me) to be a part of Extract
Method Refactoring. And given the ability to specify you return true
who will provide Reshaper with the return value?


I'm new to ReSharper and am trying to figure out how Extract Method should
work -- and there's no documentation yet, so I'm using this the wrong way.
Things will be clearer once the documentation's out, I'm sure.


0

Please sign in to leave a comment.