C
C#2d ago
Faker

✅ Separate local function with explicit 'return' statement

C#

string[] words = {"racecar" ,"talented", "deified", "tent", "tenet"};

Console.WriteLine("Is it a palindrome?");
foreach (string word in words)
{
Console.WriteLine($"{word}: {IsPalindrome(word)}");
}

bool IsPalindrome(string word)
{
int startPointerIndex = 0;
int endPointerIndex = word.Length - 1;
for (int i = 0; i < word.Length - 1; i++)
{
if (word[startPointerIndex] == word[endPointerIndex] && startPointerIndex < endPointerIndex)
{
continue;
}
else
{
return false;
}

startPointerIndex++;
endPointerIndex--;
}

return true;
}
C#

string[] words = {"racecar" ,"talented", "deified", "tent", "tenet"};

Console.WriteLine("Is it a palindrome?");
foreach (string word in words)
{
Console.WriteLine($"{word}: {IsPalindrome(word)}");
}

bool IsPalindrome(string word)
{
int startPointerIndex = 0;
int endPointerIndex = word.Length - 1;
for (int i = 0; i < word.Length - 1; i++)
{
if (word[startPointerIndex] == word[endPointerIndex] && startPointerIndex < endPointerIndex)
{
continue;
}
else
{
return false;
}

startPointerIndex++;
endPointerIndex--;
}

return true;
}
Hello guys, can someone explain why my IDE yells at me to use an "explicit" return statement pls. What is it trying to convey and why should I use it?
No description
21 Replies
dcode1213
dcode12132d ago
The reason your IDE is complaining about using an explicit return statement is due to how your IsPalindrome method is structured. Specifically, the problem is in the loop condition. You have a loop that iterates with i, but inside the loop, you’re checking if the characters at startPointerIndex and endPointerIndex match. However, the loop itself runs one iteration too many, because the condition i < word.Length - 1 means that it will not check the last character properly. Additionally, the loop structure and flow may result in the IDE warning you to use an explicit return statement, particularly because it expects that a value should be returned in all possible cases (even if you reach the end of the method). To resolve this issue, you should adjust the loop and the condition where the return statement is placed.
Faker
FakerOP2d ago
ahhh yeah true, I shouldn't have the word.Length -1 but word.Length. The problem was due to that? Like no taking into account all iterations possible? Because when I change it, the warning went away but still I don't get what the message is trying to say, for e.g. in the code above where would I put an explicit return statement pls Also, does it matter if I ignore this warning ?
Mayor McCheese
Mayor McCheese15h ago
you can safely ignore, you probably shouldn't, but you can @Faker It also has nothing to do with the internals of IsPalindrome. There is an analyzer suggesting a return such that any local functions are "unreachable code". In the example below, if you paste into an IDE you'll get the warning on line 9, and a slightly different warning on line 11.
string[] words = { "racecar", "talented", "deified", "tent", "tenet" };

Console.WriteLine("Is it a palindrome?");
foreach (string word in words)
{
Console.WriteLine($"{word}: {IsPalindrome(word)}");
}

bool IsPalindrome(string word)
{
bool CheckIsPalindrome()
{
for (int left = 0, right = word.Length - 1; left < right; left++, right--)
if (char.ToLower(word[left]) != char.ToLower(word[right]))
return false;

return true;
}

return CheckIsPalindrome();
}
string[] words = { "racecar", "talented", "deified", "tent", "tenet" };

Console.WriteLine("Is it a palindrome?");
foreach (string word in words)
{
Console.WriteLine($"{word}: {IsPalindrome(word)}");
}

bool IsPalindrome(string word)
{
bool CheckIsPalindrome()
{
for (int left = 0, right = word.Length - 1; left < right; left++, right--)
if (char.ToLower(word[left]) != char.ToLower(word[right]))
return false;

return true;
}

return CheckIsPalindrome();
}
Adding the local functions explicitly below the return leaves the logic of the function separated from the rest of the code giving you a cleaner separation.
string[] words = { "racecar", "talented", "deified", "tent", "tenet" };

Console.WriteLine("Is it a palindrome?");
foreach (string word in words)
{
Console.WriteLine($"{word}: {IsPalindrome(word)}");
}

return;

bool IsPalindrome(string word)
{
return CheckIsPalindrome();

bool CheckIsPalindrome()
{
for (int left = 0, right = word.Length - 1; left < right; left++, right--)
if (char.ToLower(word[left]) != char.ToLower(word[right]))
return false;

return true;
}
}
string[] words = { "racecar", "talented", "deified", "tent", "tenet" };

Console.WriteLine("Is it a palindrome?");
foreach (string word in words)
{
Console.WriteLine($"{word}: {IsPalindrome(word)}");
}

return;

bool IsPalindrome(string word)
{
return CheckIsPalindrome();

bool CheckIsPalindrome()
{
for (int left = 0, right = word.Length - 1; left < right; left++, right--)
if (char.ToLower(word[left]) != char.ToLower(word[right]))
return false;

return true;
}
}
Unknown User
Unknown User9h ago
Message Not Public
Sign In & Join Server To View
Mayor McCheese
The easy answer is they aren't intentionally. My example adds more clarity into why the message is coming up. This is just all top level statements.
Faker
FakerOP5h ago
yeah I see, the idea is the IDE expect that the code needs to run until the end, but the thing is at the end, we have a method definition; we just use return to indicate that we are leaving the "outer" method ? Hmm in the exercise template I used, I think they use the local function (function inside function); this isn't recommended or?
Unknown User
Unknown User4h ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4h ago
ahhh I see yep noted, thanks !
Mayor McCheese
Yesish, the compiler doesn't care either way, it's purely about readability from humans Local functions can damage human readability so should be used with care. In rare cases the compiler won't be able to resolve a location function. Like Thaum says in general the use cases are pretty niche
Faker
FakerOP4h ago
yep I see, I just stick in writing functions on their own, like no nested functions for now then, unless I have a use case?
Mayor McCheese
Basically The thing I want to really call out, is the initial answer you got was "wrong"
Faker
FakerOP4h ago
hmm in what sense pls
Mayor McCheese
I'm not saying their statements were wrong about the loop complexity, but rather that the reason you got the analyzer warning had nothing to do with that Your loop complexity could be perfect and you'd still get it
Faker
FakerOP3h ago
ah
Mayor McCheese
I'm not saying their advice was wrong, but that it didn't answer the question you had
Faker
FakerOP3h ago
yep I see basically the answer was to mark the "end" of the "outer" function, for readability like you said?
Mayor McCheese
ipso facto, if you went with the initial answer every time you saw the analyzer get triggered you'd potentially end up looking for a problem that didn't exist If that makes a lick of sense
Faker
FakerOP3h ago
yeah true
Mayor McCheese
Anyway, if you're happy with the answer(s) $close
MODiX
MODiX3h ago
If you have no further questions, please use /close to mark the forum thread as answered
Faker
FakerOP3h ago
Yep, thanks !

Did you find this page helpful?