Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To ask permission or apologize?

I come from a python background, where it's often said that it's easier to apologize than to ask permission. Specifically given the two snippets:

if type(A) == int:
  do_something(A)
else:
  do_something(int(A))

try:
  do_something(A)
except TypeError:
  do_something(int(A))

Then under most usage scenarios the second one will be faster when A is usually an integer (assuming do_something needs an integer as input and will raise its exception fairly swiftly) as you lose the logical test from every execution loop, at the expense of a more costly exception, but far less frequently.

What I wanted to check was whether this is true in C#, or whether logical tests are fast enough compared to exceptions to make this a small corner case?

Oh and I'm only interested in release performance, not debug.


OK my example was too vague try this one:

Naive solution:

return float(A) % 20 # coerse A to a float so it'll only fail if we actually don't
                     # have anything that can be represented as a real number.

Logic based solution:

if isinstance(A, Number): # This is cheaper because we're not creating a new
    return A % 20         # object unless we really have to.
else:
    return float(A) %20

Exception based solution:

try: # Now we're doing any logical tests in the 99% of cases where A is a number
  return A % 20
except TypeError:
  return float(A) % 20

Examples using FSOs, database connections, or stuff over a network are better but a bit long-winded for a question.

like image 717
theheadofabroom Avatar asked Jun 10 '11 11:06

theheadofabroom


People also ask

Why is it better to ask for permission?

When you ask others for permission, you make it clear that you want them to have a say in what's going on. More importantly, you make it clear that you have something to say that is important to you. You are creating a container to go into the thing that might be scary, difficult or emotional, yet valuable.

What is the saying beg for forgiveness?

I have always believed in the saying, “It's better to beg for forgiveness than to ask for permission.” It's a way of life. It's not about abusing situations but about knowing when to push the boundaries.

What is the saying about asking for forgiveness later?

There's an old saying: It's easier to ask for forgiveness than permission.

Who said it's easier to ask for forgiveness than permission movie?

What Admiral Grace Hopper really meant when she said, "It's easier to ask forgiveness than it is to get permission"


2 Answers

Probably not. .NET exceptions are relatively expensive.

Several .NET functions offer both variants for this reason. (int.TryParse, which returns a success code is often recommended because it is faster than int.Parse which throws an exception on failure)

But the only answer that matters is what your own profiling data tells you. If you need performance, then you need to measure, measure, measure.

Because what was fastest on my computer, with my code, with my version of the .NET framework, at this time may not be the fastest on your computer, with your code, with your version of the .NET framework at the time when you read it.

like image 135
jalf Avatar answered Nov 08 '22 08:11

jalf


Exceptions in .NET are fairly heavyweight, so the philosophy in C# is to use exceptions only for exceptional situations, not for program flow.

The philosophy in C# is also geared towards checking all input received from external code before using it. Example:

public void Foo(int i)
{
    if (i == 0)           // validate input received from external code
    {
        throw new ArgumentOutOfRangeException("i");
    }

    DoSomething(i);
}

public void Foo()
{
    DoSomething(1);
}

internal void DoSomething(int i)
{
    Debug.Assert(i != 0); // validate that i is not zero in DEBUG build
                          // assume that i is not zero in RELEASE build

    Console.WriteLine(42 / i);
}
like image 20
dtb Avatar answered Nov 08 '22 06:11

dtb