Jump to content
  • Advertisement
Sign in to follow this  
subflood

using short circuit evaluation to avoid pointer errors

This topic is 4374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"... pointer errors...using Java..."
Java doesn't have pointers like C and C++.(this doesn't mean it doesn't use pointer).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
LOL ... man I'm just not quite there ... I mean I reread the post even ... damn those invisible words.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!