Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting the ReSharper error "The extracted code has multiple entry points"?

I am using the ReSharper to re-factor my code. When I try to move a block of code to the method, I get the following warning:

The extracted code has multiple entry points

Here is the method signature I am planning to use:

private void GetRatePlanComponents(ProductPlan productPlan, 
    ProductRatePlan productRatePlan)    

I searched the web to understand what does it mean. But didn't have much luck. Would someone explain it?

For your reference, here is the code snippet I am trying to move to a separate method:

QueryResult productRatePlanChargeQueryResult = 
    _zuoraService.query(string.Format(@"select Id, Name, IncludedUnits from
        ProductRatePlanCharge where ProductRatePlanId = '{0}' and 
        ChargeModel = 'Overage Pricing'", productRatePlan.Id));

if (productRatePlanChargeQueryResult.size > 0)
{
    foreach (ProductRatePlanCharge productRatePlanCharge 
        in productRatePlanChargeQueryResult.records)
    {
        string numberOfUnits = productRatePlanCharge.IncludedUnits.ToString();

        if (productRatePlanCharge.Name.Equals("Users"))
        {
            productPlan.NumberofUsers = numberOfUnits;
        }
        else if (productRatePlanCharge.Name.Equals("Projects"))
        {
            productPlan.NumberofProjects = numberOfUnits;
        }
        else if (productRatePlanCharge.Name.Equals("Storage"))
        {
            decimal volumeOfStorage;
            if (decimal.TryParse(productRatePlanCharge.IncludedUnits.ToString(), 
                out volumeOfStorage))
            {
                if (volumeOfStorage < 1) volumeOfStorage *= 1000;
                    productPlan.VolumeofStorage = volumeOfStorage.ToString();
                }
                else
                {
                    productPlan.VolumeofStorage = numberOfUnits;
                }
            }
        }
    }
}
like image 755
Moon Avatar asked Oct 03 '11 19:10

Moon


2 Answers

It looks like you may have encountered a known issue:

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile )
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = projectFile.Location;

  foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath))
    yield return holder;

  foreach(var holder in GetHoldersInFile(sourceFile, targetPath))
    yield return holder;
}

Select both foreach-loops and extract method. It gives strange warning that the fragment has multiple entry points (??!) and results in the following code:

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile )
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = projectFile.Location;

  foreach(var tagPrefixHolder in Foo(sourceFile, targetPath))
       yield return tagPrefixHolder;
}

private static IEnumerable<ITagPrefixHolder> Foo(IPsiSourceFile sourceFile, FileSystemPath targetPath)
{
  foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath))
    yield return holder;
  foreach(var holder in GetHoldersInFile(sourceFile, targetPath))
    yield return holder;
}

It would be better to replace generated foreach with simple return Foo(sourceFile, targetPath);.

like image 179
2 revs, 2 users 91% Avatar answered Oct 31 '22 19:10

2 revs, 2 users 91%


I have seen ReSharper do this same thing when the code I was trying to extract had a couple of throw statements.

You can do what I did in that case--systematically comment out one line at a time until you find the one that ReSharper trips over. Then you can extract the method and uncomment the line afterwards.

Or you can just refactor it by hand.

like image 1
JLScott Avatar answered Oct 31 '22 19:10

JLScott