I have been wondering for some time why does everyone check everywhere if a pointer is null before using it, here is an example from the tuto I’m was following:
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("BeginPlay"));
The thing is, besides it’s often less than unlikely that these tested-pointers would be null, also, of course the application will crash if it’s not checked, but isn’t it easier to crash and immediately know what’s going on rather that having this WHY-IS-NOTHING-HAPPENING feeling ?
can’t imagine the mess with an huge code base if something start to not working for no apparent reason, and then have to use the debugger “if” by “if” in the whole application to finally figure out which pointer is now null…
Even if it’s not really unreal engine related, my question is, why the hell don’t you let it crash ?
Even if it’s not really unreal engine related, my question is, why the hell don’t you let it crash ?
It makes for a very bad user experience for starters.
That’s kinda okayish for development but not ideal for production. It could be possible that it’s never null for you but a user finds some edge case that causes your pointer to be null.
If you needed that pointer to be valid and it’s not, then you should handle that condition. That should mitigate any weird bugs. Look at what you’re doing with the pointer and judge what the implications are. In your example it’s not a big deal - you can just log to a text file or just skip it. In other cases you may need to gracefully fall back - for example don’t spawn a character and play an error sound.
You should always check pointers that are passed in or ones you get from calling a function - eg GetWorld(); You just never know what their state is frame to frame.
There will be times when you’re certain it’ll be a valid pointer (ie created an object in your class fully controlled by you). UE4 doesn’t check them all. UE4 also uses asserts in some areas.
Anyway, ultimately crashing makes for a terrible user experience. Gracefully handle null pointers.
Would you put up with the UE4 editor crashing randomly because that’s how Epic catches bugs?
Checking GEngine pointer in game-code is useless, since engine will crash much earlier before check point.
How Bino noted, that’s an awful way to catch bugs - just let it crash. You should always check pointer that passed to the function, or the one that was cast to another type and so on, since you can’t be sure it’s not null.
Regarding debugging and nothing happening feeling, that’s why you don’t simply check pointer but print some information and have an external way to quit execution, instead of crashing.
Well, there are better ways than just checking for nullPtr’s.
You could use the Nullobject-pattern or exceptions as an elegant way to deal with the problem and actually provide feedback to the developer or the user if needed.
BUT YOU NEVER EVER WANT TO ACCESS UNCHECKED POINTERS! Nothing is worse than reproducable crashes that only happen on 1-2 system setups. If you failed to test this specific setup, you practically ship a game that could just crash on every non-tested computer/setup.
Belief is not knowledge, however. If the API contract states that a given value can be null, then it can be null at any time, and you have to check for that. Even if you think you know that it can’t be null at a given point in time, because you cannot be certain that that behaviour will stay that way in subsequent revisions.