Opinion about the one-liner return statement to check for nullptr

First of all, congratulations for your video. It’s very easy to follow!

I’m watching Lecture 92 and I’m at a point where you suggest we use this:
if (!PhysicsHandle) { return; }

I just wanted to say that in my opinion it’s not a very good practice. Putting return statements in the middle of a function kind of messes with control flow, and makes functions difficult to understand and/or debug.
Personally, I believe that organizing the code better, with carefully placed control flow statements (if-else) is a better way to go.

Thoughts? :slight_smile:

2 Likes

These are pretty short functions. It’s fairly easy to tell what’s happening. I personally wouldn’t do it this way either. I’d probably either use if-else like we did with the grabber’s input component or instantiate a locally scoped variable to alter and return.

1 Like

Agreed, messing up with the cpu pipeline is not a good thing :slight_smile:

This is an area where they are using a common pattern from other languages that’s not a good one for c++. Returning early in interpreted languages saves execution time because less statements have to be run, and there is no problem to have multiple returns. (It’s also just plain convenient)

But I guess C++ can do some optimizations with the return and it’s not good to have multiple returns or have them too early where some code paths are not visited.

Privacy & Terms