Is variable assignment expensive compared to a null check? For example, is it worth checking that foo is not null before assigning it null?
if (foo != null)
I wouldn't worry about it - it's just extra lines of code to maintain. This is the sort of micro-optimization you should never do unless you have lots of documented proof that it's your bottleneck.
First of all, it's micro optimization. So no need to worry much about it.
But to answer your question, you need to reduce it to just one line. (as all your code does is to set it to NULL).
foo = NULL;
Reasons being that,
Comparison is a much much costlier operation than assignment. (As comparison eats up many assembly instructions comparatively. Generally a subtraction and a comparison to zero or an XOR and comparison to zero). Assignment takes up fewer instructions.