Just in case anyone needs to know why this is bad:
Never use exceptions for control flow (unless absolutely necessary because the API you're using doesn't offer a way to check the precondition yourself and avoid the exception). This is because exceptions and exception handling are very expensive. Additionally it can leave objects in undefined/broken states.
Exceptions really aren't that expensive on their own. It is extremely expensive however when you decide to use catch(type) everywhere rather than a return object with say an evaluated boolean for instance.
Don't do it because it can cause real control flow issues when you forget to catch it, or because that's simply not what exceptions are good for and other code is more clear. The performance is a minor factor imo.
Exceptions always cause stack unwinding. They are always expensive. The stack unwinding stops at a catch and may continue on from there if throw is used again.
If you’re writing a small app, sure they aren’t “that expensive” if you want to notate it that way. But exceptions always costs hundreds and hundreds of CPU cycles that don’t have to be there at all if avoided when possible.
I once ended up memoizing the results of Int.Parse because I hadn't heard of Int.TryParse, the code was sluggish, and the profiler said most of the CPU time was in the catch block. This was for a one-off project, but it would've been reused if my conference submission had been accepted. (Whether I would've finished the paper before karoshi-ing is another matter.)
I'm not talking about retrieving error codes (that's also error based control flow btw). I'm talking about preventing errors from happening in the first place. Avoiding calls that error
I am not convinced. Docs for msvc, a popular c++ implementation state:
The exception mechanism has a minimal performance cost if no exception is thrown. If an exception is thrown, the cost of the stack traversal and unwinding is roughly comparable to the cost of a function call.
I know there are purists who "NEVER CATCH EXCEPTIONS EXCEPT WHEN REALLY NEEDED, ALWAYS CHECK CONDITIONS".
I don't really agree with that.
Yes, checking if b is zero in a / b is the way to do it, but there are valid usecases for exceptions. This is one of them, the "let it fail" approach, especially when checking the conditions can be complex.
There are even programming languages that encourage exceptions in some situations, like Python.
Also, for those bringing up that exceptions cause stack unwinding and it's a heavy operation and yada yada, you've probably got Electron apps like Discord or Slack or Microsoft Teams or Visual Studio Code installed. It's 2021, we're no longer writing stuff in assembly and micro optimizing. If performance is such a big deal that not throwing exceptions is important, you probably already know enough to know this. For most cases though, it's rather insignificant.
My takeaway is: when it makes writing code easier, but especially if code is still easily readable (or even more readable), just catch exceptions. Don't overuse it though. Returning a boolean is still often better than exception handling.
A) move and then trim down to the min/max, ie: new position = clamp( old position + direction, min position, max position )
B) check before you move, ie: if can move in direction then new position = old position + direction
If you chose B then you can do state based behavior, and fully avoid if's in favor of switches, so you have a collision state that you update after moving (if you move left and end next to a wall, you toggle the flag for left wall, same if you move away), and then when you press a move key each state has a defined behavior (move or not move). It sounds a lot more involved than it actually is, and it's probably still too overenginnered for a snake game, but good practice for "branchless" stuff lol.
22
u/[deleted] Dec 30 '21
Fuck I made a typo, supposed to be *against*
67
u/seeroflights Dec 30 '21
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
38
u/Lorddragonfang Dec 31 '21
I mean, this would be considered perfectly idiomatic Python code, so I have a hard time calling this truly "bad"
159
u/TheBrainStone Dec 30 '21
Just in case anyone needs to know why this is bad:
Never use exceptions for control flow (unless absolutely necessary because the API you're using doesn't offer a way to check the precondition yourself and avoid the exception).
This is because exceptions and exception handling are very expensive. Additionally it can leave objects in undefined/broken states.
28
u/gay_for_glaceons Dec 31 '21
20
u/kbruen Dec 31 '21
Not that bad.
I know there are purists who "NEVER CATCH EXCEPTIONS EXCEPT WHEN REALLY NEEDED, ALWAYS CHECK CONDITIONS".
I don't really agree with that.
Yes, checking if
bis zero ina / bis the way to do it, but there are valid usecases for exceptions. This is one of them, the "let it fail" approach, especially when checking the conditions can be complex.There are even programming languages that encourage exceptions in some situations, like Python.
Also, for those bringing up that exceptions cause stack unwinding and it's a heavy operation and yada yada, you've probably got Electron apps like Discord or Slack or Microsoft Teams or Visual Studio Code installed. It's 2021, we're no longer writing stuff in assembly and micro optimizing. If performance is such a big deal that not throwing exceptions is important, you probably already know enough to know this. For most cases though, it's rather insignificant.
My takeaway is: when it makes writing code easier, but especially if code is still easily readable (or even more readable), just catch exceptions. Don't overuse it though. Returning a boolean is still often better than exception handling.
3
u/nyanpasu64 Dec 31 '21
Please don't try this in C++.
3
u/xSilverMC Dec 31 '21
I mean, it looks like it runs, so that's something.
I'd probably check if the new position would be valid in the update method, but I can't fault anyone for just doing what works
2
u/DidiBear Dec 31 '21
EAFP: Easier to ask for forgiveness than permission
1
u/suppow Dec 31 '21
You can:
A) move and then trim down to the min/max, ie:
new position = clamp( old position + direction, min position, max position )B) check before you move, ie:
if can move in direction then new position = old position + directionIf you chose B then you can do state based behavior, and fully avoid
if's in favor of switches, so you have a collision state that you update after moving (if you move left and end next to a wall, you toggle the flag for left wall, same if you move away), and then when you press a move key each state has a defined behavior (move or not move).It sounds a lot more involved than it actually is, and it's probably still too overenginnered for a snake game, but good practice for "branchless" stuff lol.