✅ Separate local function with explicit 'return' statement
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?
21 Replies
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.
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 ?
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.
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.
Unknown User•9h ago
Message Not Public
Sign In & Join Server To View
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.
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•4h ago
Message Not Public
Sign In & Join Server To View
ahhh I see
yep noted, thanks !
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
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?
Basically
The thing I want to really call out, is the initial answer you got was "wrong"
hmm in what sense pls
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
ah
I'm not saying their advice was wrong, but that it didn't answer the question you had
yep I see
basically the answer was to mark the "end" of the "outer" function, for readability like you said?
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
yeah true
Anyway, if you're happy with the answer(s)
$close
If you have no further questions, please use /close to mark the forum thread as answered
Yep, thanks !