Is it possible to use a lot of `try`, and not more if``s`?

In my game, a lot of things are checked, and often the indexes are outside the borders, and this is usually normal if sometimes things are not drawn or checked. My code is currently fairly simple to read, although when debugging the game loses a frame or two due to exceptions that are logged. However, it works great outside of VS. When I tried to avoid exceptions using many and many if , this greatly reduced the readability of the code and removed the low fps loss. I wonder if he has any other effects.

+4
source share
4 answers

The throw exception is very resource intensive - it is much faster to use if / then instructions or other "normal" flow control.

Exceptions are mostly expensive, because to throw an Exception instance, you need to execute a stack to figure out where the exception was thrown (stack trace). It also puts additional strain on the garbage collector and is considered a very poor design for using exceptions outside of exceptional situations.

+7
source

Is it possible to use a lot of try , and not even more if s?

If you need one of them to have a functional application, there might be something wrong with your design. Exceptions should usually be exceptional, not ordinary.

The situations that you described (indexes outside the limits) are clearly not exceptional, especially since you ignore exceptions because this is not a big problem. This is a big hint that you should invest in preventing exceptions that happen first.

When I tried to avoid exceptions using many and many if statements ...

A lot of if not a good approach, since you have found it is hard to read, and this can hide logical errors that just create more headaches.

Instead, consider giving objects more clearly defined and discrete responsibilities so that they do not enter states that may be exceptional to begin with. If you need the "batches and batches" of if , then your objects check many conditions, which means that their responsibilities are probably too large.

+4
source

In general, you should write your code and structure your logic so that exceptions are not used to process program logic. In some cases, you can use exceptions to cope with conditions that are rare but expected. However, exceptions should be avoided for the control flow, since they are mainly goto that can make the control flow very confusing and they do not work.

In your case, this is not like using exceptions to handle logical errors. In fact, the exceptions sound as if they are doing exactly what they were supposed to do. That is, they tell you that your algorithm is incorrect.

I would recommend looking carefully at your code and determining why your code is IndexOutOfBoundsException . If it will load during the internal processing of your own parameters, you probably have an error or a poorly developed algorithm. Ask: "Where did this index come from?" and "Is there a way to write an algorithm so that borders are built into the design and not attached to them like an afterthought?"

+2
source

You should always write your code to avoid exceptions. They are very expensive to catch.

You should also avoid many nested if . You are right, they make your code less readable.

You may find that switch can help, but there are a number of other ways to avoid exceptions and if .

For example, you can write this extension method:

 public static class Ex { public static void IfBounded<T>(this T[] @this, int index, Action<T> action) { if (@this == null) throw new System.ArgumentNullException("@this"); if (action == null) throw new System.ArgumentNullException("action"); if (index >= @this.GetLowerBound(0) && index <= @this.GetUpperBound(0)) { action(@this[index]); } } } 

Now you can write the following code:

 var messages = new [] { "Hello", "Goodbye", }; messages.IfBounded(-1, t => Console.WriteLine(t)); messages.IfBounded(0, t => Console.WriteLine(t)); messages.IfBounded(1, t => Console.WriteLine(t)); messages.IfBounded(2, t => Console.WriteLine(t)); 

This particular extension method may not be suitable for your situation, but something like this can make your code quite concise and concise.

0
source

All Articles