Tech Off Thread

41 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

is 'new object().property' or 'new object().method()' a good idea?

Back to Forum: Tech Off
  • User profile image
    shuff1203

    evildictaitor wrote:
    
    JChung2006 wrote:
    
    GetType(Application).GetConstructor(New System.Type() {}).Invoke(New Object() {}).Run(f)


    While your post is technically correct, using reflection just to inline a function is never a good idea.


    Ya, but he did answer the question, that is a way to do it with just oneline of code in vb.net...

  • User profile image
    evildictait​or

    shuff1203 wrote:
    
    Ya, but he did answer the question, that is a way to do it with just oneline of code in vb.net...


    Well, sort of. The inlined expression in C# does a very different control path to the thing he showed there, which can experience considerable slowdown, since reflection is a relatively expensive process (computationally speaking), and is never a good idea if there's an alternative.

  • User profile image
    Minh

    If new String("Bob") could not be created, then an exception would be thrown, would it not?

    Then .Length would never be called.

    Also, you worry too much.

  • User profile image
    JChung2006

    evildictaitor wrote:
    
    shuff1203 wrote:
    
    Ya, but he did answer the question, that is a way to do it with just oneline of code in vb.net...


    Well, sort of. The inlined expression in C# does a very different control path to the thing he showed there, which can experience considerable slowdown, since reflection is a relatively expensive process (computationally speaking), and is never a good idea if there's an alternative.

    You missed the point so please just drop it.  Yes, reflection is slower than if the references were already there, but guess what?  Reflection is still faster than a network roundtrip or a disk seek by several orders of magnitude.

    "Premature optimization is the root of all evil." -C. A. R. Hoare

  • User profile image
    evildictait​or

    JChung2006 wrote:
    

    You missed the point so please just drop it.  Yes, reflection is slower than if the references were already there, but guess what?  Reflection is still faster than a network roundtrip or a disk seek by several orders of magnitude.

    "Premature optimization is the root of all evil." -C. A. R. Hoare



    Maybe, but compiler optimisations is one of the reasons your computer is getting faster year-on-year. It's not all hardware you know.

    And even if reflection is faster than a network roundtrip it's less maintainable code, and automatic reasoning agents can't reason over reflection code, so the compiler can't do any optimisations over the function call.

    But I'll assume I've missed the point, and stop pressing the issue.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.