3

I took a PHP test for NASA recently and thought I should have done better. I believe the issue is something I used to know about, but need a refresher in. I can't remember what it is called though. It is the concept that you have to check for null, empty, and edge-cases. No, it's not unit testing or anything like that. It is a programming technique involving conditionals, equality, and existence tests, and then handling error exceptions.

Does anyone know what the web-searchable, hash-taggable, technical term for this technique is?

3 Answers3

10

It's part of defensive programming, but that may encompass many other things:

Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances. Defensive programming practices are often used where high availability, safety, or security is needed.

Defensive programming is an approach to improve software and source code, in terms of:

  • General quality – reducing the number of software bugs and problems.
  • Making the source code comprehensible – the source code should be readable and understandable so it is approved in a code audit.
  • Making the software behave in a predictable manner despite unexpected inputs or user actions.

(source: Wikipedia)

Glorfindel
  • 3,167
3

If you are referring to a particular check, that is usually called a “guard condition”, if you are talking about using guard conditions that would be defensive programming.

Some languages have ways to embed particular types of such checks in the language, which is typically done through the type system. For instance C# allows you to create a variable where the type is a non-null string. Pascal allows you to declare a type where the underlying value is an integer between 18 and 120.

jmoreno
  • 11,238
2

If a function argument can be null, but is never supposed to be null, you have three choices: a. You ignore this and whatever happens, happens. B. You check and if the pointer is null you report an error, throw an exception, kill the program, whatever seems most appropriate. C. You check and if the pointer is null you muddle through as best as you can. C. would likely be called “defensive programming”

You decide on a case-by-case basis what to do. Usually you look at it from a higher level where you decide “if this pointer, which should never be null, ends up being null, what’s the best thing to do”. During development the best would often be to fall into the debugger so the developer can figure out why the pointer is null and fix the problem. In production you need to decide intelligently what to do. In many situations it’s better for a program to crash than to give wrong results.

PS. In C++, a null reference is undefined behaviour. For example "int* p = NULL; int& r = *p;". Since it is undefined behaviour, checking that an int& reference that your function received is a null reference won't necessarily work. The compiler can say "null references are undefined behaviour, therefore there are no null references, therefore a check if (&r == NULL) is assumed to always fail". In Java, references to objects can be nil so you can check for it.

Xcode + Clang in C and C++ have an interesting feature: Since they don't know if a pointer argument is allowed to be a null pointer or not, they assume that the caller passed in a non-null pointer to avoid gazillions of warnings. But once you check whether it is null, the compiler assumes it can be null (or why would you have checked?) So

if (p == NULL) printf ("Error, p is null\n");
*p = 1; 

will give you a warning. Without the p == NULL check, no warning.

gnasher729
  • 49,096