Invert "If" Statement to Reduce Nesting

Invert if statement to reduce nesting

A return in the middle of the method is not necessarily bad. It might be better to return immediately if it makes the intent of the code clearer. For example:

double getPayAmount() {
double result;
if (_isDead) result = deadAmount();
else {
if (_isSeparated) result = separatedAmount();
else {
if (_isRetired) result = retiredAmount();
else result = normalPayAmount();
};
}
return result;
};

In this case, if _isDead is true, we can immediately get out of the method. It might be better to structure it this way instead:

double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();

return normalPayAmount();
};

I've picked this code from the refactoring catalog. This specific refactoring is called: Replace Nested Conditional with Guard Clauses.

Best way to optimise an if statement in C#?

It's not a compiler optimization, it's an author optimization. It flattens arrow code.

VHDL invert if to reduce nesting

The strategy you suggest is called early return because you are returning from a function early,
before reaching its end. It can be done in VHDL and it is as useful as in other languages, but the downside is that it can only be used in subprograms. You cannot "return" from a process.

In theory, you could move the code from inside your process to a procedure, but it would not help achieve what you want. I suggest that you read section 6.3 - Concurrent Procedure Call Statements
from Ashenden's Designer's Guide to VHDL to understand the details. In short, there are many restrictions to how the wait statement can be used in a procedure.

Why does ReSharper invert IFs for C# code? Does it give better performance (even slightly)?

Updated Answer:

It's a code maintainability suggestion. Easier to read than nesting the rest of the code in an IF statement. Examples/discussion of this can be seen at the following links:

  • Flattening Arrow Code
  • Replace Nested Conditional With Guard Clauses
  • Code Contracts section "Legacy Requires Statements"

Original Answer:

It will actually run (very negligibly) slower from having to perform a
NOT operation.

So much in fact, some people actually consider that prettier way to
code as it avoids an extra level of indentation for the bulk of the
code.

Invert if statement to reduce nesting

A return in the middle of the method is not necessarily bad. It might be better to return immediately if it makes the intent of the code clearer. For example:

double getPayAmount() {
double result;
if (_isDead) result = deadAmount();
else {
if (_isSeparated) result = separatedAmount();
else {
if (_isRetired) result = retiredAmount();
else result = normalPayAmount();
};
}
return result;
};

In this case, if _isDead is true, we can immediately get out of the method. It might be better to structure it this way instead:

double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();

return normalPayAmount();
};

I've picked this code from the refactoring catalog. This specific refactoring is called: Replace Nested Conditional with Guard Clauses.

Replacing nested if statements

Well, not directly an answer to your question since you specifically ask about switch/case statements, but here is a similar question.

Invert “if” statement to reduce nesting

This talks about replacing nested if's with guard-statements, that return early, instead of progressively checking more and more things before settling on a return value.

C#: Nested conditionals vs continue statement

As a rule I have found it best to always start statement blocks with any conditions that will except out as it reduces complexity, but more importantly throws out non-compatible circumstances before they are stepped any further which can increase code and memory performance. This also ensures safety of your conditions over a duration through maintenance, that it's less likely to have invalid scenarios passed into code they don't belong in.

Plus I think the second of the two is more readable personally because you don't have the scope layers confusing what's available, it's easy to create a variable in one layer later down the road and not realize it's unavailable in another layer, or having to manage them to be modified appropriately etc.

This isn't just continue in loops, but this also refers to conditions of methods should return; instead of having a method start

if (valid)
{
do stuff;
}

it should always start

if (notValid)
{
return;
}

is there any specific reason why you wouldn't use nested if statements?

In general, the compiler will generate the same or nearly the same code for these two variants. For interpreted languages that support short-circuiting (not evaluating second part of an || or && when the outcome has been determined), it will also be pretty much the same thing.

My advice would be to use the form that represents what you want to do most clearly.

For example, if we want to check if two coordinates match:

if (x1 == x2 && y1 == y2) 

Buf if your code is something like this:

if (country_of_origin == "Sweden")
{
if (today == thursday)
{
...
}
}

may make more sense...



Related Topics



Leave a reply



Submit