if->return vs. if->else efficiency

PerformanceReturnIf Statement

Performance Problem Overview


This may sound like a silly question, and I hesitated to post it, but still: if something needs to run only in a certain condition, which of these is more efficient:

A.

if (condition) {
   // do
   // things...
}

B.

if (!condition) { return; }
// do
// things...

Performance Solutions


Solution 1 - Performance

They are equally efficient, but B is usually considered to give better readability, especially when used to eliminate several nested conditions.

Solution 2 - Performance

Please pick the thing that is most readable. Performance optimizations at this level are hardly ever an issue. Even really performance sensitive parts of frameworks (such as the .NET framework) do not benefit from such an micro optimization.

Solution 3 - Performance

It's a style thing. The performance is not relevant; both produce nearly identical machine code.

A few considerations on the style:

If you want to avoid 'horizontal programming', you might want to prefer B to avoid nested conditions. For example, if you want to add exceptions without affecting the flow of the method too much:

A:

public String getDescription(MyObject obj) {
    if (obj == null) {
        return "";
    } else {
        if (!obj.isValid()) {
            return "invalid";
        } else {
            ...
        }
    }
 }

B:

public String getDescription(MyObject obj) {
    if (obj == null) {
        return "";
    }

    if (!obj.isValid()) {
        return "invalid";
    }

    ....
 }

But the difference is minimal if you ask me. Definitely not worth a 'code style war'.

Solution 4 - Performance

The real question is, should you really care?

I say NO! It's more important to have better readable code than doing some micro-optimization.

Solution 5 - Performance

While I agree that you should choose readability first, I'll go ahead and add a little info: In C#, there is no difference. It compiles to the same thing (when optimized by building Release mode). Other languages? Who knows, I'm sure some of them consider it different, but the chances that you actually need to be concerned about it is slim to none.

Solution 6 - Performance

Performance is more or less the same in both the cases. Therefore it becomes more of a style or preference question.

I personally prefer writing if--> return i.e. case B because it makes the code to look cleaner and easy to read specially if the code comprises of complex nested conditions.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuval A.View Question on Stackoverflow
Solution 1 - PerformanceKlaus Byskov PedersenView Answer on Stackoverflow
Solution 2 - PerformanceStevenView Answer on Stackoverflow
Solution 3 - PerformanceThe NailView Answer on Stackoverflow
Solution 4 - PerformancePeeHaaView Answer on Stackoverflow
Solution 5 - PerformanceTim S.View Answer on Stackoverflow
Solution 6 - Performanceuser3260035View Answer on Stackoverflow