Now You Know What I Think

14. February 2010 12:33
by nickolay_kolev

Fun with && and ||

14. February 2010 12:33 by nickolay_kolev | 1 Comments


I am surprised that only few people know and apply this little trick when it comes to checking for a NULL pointer.


You should check for a NULL every time you use new to get an object instance of a class. You should also check for a NULL pointer when you receive a non-value object as a function parameter. The later can be omitted only if you develop under the strict rules of programming by contract (a.k.a design by contract). But, I have never seen anybody develop using programming by contract, because it will actually require managers and architects to have a good idea what the software will have to do, which is never the case. ...


Even if they do, there are usually crazy time constrains, that will be used as an excuse for doing ad hoc design. Instead, project managers and architects opt for agile development. I will have another post about agile and who and when should use it.


Back to the fun with NULL pointers. Let’s say you have a function that receives a String.


private void foo(String str) {…}


You should make sure you have received a good parameter. Let’s say you need a string and NULL or empty string is not a good value for you. For this example we will return an error if the parameter is NULL or empty. You might choose to throw and exception instead.


if (str == NULL || str.isEmpty())


            return ERROR;


// do something important here


This check has one serious problem. You will get a null-pointer exception, if str is in fact NULL. But you new that, I expect.

So, what you do is usually you reverse your code in order to use the && operator to avoid the null-pointer exception:


if (str != NULL && !str.isEmpty())


            //do something important


return ERROR;


This is not a big change. The problem is that it somewhat moves the return of ERROR or throwing an exception down in your function body, instead of keeping the precondition check at the top of the function. It introduces additional "mental flow", when reading the code, which makes it harder to read. In case where your expression is more complex, it will require some serious thinking, to understand what is going on.


I like to have the precondition checks appear at the beginning of the function body and throw exceptions or return errors before I get to read the rest of the code.


So, here is the trick. It is called the De Morgan’s law (I thought it was called Morgan’s law – so, I learned something too). It states that:


A || B == !(!A && !B)

A&& B == !(!A || !B)


This means that if you want to keep the execution flow as required at the beginning, it should look like this :


//if (str == NULL || str.isEmpty())

if (!(str != NULL && !str.isEmpty()))


            Return ERROR;


// do something important


You might argue that this does nothing else, but add negation before the expression. This is what effectively you did. The difference is the intention (you applied the De Morgan’s law in the second code and it is clear what you did) and it also will help you with more complex expressions.


I haven’t seen anybody applying the De Morgan’s law in its pure definition. And I’ve seen a lot of code. The statement is usually changed to use && operator to avoid the exception, but without using the negation in front of it, which is just changing the logic, not applying the law.


It is your choice how you are going to write your code. I just thought this might be something interesting that you might consider the next time you create your if-statements. I will be glad if this posting helped you in some way to simplify your code.



Comments (1) -

I have got many articles but they didn’t had any fascinating or exclusive contents which I want to read. You have included all the pertinent details in this. I was penetrating for this kind of information. I like to refer your site to get excellent topics to read. Thanks a lot for sharing, take cheap essay writing service help to write a lengthy article. Approximately I have all your posts and this one is the best. This article has clarified the ideas in an interesting way. Keep the same worth in the rest of posts which is the best thing I have got from your articles. my site is:

Add comment