using short circuit evaluation to avoid pointer errors

Started by
7 comments, last by Zahlman 17 years, 9 months ago
Hello, I was wondering if it's okay to have expressions such as this: (this.current != null && this.current.index <= this.head.prev.index) you see if I would write the above inverted like this: (this.current.index <= this.head.prev.index && this.current != null) this.current might be null and thus an exception will be thrown. All I'm doing is taking advantage of short circuit expression evaluation and it works great. I'm just wondering if it's allright to do something like this, such that it will always work. By the way I'm using Java as my programming language.
Advertisement
I can't say if Java promise left-to-right evaluation of logical and expressions, but IMO you shouldn't do it like that. Can't you split it into two tests? Your code will just look confusing to someone reading your code. Also since this.current != null is a prerequisite for this.current.index <= this.head.prev.index, I think you should seperate them.
"... pointer errors...using Java..."
Java doesn't have pointers like C and C++.(this doesn't mean it doesn't use pointer).
That is a standard idiom in C / C++, it is guaranteed to act like you expect, it is used frequently and therefore any C / C++ coder beyond 1-2 years experience will read such code without even having to think.
Quote:Original post by Xai
That is a standard idiom in C / C++, it is guaranteed to act like you expect, it is used frequently and therefore any C / C++ coder beyond 1-2 years experience will read such code without even having to think.


Quote:By the way I'm using Java as my programming language.
LOL ... man I'm just not quite there ... I mean I reread the post even ... damn those invisible words.
Quote:Original post by CTar
I can't say if Java promise left-to-right evaluation of logical and expressions

It does, IIRC it's identical to C/C++, at any rate the code shown is valid and will always do what you expect.

Whether it's readable, and whether you should be testing for null is another matter. Generally testing against null means you've got some design kinks that need working out. In this case it looks like some kind of linked list structure so it's probably ok.
Yes, that will work fine.
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94
Quote:Original post by Anonymous Poster
"... pointer errors...using Java..."
Java doesn't have pointers like C and C++.(this doesn't mean it doesn't use pointer).


But you do still frequently have to check things for null-ness. Although as correctly pointed out, you should try to avoid that as much as possible with careful design, which may (but needn't) include certain design patterns.

Anyway, yes, it is also "a standard idiom in Java, it is guaranteed to act like you expect, it is used frequently and therefore any Java coder beyond 1-2 years experience will read such code without even having to think.". Specifically *because* it was a C / C++ idiom. The Java implementors kept short-circuit evaluation in place because it's expected in a language that *looks* so much like C or C++ (i.e. Algol syntax).

Anyway, the only time you'd want the *other* behaviour is if calculating the second expression has side effects that you want to happen regardless, and that expectation is at least as evil as the conditional evaluation that you currently have. So most languages with the concept of binary logical operators do this (or something equivalent), Java included.

Edit: I really, really hope you're not seriously writing your own linked list implementation there. Use the standard library.

This topic is closed to new replies.

Advertisement