For context, I am trying to do a save system for a game.

0 points

I would use both.

permalink
report
reply
1 point

I would explicitly check in this case - but this might not be the right way if you were doing something else, like updating a row in a table millions of times a second. It’s always context dependent.

permalink
report
reply
5 points

You should do both. Creation and catching of exceptions can be an expensive process and even if the check for the file succeeds the loading of the file may not in which case you’ll need to handle the exceptions anyway.

permalink
report
reply
3 points

If you’re already using an existing engine this is already baked in.

But a new engine I would suggest confirming that the file is there and that there is no handles holding it.

permalink
report
reply
3 points

I saw long time ago from somewhere saying that handling exception is expensive in terms of stack operations. To avoid the unexpected I guess you should do both, but a check before loading just saves you from unnecessary exception handlings which, if the very first statement is indeed true, would harm the performance.

permalink
report
reply
2 points
*

It is an expensive operation as it needs to unwind the stack to get the stack trace.

But if you’re checking a single file you won’t notice it.

If you’re doing it in a tight loop it’ll be very noticeable.

Checking the file exists also has a cost.

I likely wouldn’t bother if I was saving a single file especially as there are other exceptions that could be thrown in the process.

permalink
report
parent
reply

Learn Programming

!learn_programming@programming.dev

Create post

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don’t make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don’t ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you’re trying to solve. Don’t focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

Community stats

  • 2

    Monthly active users

  • 102

    Posts

  • 481

    Comments