Consider the following code sample:
private void AddEnvelope(MailMessage mail)
{
if (this.CopyEnvelope)
{
// Perform a few operations
}
}
vs
private void AddEnvelope(MailMessage mail)
{
if (!this.CopyEnvelope) return;
// Perform a few operations
}
Will the bottom code execute any faster? Why would ReSharper make this recommendation?
Update
Having thought about this question the answer might seem obvious to some. But lots of us developers were never in the habit of nesting zounds of if
statements in the first place...
INVERT IF STATEMENTS and EARLY RETURNS to improve your code readability. Inverting if statements and early returns are two techniques that can be applied that can improve your codebase. When used properly it can reduce indentation and complexity.
Place your cursor in an if or if else statement. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Invert if.
Position the caret over an if-else statement. Press Alt+Enter. From the pop-up menu, select Invert if-else statement.
It doesn't matter. Stop agonizing over performance issues that don't exist - use a profiler to identify areas in your code that DO exhibit issues, and fix them. Proactive optimization - before you know that there is a problem - is by definition a waste of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With