Why write a static method when you can just abuse constructors instead?



  • And I thought my last post on exception mis-handling was rough...

    I kept seeing this strange idiom in the project I was working on:

    catch(Exception ex)
    {
    CustomExceptionHandler newException = new CustomExceptionHandler(ex);
    }

     

    This magical new object was then (always) promptly discarded as control reached the end of the Catch block.  What was this strange and wonderful CustomExceptionHandler?

    
    

    public class CustomExceptionHandler : ApplicationException
    {
    public CustomExceptionHandler(Exception e)
    {
    BusinessLogic.ReportException(e);
    Trace.WriteLine(BusinessLogic.ExceptionToString(e, ExceptionMode.Log));
    }
    }

     

    Of course, in true WTF fashion, instead of handling exceptions this way in one place (most likely with AppDomain.UnhandledException), we do this explicitly hundreds of times.



  • Memory leaks are fun. I remember them being bad for some reason but all I can remember about it is pink bubble gum. Wee!



  • The compiler lets me do it, so it must be good!



  • @Lingerance said:

    Memory leaks are fun. I remember them being bad for some reason but all I can remember about it is pink bubble gum. Wee!

     

    This isn't C++.  There's no memory leak.

    In fact, if the compiler is smart, it might never allocate any memory at all, so that the bytecode produced is the same as a call to a static method.
     



  • @seaturnip said:

    @Lingerance said:

    Memory leaks are fun. I remember them being bad for some reason but all I can remember about it is pink bubble gum. Wee!

     

    This isn't C++.  There's no memory leak.

    In fact, if the compiler is smart, it might never allocate any memory at all, so that the bytecode produced is the same as a call to a static method.
     

    do i get to abuse every feature that might be optimized away?  sounds like a great time!
     



  • @arty said:

    @seaturnip said:

    @Lingerance said:

    Memory leaks are fun. I remember them being bad for some reason but all I can remember about it is pink bubble gum. Wee!

     

    This isn't C++.  There's no memory leak.

    In fact, if the compiler is smart, it might never allocate any memory at all, so that the bytecode produced is the same as a call to a static method.
     

    do i get to abuse every feature that might be optimized away?  sounds like a great time!

    How is letting the garbage collector do it's job 'abuse'?



  • @MasterPlanSoftware said:

    @arty said:

    @seaturnip said:

    @Lingerance said:

    Memory leaks are fun. I remember them being bad for some reason but all I can remember about it is pink bubble gum. Wee!

     

    This isn't C++.  There's no memory leak.

    In fact, if the compiler is smart, it might never allocate any memory at all, so that the bytecode produced is the same as a call to a static method.
     

    do i get to abuse every feature that might be optimized away?  sounds like a great time!

    How is letting the garbage collector do it's job 'abuse'?

    The abuse is in doing side effects in a constructor and throwing away the object.  I'm fine with garbage collection.  I might argue that since the value isn't used, the compiler might just as well elide initialization.  That'd be a smart compiler.
     



  • @arty said:

    @MasterPlanSoftware said:

    @arty said:

    @seaturnip said:

    @Lingerance said:

    Memory leaks are fun. I remember them being bad for some reason but all I can remember about it is pink bubble gum. Wee!

     

    This isn't C++.  There's no memory leak.

    In fact, if the compiler is smart, it might never allocate any memory at all, so that the bytecode produced is the same as a call to a static method.
     

    do i get to abuse every feature that might be optimized away?  sounds like a great time!

    How is letting the garbage collector do it's job 'abuse'?

    The abuse is in doing side effects in a constructor and throwing away the object.  I'm fine with garbage collection.  I might argue that since the value isn't used, the compiler might just as well elide initialization.  That'd be a smart compiler.
     

    I don't see any abuse here. I agree 'doing stuff' in the constructor might be bad form... but they are all static methods in the constructor. So who really cares? Would you rather a 'MyException.DoStuff();' in there?

    I see no memory issues in this code.

    Granted there are many wtfs here...

    Like not throwing the exception, like improperly using custom exceptions, etc. But making a static logging function to handle the exception through AppDomain.UnhandledException would be ideal.

    Memory issues? I don't see them. Enlighten me.

     



  • @MasterPlanSoftware said:

    Memory issues? I don't see them. Enlighten me.

    Pressuming you're aluding to my comment about memory leaks, I was tired and I forgot not all languages require explicit cleaning (freeing). My wtf.



  • @Lingerance said:

    @MasterPlanSoftware said:
    Memory issues? I don't see them. Enlighten me.

    Pressuming you're aluding to my comment about memory leaks, I was tired and I forgot not all languages require explicit cleaning (freeing). My wtf.

    Responding to both comments that seem to completely miss or ignore the actual wtfs here.



  • @MasterPlanSoftware said:

    @arty said:


    do i get to abuse every feature that might be optimized away?  sounds like a great time!

    How is letting the garbage collector do it's job 'abuse'?

    Did you miss the word "might" there? There are lots of things that "might" be optimised away. Doesn't mean that you should just do them and hope for the best!

     
    Also note that this isn't perl - it is at some point going to be read by a human. Abuse like that will only lead to confusion, and confusion creates bugs. This person reading your code may very well "optimise away" your unused object. The very least that will happen is that they will learn to hate you and plan to at some point in the future tear out your innards with a rusty spork. I'd agree with them, too. Why deal with this kind of junk when you can optimise away the bad programmer!




  • @RayS said:

    @MasterPlanSoftware said:

    @arty said:


    do i get to abuse every feature that might be optimized away?  sounds like a great time!

    How is letting the garbage collector do it's job 'abuse'?

    Did you miss the word "might" there? There are lots of things that "might" be optimised away. Doesn't mean that you should just do them and hope for the best!

     
    Also note that this isn't perl - it is at some point going to be read by a human. Abuse like that will only lead to confusion, and confusion creates bugs. This person reading your code may very well "optimise away" your unused object. The very least that will happen is that they will learn to hate you and plan to at some point in the future tear out your innards with a rusty spork. I'd agree with them, too. Why deal with this kind of junk when you can optimise away the bad programmer!


    My point is that this WTF has nothing to do with optimization or memory issues.
    It is simply wrong. If this person wanted to catch an exception, and throw one more specific the problem, that is fine.

    But instead, they made a new exception, and in the constructor handled error logging.

    The OP was perfectly correct. If they wanted to do this, they should have routed all unhandled exceptions through a static error reporting in ONE place.

    This stuff comes from the (seemingly very common) lack of understanding of exceptions.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.