0

I've a method which is being called more than 100 times per second, and because of its internal implementation, it throws an exception often.

There's no way to prevent those exceptions because of method's internal implementation, but I'd like to reduce performance costs of these exceptions, for example by disabling stack trace gathering for exceptions, because those information are not come in handy for me.

Any idea ?

Method Implementation:

 public static TValue GetValue<TObj, TValue>(this TObj obj, Func<TObj, TValue> member, TValue defaultValueOnNull = default(TValue))
    {
        if (member == null)
            throw new ArgumentNullException("member");

        if (obj == null)
            throw new ArgumentNullException("obj");

        try
        {
            return member(obj); // We've lots of null reference exceptions here.
// And I'm not interested in stack trace of those exceptions, how to reduce performance costs here ?
            }
            catch (NullReferenceException)
            {
                return defaultValueOnNull;
            }
        } 

Method usage :

String flightNumber = originDestinationInformation.GetValue(dest => dest.OriginDestinationOption.FlighInfo.FlightNumber, "");
Yaser Moradi
  • 3,267
  • 3
  • 24
  • 50
  • 3
    `NullReferenceException` is something you have to prevent not catch. – Sriram Sakthivel Aug 06 '14 at 06:51
  • 2
    If there was some magical flag to do this, the system would *still* have to walk all the way up through various stacks frames, perform unwinding, etc, to *find* that magical flag. It wouldn't save much in terms of performance. – Damien_The_Unbeliever Aug 06 '14 at 06:54
  • @SriramSakthivel Yes, you're true, but I've used this approach to have more clear codes without lots of 'if not null' checks for objects. – Yaser Moradi Aug 06 '14 at 06:54
  • 1
    @YasserMoradi Am sorry to say this: Your comment is ridiculous. If you don't want to check for null use [NullObject Pattern](http://en.wikipedia.org/wiki/Null_Object_pattern), you shouldn't catch `NullReferenceException`. IMHO code that catches `NullReferenceException` is a crap. – Sriram Sakthivel Aug 06 '14 at 06:58
  • Then why do you have `if (member == null)` and `if (obj == null)` checks? You can eliminate it and get even better code isn't it? – Sriram Sakthivel Aug 06 '14 at 07:00
  • I can not simply understand the purpose of this function. Why do you want to use a function to access properties? simply writing down String flightNumber = originDestinationInformation.OriginDestinationOption.FlighInfo.FlightNumber is enough instead of having a function. If the purpose is just handling null references, you may as well assign not null values during object creation, like string.empty (you may just use prototype pattern). – daryal Aug 06 '14 at 07:07
  • @SriramSakthivel Calling GetValue method without passing those parameters (obj & member) correctly is not a valid method call and I should throw an exception in that case. There are several if with this style in ASP.NET and EntityFramework codes and so on. This is a best practices. I'm not sad because of your comment (-:, but could you please provide me a real world example for me which is more easier than this : String flightNumber = originDestinationInformation.GetValue(dest => dest.OriginDestinationOption.FlighInfo.FlightNumber, ""); ? – Yaser Moradi Aug 06 '14 at 07:09
  • @daryal Yes, you're right, but I'd like to use most easiest approach while developing applications. Lots of if not null checks or assigning default values are not handy and there is no value in this approach. – Yaser Moradi Aug 06 '14 at 07:15
  • @YasserMoradi You can use [this](http://stackoverflow.com/a/2081942/2530848) – Sriram Sakthivel Aug 06 '14 at 07:38
  • @SriramSakthivel Thanks, but that code is 25x slower than my code, with or without exceptions, in any scenario. It's obvious because he has used Reflection, lambda expressions which are more solver than exceptions, specially with that wrong usage. Just test that with Stopwatch. – Yaser Moradi Aug 06 '14 at 07:58

1 Answers1

1

AFAIK, You can't do that without modifying the .NET framework.

M.A. Hanin
  • 8,044
  • 33
  • 51