One room, many exits



  • What, exactly, am I supposed to do with the output of this method?

     

      public class OurException extends RuntimeException {
    // Wrap all RuntimeException constructors
    }

    public Object someFunc(...) {
    try {
    ...
    if (some condition) {
    return -1;
    }

        if (some other condition) {
           return new ArrayList<Widget>();
        }
    
        if (yet another condition) {
           return some calculated List<DifferentWidget>;
        }
    
        if (some error condition) {
           throw new OurException("The message");
        }
    } catch (Exception e) {
      throw new OurException(e.getMessage());
    }
    

    }



  • Humanely destroy the author to prevent repetition and serve as a deterrent to the survivors?



  • Print out a couple thousand copies of the output, then use that paper to crush the person(s) responsible.



  • Usual stuff in PHP.

    If it isn't ... The devs who have written that thing was certainly a (bad) ex-PHP dev.



  • Looks like Russian Roulette with a fully-loaded revolver, except it may shoot

    • A bullet
    • A squid
    • A Jell-O Pudding Cup
    • A feral cat
    • A Durian fruit
    • Bus full of escaped rapists, serial killers, and cannibals.


  • Make an enum "returnsInt", "returnsWidgetList", "returnsDifferentWidgetList".

    Now make a struct with the following fields:

    returnsEnum (we defined above)
    int intReturn
    List<Widget> widgetReturn
    List<DifferentWidget> differentWidgetReturn

    Now refactor the function to populate the struct and return it. PROBLEM SOLVED!



  • You'll never get it up the stairs...



  •  Force the original developer to implement polymorphic return types into the JVM.  This will place them in an infinite busy loop and rendered harmless to future generations.



  • Ouch, Snoofle.



  • Is the exception stack trace lost on purpose? (like, for "security reasons")

    • If no -> Apply clue-by-4 to developer, use inner exceptions or whatever the Java equivalent is.
    • If yes -> Apply clue-by-4 to designer, and log that stack trace somewhere before it's forgotten.


  • You could make this code a little more enterprise.

    public interface IReturnValue
    {
        bool IsInt();
        bool IsListOfWidget();
        bool IsListOfDifferentWidget();
    }
    
    public interface IIntReturnValue : IReturnValue
    {
        int GetReturnValue();
    }
    
    public interface IListOfWidgetReturnValue : IReturnValue
    {
        List<DifferentWidget> GetReturnValue();
    }
    
    public interface IListOfDifferentWidgetReturnValue : IReturnValue
    {
        List<DifferentWidget> GetReturnValue();
    }
    
    public interface IExceptionReturnValue : IReturnValue
    {
        int GetReturnValue();
    }
    
    public abstract class BaseReturnValue : IReturnValue
    {
        public bool IsInt()
        {
            return false;
        }
    
        public bool IsListOfWidget()
        {
            return false;
        }
    
        public bool IsListOfDifferentWidget()
        {
            return false;
        }
    }
    
    public class IntReturnValue : BaseReturnValue, IIntReturnValue
    {
        private int m_intReturnValue;
    
        public IntReturnValue(int p_intReturnValue)
        {
            m_intReturnValue = p_intReturnValue;
        }
    
        public int GetReturnValue()
        {
            return m_intReturnValue;
        }
    
        public bool IsInt()
        {
            return true;
        }
    
        public bool IsListOfWidget()
        {
            return false;
        }
    
        public bool IsListOfDifferentWidget()
        {
            return false;
        }
    }
    
    public class ListOfWidgetReturnValue : BaseReturnValue, IListOfWidgetReturnValue
    {
        private List<Widget> m_listOfWidgetReturnValue;
    
        public ListOfWidgetReturnValue(List<Widget> p_listOfWidgetReturnValue)
        {
            m_listOfWidgetReturnValue = p_listOfWidgetReturnValue;
        }
    
        public int GetReturnValue()
        {
            return m_listOfWidgetReturnValue;
        }
    
        public bool IsInt()
        {
            return false;
        }
    
        public bool IsListOfWidget()
        {
            return true;
        }
    
        public bool IsListOfDifferentWidget()
        {
            return false;
        }
    }
    
    public class ListOfDifferentWidgetReturnValue : BaseReturnValue, IListOfDifferentWidgetReturnValue
    {
        private List<DifferentWidget> m_listOfDifferentWidgetReturnValue;
    
        public ListOfDifferentWidgetReturnValue(List<DifferentWidget> p_listOfDifferentWidgetReturnValue)
        {
            m_listOfDifferentWidgetReturnValue = p_listOfDifferentWidgetReturnValue;
        }
    
        public int GetReturnValue()
        {
            return m_listOfDifferentWidgetReturnValue;
        }
    
        public bool IsInt()
        {
            return false;
        }
    
        public bool IsListOfWidget()
        {
            return false;
        }
    
        public bool IsListOfDifferentWidget()
        {
            return false;
        }
    }
    
    public class ExceptionReturnValue : BaseReturnValue, IExceptionReturnValue
    {
        private Exception m_exceptionReturnValue;
    
        public ExceptionReturnValue(List<Widget> p_exceptionReturnValue)
        {
            m_exceptionReturnValue = p_exceptionReturnValue;
        }
    
        public int GetReturnValue()
        {
            throw m_exceptionReturnValue;
        }
    
        public bool IsInt()
        {
            return false;
        }
    
        public bool IsListOfWidget()
        {
            return false;
        }
    
        public bool IsListOfDifferentWidget()
        {
            return false;
        }
    }
    

    Then change someFunc to be

    public Object someFunc(...) {
        try {
            try {
                ...
                if (some condition) {
                    return new IntReturnValue(-1);
                }   
     
                if (some other condition) {
                    return new ListOfWidgetReturnValue(new ArrayList<Widget>());
                }
     
                if (yet another condition) {
                    return new ListOfDifferentWidgetReturnValue(some calculated List<DifferentWidget>);
                }
     
                if (some error condition) {
                    throw new OurException("The message");
                }
            } catch (Exception l_exception1) {
                throw new OurException(l_exception1.getMessage());
            }
        } catch (Exception l_exception2) {
            return new ExceptionReturnValue(l_exception2);
        }
    }
    

    And of course, using it is pretty simple, you just need a small wrapper.

    public Object someFuncWrapper(...)
    {
        IReturnValue l_returnValueValue = someFunc(...);
        Object l_objectResult;
    
        if (l_returnValueValue instanceof IIntReturnValue)
        {
            l_objectResult = new Integer.valueOf(((IIntReturnValue)l_returnValueValue).GetReturnValue()).intValue();
        }
        else
        {
            Method l_methodGetReturnValueMethod = l_returnValueValue.getClass().getMethod("GetReturnValue");
            try {
                l_objectResult = l_methodGetReturnValueMethod.invoke(value);
            } catch (Exception l_exception) {
                l_objectResult = l_exception;
            }
        }
    
        if (l_objectResult instanceof Throwable)
        {
            throw new OurException("An exception was thrown.");
        }
    
        if (l_objectResult == null)
        {
            return null;
        }
    }
    

    Note: code should work but I haven't tested it. Use at your own risk.



  • My favorite exit is the one where all the conditions are false and no exceptions are thrown. And it just returns nothing (void? (Java?)).

    That's the exit I would take if I was an execution thread. For sure.



  • @superjer said:

    My favorite exit is the one where all the conditions are false and no exceptions are thrown. And it just returns nothing (void? (Java?)).

    That's the exit I would take if I was an execution thread. For sure.


    I'm pretty sure it wouldn't compile if it was missing a return on some execution path. Also, there's a quite large difference between returning nothing and returning an uninteresting value. Namely, the first one doesn't return.



  • @mott555 said:

    Looks like Russian Roulette with a fully-loaded revolver, except it may shoot

    • A bullet
    • A squid
    • A Jell-O Pudding Cup
    • A feral cat
    • A Durian fruit
    • Bus full of escaped rapists, serial killers, and cannibals.

    This is now my favourite post ever.


  • Discourse touched me in a no-no place

    I'm surprised that doesn't just return the OurException instead of throwing it.



  • I suggest the following for consistency:

    {
        Object result = someFunc(...);
        throw new OurException();
    }
    

    Alternatively, if you want to flaunt your knowledge about the exotic Object class:

    {
        Object result;
        try
        {
            result = someFunc(...);
        }
        catch (OurException ex)
        {
            result = ex;
        }
        return result.toString();
    }
    


  • try  {

       return somefunc(whatever);

    } catch (Exception e) {

     throw new OurException("You have been eaten by a grue");

    }

     



  • @snoofle said:

    What, exactly, am I supposed to do with the output of this method?
    The same as with any other polytypic method: whatever you like.

     



  • I wonder if some of your code was developed by the guys who developed our code base ...



  •  @Ben L. said:

    I'm pretty sure it wouldn't compile if it was missing a return on some execution path. Also, there's a quite large difference between returning nothing and returning an uninteresting value. Namely, the first one doesn't return.

     What? Really? What language are you talking about?

     



  • @superjer said:

    What? Really? What language are you talking about?

    C#, to the best of my knowledge, won't compile if an execution path of a function has no return (and the function is defined to return something, of course.)



  • @blakeyrat said:

    C#, to the best of my knowledge, won't compile if an execution path of a function has no return (and the function is defined to return something, of course.)
     

    Hmm... yes. Seems to be that way in C# and Java too. I guess I never checked before in any language that didn't just return the default value for the function's type or something Null-ish.

    Not that I would ever rely on something like that... *cough*



  • @configurator said:

    bool IsInt();
    bool IsListOfWidget();
    bool IsListOfDifferentWidget();

    You just implemented multiple inheritance in Java! Brillant!



  • @superjer said:

     @Ben L. said:

    I'm pretty sure it wouldn't compile if it was missing a return on some execution path. Also, there's a quite large difference between returning nothing and returning an uninteresting value. Namely, the first one doesn't return.

     What? Really? What language are you talking about?

     


    Assuming you're talking about the second part, void is an uninteresting value, not a nonexistent value. System.exit does not return. Unfortunately, Java's syntax does not reflect this.


Log in to reply