A stupid bug I wrote...

Wrote this code:


			target.set(position).add(direction);
			up.set(0, 1, 0);
			if(direction.x == 0 && direction.y == 0);{
				up.set(0, 0, 1); //Ensure up is not the same direction as "direction"
			}

			System.out.println(up); //Bug: up is always (0, 0, 1).

			viewMatrix.setToLookAt(position, target, up);

Neither direction.x and direction.y were 0, but up was still set to (0, 0, 1). What was the problem?

Answer:
[spoiler]
There’s a semi-colon right after the if()-clause. The code is basically this:

if(…);

{
up.set(0, 0, 1);
}

It’s an if-statement without a body. The curly braces are unrelated to the if-statement and don’t really do anything here.
[/spoiler]

Not sure if that was obvious to everyone else, but it sure took me some debugging to find that… .___.

well: there is an angry face in the code:

;{

always delete the angry faces!

Don’t you just wish you had a debugger in your brain?

This won’t really work. Comparing floating point values with ‘==’ or ‘!=’ should have a very high stink value. LookAt formulations degenerate as the direction approaches linear dependent (so approaching same and approaching opposite).

I’m not sure, but I think there might be possible for Eclipse to teach him, to warn about angry faces. Unless, they are in regular use as well, then it’d be annoying.

The direction is manually set, so it’s very likely to be hardcoded to exactly straight down. I haven’t had any precision problems with it so far, but yeah, might have to add a tolerance area.

IDEs like NetBeans & Eclipse warn about this…

Weird, my Eclipse doesn’t…

You probably have to enable it in some settings

My Eclipse and Netbeans don’t either. I use Netbeans at school and Eclipse at home and never once has either IDE warned me about frowny faces.

There is no such warning for eclipse.
There ought to be D=

Why can I just open curly brackets ? Why is this allowed at all ? =/

IntelliJ warns about it, saved me a couple of times. Don’t know about the rest of you, but even when writing text I tend to end lines with semicolons… :slight_smile:

There is a plugin called “FindBugs” that warns for it (among other potential issues like forgetting to close a stream). :slight_smile:

Mike

That is a funny bug that happens to me as well. I have Eclipse and Intellij, and I don’t get warnings for it either. Gonna start searching for some now :stuck_out_tongue:

Is there really any practical use for [icode]if(true);[/icode] that I’m not aware of? ??? (Maybe it was just in there to troll debugging programmers :smiley: )

My code formatter (which executes every time I save), would have changed:


         if(direction.x == 0 && direction.y == 0);{
            up.set(0, 0, 1); //Ensure up is not the same direction as "direction"
         }

to:


         if(direction.x == 0 && direction.y == 0);
         {
            up.set(0, 0, 1); //Ensure up is not the same direction as "direction"
         }

which would cause the alarm bells to ring immediately :slight_smile:

NetBeans:

The eclipse warning for this will be “Empty flow-control statement”.

Yes: debugging. Otherwise languages that are too strict about potential problems are quite annoying…try turning all all warnings that your IDE’s compiler supports.

Curly brackets start a new scope. I’ve seen only a few times, this kind of scoping is used within a method body. As one would guess, You can live without it.


public static boolean TRUE  = Math.random() < 1.0;
public static boolean FALSE = Math.random() > 1.0;

if(TRUE) {
   // stuff
}
else {
   // dead code, but no warning ^.^
}

Surprisingly useful, and allows you to make your compiler/ide as strict as possible.

Lol, I read that as “makes your booleans truer”. xD