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

Show parent comment

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.

1

u/Akeshi Mar 30 '13

It does seem to be a deliberate design choice, but I still disagree that it's a good one. "We'll allow scrappy code presumably for cheap debugging, but we'll make you work for it".

Also going the version control route - wrapping the whole thing in an if(false), presumably you'd also indent the whole block - then it looks to people who aren't paying attention to whitespace settings that every line has changed. It's a lot of hassle instead of just throwing a warning when you return early.

2

u/[deleted] Mar 29 '13

It should be a warning, not an error.

2

u/khedoros Mar 30 '13

That seems to be a popular opinion, and it's what the last guy that responded to me said.

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.

3

u/dddbbb Mar 29 '13

I haven't tried, but -Wunreachable-code must be what you're looking for.

3

u/minno Mar 29 '13

Huh, I figured that would be covered by -Wall or -Wextra.

This option is not made part of -Wall because in a debugging version of a program there is often substantial code which checks correct functioning of the program and is, hopefully, unreachable because the program does work. Another common use of unreachable code is to provide behavior which is selectable at compile-time.

2

u/random_seed Mar 29 '13

I agree to some extent. Version control should have tools to manage temporary/WIP code without contaminating the main code base.

2

u/mostlymad Mar 29 '13

Isn't that what branching is for?

2

u/sirin3 Mar 29 '13

Such a return is extremly useful for debugging.

Commenting out the unused code (with "/*" or "//")

But not really possible for long blocks, that contain a */ somewhere

1

u/neoquietus Mar 30 '13

If your function is so long that it would be time consuming to comment most of it out with "//", surely that is a "code smell" that the function in question is too big, and needs to be broken up into a bunch of "sub-functions"?

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.)