Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I happened to create a mutable class like this:

class Mutable<T> {
    private T value;
    public Mutable() { this.value = null; }
    public Mutable(T value) { this.value = value; }
    T get() { return this.value; }
    void set(T value) { this.value = value; }
}

And then it's often used in a method like this:

boolean operation(String input, Mutable<Set<String>> dataOut) throws ... {
    boolean result;
    try {
         String data = doSomething(input);
         result = validate(data);
         if (result && dataOut != null) {
             List<String> values = Arrays.asList(data.split(", "));
             Collections.sort(values);
             dataOut.set(new LinkedHashSet<String>(values));
         }
    } catch(SpecificIgnorableException ex) {
         result = false;
         logger.debug(ex);
    }
    return result;
}

...which is just an example, could be any use case, where one would use ref or out parameters in C#, or non-const reference parameters in C++, or pointers to output parameters in C.

First, same could be done by using an array (with one element) instead of above custom type. Does it make sense to have this custom type which clearly states mutable, instead of using an implicitly mutable array?

Second, is this pattern bad and code smell in Java? Let's limit to cases where using out parameter would make sense in C#. Should every instance of this kind of Java code be replaced? With what?

share|improve this question
in your example returning null or rethrowing the Exception (wrapped or not) up the stack to handle it there instead of a boolean success return value (which have been deprecated since exceptions) – ratchet freak Mar 28 at 16:31
@ratchetfreak Exceptions are fairly heavy, and do not fit every situation. A good sign of return value being better is, if most code would have empty catch block (which gets replaced by non-existent else when return value is used instead). Opposite of success is not always failure. Changed code to imply logging only as debug measure. – hyde Mar 28 at 16:36
3  
ugh don't diss things just because they are "heavy", for most applications something "heavy" but easy to use is better than a "light" custom awkward construct, also no-one is forcing you to use the checked exception classes, when failure is a common result though a null return value is enough to signal an error – ratchet freak Mar 28 at 16:49

2 Answers

up vote 4 down vote accepted

The real question is "are functions with side-effects bad?"

Providing a reference to an explicit mutable ("out") variable is no different than providing a Map that you modify, or referencing a global variable from within the function. In all three cases, the function is permitted to modify something in a way that is hard to reason about. Consider, for example, a function that modifies the "out" parameter, then throws.

The counter-argument is that this is no different from a method call that modifies an object's private state and then throws.

Personally, if I'm writing a method that is focused on a "result", I would prefer creating a new class to hold it; classes are cheap in Java. If I'm writing a method that modifies an object's internal state, I generally don't return anything from the method.

share|improve this answer
3  
C# has an idiom called TryParse. It relies on an out parameter to get the parsed value, and returns a boolean indicating success or failure. It's the only time I really use out; making a class just for a return value seems a bit pointless, unless you use it everywhere where you need a number and a success indicator. I suppose you could simply return a Tuple<T, bool>. – Robert Harvey Mar 28 at 16:51
I would rather create a class than try to figure out what 'tuple.Item1' represents. – jhewlett Mar 29 at 5:59
I think this is what I find clear yet simple about "out" parameters: they have a name, without requiring creation of a new type. And I think creation of extra type is clutter if it is only used as return type of single method, and does not have any custom behaviour (ie. methods other than setters and getters). – hyde Mar 29 at 11:44

First of all out and ref have nothing to do with each other. An out parameter in C# is just the only way the language has of returning multiple values from a function, short of creating a new type to use as the return value. Just because it's in the parameter list is only a syntax thing. There's no equivalent in Java.

I think what you've got there is a code smell because it's not really idiomatic Java. In your situation, I would just define a new class to use as a result value.

share|improve this answer
I think out and ref have nothing to do with each other is a bit of a stretch, second para I'd agree with though – jk. Mar 28 at 17:08
@jk - well, if you use a ref you're saying that the function is both reading and modifying that value, but if you're using an out then it's strictly an output, no different than if you'd used it on the left hand side of an assignment. – Scott Whitlock Mar 28 at 18:00
I'd say C# out parameters are just syntactic sugar on top of ref parameters (not sure if there is bytecode level difference), so very similar from one point of view. A great feature to be sure, making a common use case of reference parameters distinct, self-documenting and compiler-enforceable. – hyde Mar 29 at 11:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.