Which one out of following two is best wrt to performance and standard practice. How does .NET internally handles these two code snippets?
Code1
If(r
If I had to say, I would say the better practice is the if-else rather than the "implied" else. The reason being is that if someone else modifies your code, they can easily catch it by glancing over.
I recall that there is a large debate in the programming world on whether or not you should have multiple return statements in your code. One could say that it's a source of great confusion because if you have multiple loops within the "if" statement, and have conditional returns, then it could cause some confusion.
My usual practice is to have an if-else statement and a single return statement.
For example,
type returnValue;
if(true)
{
returnValue = item;
}
else
returnValue = somethingElse;
return returnValue;
In my opinion the above is slightly more readable. However that isn't always the case. Sometimes it's better to have a return statement in the middle of the if statement especially if it requires a tricky return statement.