Reputation: 2881
private static string FindBookEanOrEanOnSplits(string[] splits, int index = 0)
{
string id = "";
if (index < 3)
{
id = Extractor.ExtractBookEanOrEan(splits[index].ToUpper());
if (id == string.Empty)
FindBookEanOrEanOnSplits(splits, index + 1);
}
return id;
}
for example, after on second call, the id is no longer empty then jumps out to return but calls for once(suddenly goes back) on this line FindBookEanOrEanOnSplits(splits, index + 1) that makes the id empty instead having not empty value
Upvotes: 1
Views: 623
Reputation: 35280
Why are you doing this recursively anyway? You could just do it in a loop:
private static string FindBookEanOrEanOnSplits(string[] splits)
{
string id = "";
for (int i = 0; i < 3; i++)
{
id = Extractor.ExtractBookEanOrEan(splits[index].ToUpper());
if (!string.IsNullOrEmpty(id)) break;
}
return id;
}
Recursive calls can be confusing to debug. It's entirely unnecessary in this case because you're only doing a simple index operation and calling another method. Recursion is useful when you're dealing with data structures or operations that are naturally hierarchical in nature and require the maintenance of "state" at each level.
Upvotes: 3