Avoiding null pointer exceptions in a large c++ code base -
I inherited a large C ++ code base and I have the task of avoiding any null pointer exception The code can be in the base. Are the static analysis tools available, I'm thinking that you have successfully used
what other things can you see?
You can start by terminating the source of zero:
if (error) {return tap; }
In
if (error) {returnOffice; // Ex: An empty vector}
If the default objects return and your code base already uses an exception, then
if (Error) {BadThingHappenedException; }
Then, add handling to the appropriate places.
If you are working with a legacy code, you can create some cover functions / classes:
result type * new_function () {ResultType * result = Legacy_function (); If (result) {return result; } And {BadtingHappedExation;
The new functionality should start using new functions and should be handled with the appropriate exception handling.
I know that some programmers do not get exceptions, smart people like, but what is happening with no return is that the tap passes like crazy because everyone thinks That it is not their business and it is not quietly to return. Some functions can return the error code, which is fine, but the caller often ends up responding to errors, until each other returns to Null. Then, you see lots of tap-checking in each single function, no matter how trivial the function is. And, this is the only place that does not check the tap to crash the program. The exception forces you to think carefully about the mistake and decides how and how it should be handled.
It looks like you're just looking at the static solution like the static analysis tool (which you should always use). Changing the signal in the context is also a great solution. However, there is the beauty of Rai in C ++, which eliminates the need to "try {} end {}" everywhere, so I think it's worth your serious thinking.
Comments
Post a Comment