I am just imaging that the ambient color of a game is set in a catch part of every try in a game. The effect would be a ligntning if errors occur. Placed that in a cyberpunk genre... oh, maybe it allready happened...
That's kind of a cool idea. Iirc there was some old console game that loaded into a secret level upon a certain error because that was the easiest workaround to "fix" it.
Some consider it P Y T H O N I C to not do things like check if a file exists before reading it. They just try it and catch if it fails.
I don't have much respect for pythonicity to begin with, mostly because the name sounds so pretentious. If they had called it "idiomatic python" I probably wouldn't care. But I think that this idiom in particular is pretty stupid.
Its actually better to "ask for forgiveness" in python rather than "ask for permission" (try and catch, rather than if) if you think that the exception will not raise most of the time. Since 'if' costs more than a try except that doesn't raise anything.
Even more so in 3.11
“Zero-cost” exceptions are implemented. The cost of try statements is almost eliminated when no exception is raised. (Contributed by Mark Shannon in bpo-40222.)
I mean the performance discussion is not often relevant to Python in the first place but I fully expect that an if which almost always goes one way would get virtually eliminated by the branch predictor (unless it goes the other way, in which case it'll still be orders of magnitude faster than an exception).
Plus if you only catch in very rare situations that fits with the "exceptions are for exceptional behaviour" angle, which I think gives better code than look before you leap anyways, with the main caveat being security-relevant input validation.
If you always assume your code will run on a CPU with a branch predictor then you might as well skip the check and assume the file always exists /s
Okay, most people's code will run on a CPU with branch prediction but I've written a python script for a 486 within the past week so it's not always going to :P
Remember kids: this approach is only good in the best case, where the exceptions are rare to the point of nonexistence.
In a worst case scenario, which would likely happen if about 1 in ten items would rasie an exception, this would result in a massive reduction in efficiency.
Exceptions being raised are at least an order of magnitude slower than an if statement.
I'll be honest, I didn't see the purpose of things being "pythonic" when I started using Python a year or two ago. Coming from primarily C++, it seemed unnecessary. But I have learned to at least appreciate one aspect, which is that pythonic code all kinda reads the same. It's easier to understand what unknown code is doing at first glance if you already know the style, and by having style guidelines that apply to the entire language you can save quite a bit of time.
To be perfectly honest, I don't know anyone that follows pythonic coding practices to a T. If you don't it's not a big deal. It's just easier (sometimes) for maintainability and teamwork if you do.
I think a unified style is good. Especially when you look at two different C++ codebases and become amazed at how it can look like a completely different language. And by-and-large most pythonic practices are uncontroversial. But with any guidelines there are parts that are bad (or that I think are bad). Like with design patterns or any other guidelines, you should use them when it makes sense but don't tie yourself to something that doesn't help you because you saw it in a book. They're designed for solving a few problems, not to be thrown at everything just because you can.
Most languages have an implied "this is how we do things", which you learn if you spend long enough working with them and see enough good code written in them. Even C++ with its massive scope and lack of meaningful direction has many defined patterns, such as RAII, and smart resource handles.
If such a race condition is possible and has consequences that your app needs to avoid then yes, you may have a good reason to favour exceptions. I would consider race conditions exceptional and I think the adage "exceptions are for exceptional behaviour" is better than "look before you leap" anyways. Personally, if I had access to a race-resistant open_if_exists I would use it for clarity, even though it's probably implemented with exceptions or a sentinel return value.
File existence is one of the few TOCTOU vulnerabilities that can't be easily solved without exceptions (or checking a sentinel from the open function, like in C). In almost every situation you should open and acquire a lock on the file, after which you check permissions and things like "is this file secretly a symlink to something the user is not allowed to overwrite so they get me to overwrite it on their behalf". You shouldn't ever assume those guarantees if you haven't checked on a file you have exclusive write access to, and if your application needs those guarantees then you need a lock.
I don't think that a check on file existence before attempting to open it is bad per se, but if you have to handle race conditions gracefully you need try/catch, or depending on the language, to check the return value of open. You can do both if you want to only take the performance penalty of exceptions in the event of a highly unlikely situation, but arguably in most situations that's redundant, especially in Python which isn't really used for high-performance applications anyways.
Do you mean early returns or a return with nothing after it?
This first I don't find weird, although I know some disapprove. The second is just strange unless you come from C89 where a return 0 was needed on main.
Edit: Sorry I misread it as "using return at the end of void functions"
Didn't even C89 do this for void functions? I can't think of a language where the return isn't implicit for void functions except for assembly (where it might be expected to fall through because there's no difference between a label and a function).
Now implicit return on non-void functions is bad, with the exception of implicit return 0 on main in C/C++, but frankly it's kinda stupid that main returns an int when it almost always returns 0. Maybe it would have been better to require one use an intrinsic like exit(int exit_code) or a global exitcode field which could be set. Especially as that exit(int exit_code) intrinsic already exists and is already used when main returns.
I've never seen a structured language that didn't auto-return from functions (after all, what else could it even do). Even Algol and Fortran in the 60s already did this.
Too real 😔. Sometimes I’m just so tired of trying to debug something that sometimes works that I just say: “Fuck it, try catch statement. Push the error to my error log array”
Hearts and worldLight are probably both implemented in a way that guarantees this to remain inside the bounds. Looks like pretty standard code for a quick and simple game project.
The name "hearts" is more indicative of an old school life system where you have about 1-10 hearts (like in Super Mario Bros or Minecraft) that are each displayed as a separate symbol, rather than added up to an HP counter like in Counter-Strike.
You may want more colour changes than just a linear gradient, which makes it much easier to implement as a lookup table. Usually such systems have a kind of exponential progression as your life runs out (with different stages in different colours channels, for example by turning more black for the last two hearts) that's hard to fine-tune as a single formula.
Isn't all game code you write in Unity already inside a try-catch encapsulation? I remember fixing bugs for a game we were working on was a nightmare, because instead of just crashing, we got an explosion of bananas when we tried to access an already deleted fruit.
140
u/AlwaysNinjaBusiness Aug 01 '21
Ah yes. Error handling=ignore any errors thrown.
125
u/abrams666 Aug 01 '21
I am just imaging that the ambient color of a game is set in a catch part of every try in a game. The effect would be a ligntning if errors occur. Placed that in a cyberpunk genre... oh, maybe it allready happened...
196
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 01 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
16
u/[deleted] Aug 01 '21
Nice, the classic "this is fine" meme.
73
u/v_maria Aug 01 '21
python moment
5
u/Impossible_Average_1 Aug 01 '21
For rapid prototyping and game jams it is okay to fix the problem instead of its source.
3
u/yaxamie Aug 01 '21
Game Jam code is doing it wrong if it’s not “bad code” LOL
5
u/CSsharpGO evil Aug 01 '21
stfu UNITY
2
u/ilikedankmemes3 Aug 01 '21
Too real 😔. Sometimes I’m just so tired of trying to debug something that sometimes works that I just say: “Fuck it, try catch statement. Push the error to my error log array”
3
u/Knuffya Aug 01 '21
Even without the try-catch, this would be pretty bad code, assuming the player has a lot of "hearts"
1
u/[deleted] Aug 01 '21
Isn't all game code you write in Unity already inside a try-catch encapsulation? I remember fixing bugs for a game we were working on was a nightmare, because instead of just crashing, we got an explosion of bananas when we tried to access an already deleted fruit.
1
u/matyklug Aug 02 '21
I don't know C#, but uh, the need for a try catch there is pretty concerning.
1
u/Yangishrobin Aug 02 '21
Listen.
People get angry. I have many times told my editor to stfu. But what's important is that we learn from our mistakes
1
u/JDude13 Aug 02 '21
Can every language just add a decorator that lets the compiler/interpreter know to ignore list index errors?
1
u/JDude13 Aug 02 '21
Can every language just add a decorator that lets the compiler/interpreter know to ignore list index errors?
1
u/Oracuda Aug 07 '21
lmao ive done this
0
u/[deleted] Aug 01 '21
[deleted] — view deleted comment
0
u/[deleted] Aug 01 '21
[deleted] — view deleted comment