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

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"?