Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see a few problems with your function; first, you don't declare <code>gobj</code> within the method body. You made a comment elsewhere that makes me think that you're trying to <code>return</code> a data member of the class; that's odd design, to say the least. (Normally, the goal is to have each method stand on its own as much as possible. If you're sharing data like this, you risk introducing bugs that are difficult to find later.)</p> <p>Your function does not <em>always</em> return data -- sometimes it'll just fall off the end of the function without an explicit <code>return</code> statement. <em>You</em> might know that one of those four conditions will always be true, but the compiler does not -- and honestly, it'll be correct as you'll inevitably break that condition in the future.</p> <p>This code is difficult to write in a way that makes it more beautiful. (Well, if Java's <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html" rel="nofollow"><code>||</code> operator</a> worked with <code>null</code> as well, this could be a very beautiful short function. But Java's <code>||</code> only works with <code>true</code> and <code>false</code>.)</p> <p>Probably the easiest way to re-write the function is to remove the guard after the last attempt:</p> <pre><code>private GObject getCollidingObject() { GObject gobj; gobj = getElementAt(ball.getX(),ball.getY()); if (gobj != null) { return gobj; } gobj = getElementAt(ball.getX()+BALL_RADIUS,ball.getY()); if (gobj != null) { return gobj; } gobj = getElementAt(ball.getX(),ball.getY()+BALL_RADIUS); if (gobj != null) { return gobj; } gobj = getElementAt(ball.getX()+BALL_RADIUS,ball.getY()+BALL_RADIUS); return gobj; } </code></pre> <p>This way, you'll execute at least one <code>return</code> no matter which path you take through the function.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload