Extract method control flow - option to flip boolean return value meaning

the extract method case where control flow is affected is brilliant and incredibly useful.

Except that, in some cases, such as when something positive is being done, I want it to return false if it should exit the parent.

eg if the method being extracted was to be called “TryAddRoleToUser”
I'd expect this to return true oif it was successful, and false if not.
I would typically want to continue in the success case.

eg
if (!TryAddRoleToUser(…))
{
   do some remedial actions
}

0
1 comment

Hello David,

thank you for contacting us.

Looks like your request can be done by the existing Extract Method functionality. We assume that your code is properly prepared for such refactoring.

For example:

static void IsStringEmpty(string strInput)
{
   var bRet = strInput.Length > 0;
   // add any other functionality here
   if (bRet)
       Console.WriteLine("Empty");
   else
       Console.WriteLine("Not Empty");
}

Select the two first lines in this method and perform the Extract Method. The result will be like this:

static void IsStringEmpty(string strInput)
{
   var bRet = BRet(strInput);
   if (bRet)
       Console.WriteLine("Empty");
   else
       Console.WriteLine("Not Empty");
}
private static bool BRet(string strInput)
{
   var bRet = strInput.Length > 0;
   // add any other functionality here
   return bRet;
}

Please confirm that this example solves the issue. Otherwise please provide more details on your request and sample code.

Thank you!

0

Please sign in to leave a comment.