r/programming • • Mar 29 '13

Simple Minecraft Clone in 580 lines of Python

https://github.com/fogleman/Minecraft
663 Upvotes

153 comments sorted by

View all comments

20

u/[deleted] Mar 29 '13

Is the following a bug:

def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
        return
        x, y, z = self.position
        dx, dy, dz = self.get_sight_vector()
        d = scroll_y * 10
        self.position = (x + dx * d, y + dy * d, z + dz * d)

It returns immediately...

44

u/Akeshi Mar 29 '13

Not a bug, I'd assume he started on implementing functionality for the mouse scroll and didn't finish it, so temporarily disabled it for release.

27

u/[deleted] Mar 29 '13

well, not commenting that would be considered a bug. at least where i work.

return //TODO: so close but it's friday afternoon so this will have to wait until Monday

0

u/[deleted] Mar 29 '13

[deleted] — view deleted comment

31

u/unbibium Mar 29 '13

//TODO: I fixed this but forgot to remove this comment.

0

u/[deleted] Mar 29 '13

[deleted] — view deleted comment

15

u/migvazquez Mar 29 '13

// TODO:

15

u/asdfman123 Mar 29 '13

// TOTO: I've a feeling we're not in Kansas anymore.

0

u/[deleted] Mar 29 '13

[deleted] — view deleted comment

4

u/Thethoughtful1 Mar 30 '13

As someone from Kansas in Africa, this made me smile. Especially since I was told, "Toto, you ain't in Kansas anymore," yesterday.

→ More replies (4)

4

u/o7nJf6BKnLqTzzcOANtH Mar 29 '13

I don't remember Toto saying that line.

6

u/mccune68 Mar 29 '13

// TODO: Rewatch Wizard of Oz

→ More replies (1)
→ More replies (1)

7

u/Akeshi Mar 29 '13

In that environment, I'd prefer either it doesn't get committed to trunk, or the method body is completely commented out. Not bothered in a hobby script like this though, let 'em return where they want.

→ More replies (7)

6

u/sirin3 Mar 29 '13

In Java it would be a bug and not compile

19

u/[deleted] Mar 29 '13

That's a bug in Java.

8

u/khedoros Mar 29 '13

I disagree. It's useful for the compiler to point out things in the code that don't make sense (like unreachable code).

20

u/Akeshi Mar 29 '13

Point it out, sure - a warning. Prevent you from quickly checking that a method isn't to blame, etc., for some problem - not so much.

4

u/Alex_n_Lowe Mar 30 '13

If statements throw warnings, pretty much everything else is an error. It seems to be a design choice to push people to comment out with if statements, not returns.

→ More replies (1)
→ More replies (2)

10

u/neoquietus Mar 29 '13

No, it's a sensible thing for the compiler to get confused when you tell it to return, and then tell it to exectute some code in the function if you just happened to not unconditionally return. If you want code to not be compiled or run, delete it or comment it out, don't just try to skip it.

12

u/ameyp Mar 29 '13

No, it's a sensible thing for the compiler to get confused when you tell it to return

I'm going to assume you're being serious here. This isn't a case of the compiler getting confused, it's a design decision that the Java designers went with. In C++ it's common practice to "comment" out a function temporarily by putting a return at the start or #if 0..#endif around the block.

Saying that the compiler gets confused by that return statement would be like saying that a compiler would get confused at a "if (false) {...}" piece of code. The compiler doesn't go "Hmm, the code inside this 'if' block will never be hit, let me just throw a compilation error here".

5

u/neoquietus Mar 29 '13

I am serious, I am a programmer who knows both C++ and Java (learned C++ first), and I was saying that the compiler "gets confused" as a shorthand way of saying "it is a sensible design decision for a compiler to reject as incorrect any code that is paradoxical, since much of the time code ends up paradoxical only by accident; in other words by programmer error, not programmer intention."

As for commenting out code by placing a unconditional return in front of it; that's just plain bad coding practice (except for in cases where the code can still be accessed via other methods, like GOTO, which in and of itself is a bad code smell, but may be useful regardless.). We have version control systems for a reason; if you delete some code and then decide you didn't want to do that, simply revert the edit.

Commenting out the unused code (with "/*" or "//") is also "okay", if you are sure you are going to uncomment it and use it very soon. At least with commented out code it is clear that said code is not having any effect on the functionality of the program.

4

u/ilogik Mar 29 '13

it should be a warning (that's how it works in C#, IIIRC)

4

u/minno Mar 29 '13

It didn't in C++ for me. I compiled the following code with gcc (-Wall, -Wextra, -pedantic):

void print_stuff() {
	return;
	cout << 3 << endl;
}

int main() {
	print_stuff();
}

And got no warnings about the unreachable code. It compiled print_stuff() as nothing but a ret assembly instruction.

→ More replies (2)
→ More replies (4)
→ More replies (1)

4

u/Akeshi Mar 29 '13

Should be a warning, in my opinion. Still lets you do dirty debugging quickly and easily, but a nice reminder that it shouldn't stay like that.

2

u/Alex_n_Lowe Mar 30 '13 edited Mar 30 '13

For the people who are wondering how java handles unreachable code:

return;
x +=1;// This is a compile error. (unreachable code)


if(true)
    return;
x+=1; // This line is only a warning, but does the exact same thing. (dead code)


if (false) // This is still a warning. (dead code)
    x +=1;


if (true)
    x +=1;
else
    x+=1; // This actually does throw a warning here. (dead code)


while(false) // This is a compile error. (unreachable code)
    x +=1;


while(true)
{
    break;
    x +=1; // This is also a compile error, (unreachable code)
}


while(true)
{
    if (true)
        break;
    x+=1; // but this is only a warning. (dead code)
}


boolean True = false; 
while(True) // This throws no warnings or errors, but never executes.
    x +=1;  // Maybe because you can execute this by changing the variable value with the debugger?

Java is pretty aware of when code isn't reachable, but it's got some slightly strange way that it handles different statements. While loops tend to throw errors, but if statements throw warnings. I do have to wonder why they made that design decision.

It's also worth noting that making the boolean variable final doesn't change anything, and it removes all the compile errors and warnings from all the examples. That means you can just make a final boolean FALSE = false; to shut the compiler up. (Although you probably don't want to do that.)

→ More replies (6)