I write a lot of software to interact with APIs. These APIs provide data usually in the form of an XML or JSON document. These documents are suppose to follow a spec and structure.
In practice however, this can be difficult to code for. Specs may change, fields may be renamed, moved, dropped, or made circumstantial.
Is it good or bad practice to always verify that a data field exists. Such as the following.
$first_name = isset($data['first_name']) ? $data['first_name'] : '';
This can make code much more difficult to read, and may need even more code in certain circumstances (such as when parsing XML attributes or XML with cdata used). Then there's also the case of validating the correct datatype.
Is it appropriate to trust an API spec, or should code check every data object that it itself didn't create?