<p>Let me start by saying that I do not advocate this approach, but I saw it recently and I was wondering if there was a name for it I could use to point the guilty party to.  So here goes.</p>

<p>Now you have a method, and you want to return a value.  You <em>also</em> want to return an error code.  Of course, exceptions are a much better choice, but for whatever reason you want an error code instead.  Remember, I'm playing devil's advocate here.  So you create a generic class, like this:</p>

<pre><code>class FunctionResult&lt;T&gt;
{
    public T payload;
    public int result;
}
</code></pre>

<p>And then declare your functions like this:</p>

<pre><code>FunctionResult&lt;string&gt; MyFunction()
{
    FunctionResult&lt;string&gt; result;
    //...

    return result;
}
</code></pre>

<p>One variation on this pattern is to use an enum for the error code instead of a string.  Now, back to my question:  is there a name for this, and if so what is it?</p>

[{'answer_id': 72729, 'author': 'noah', 'author_id': 12034, 'author_profile': 'https://Stackoverflow.com/users/12034', 'pm_score': 1, 'selected': False, 'text': '<p>How about the "Can\'t decide whether this is an error or not" pattern.  Seems like if you really had an exception but wanted to return a partial result, you\'d wrap the result in the exception.</p>\n'}, {'answer_id': 72808, 'author': 'Gilligan', 'author_id': 12356, 'author_profile': 'https://Stackoverflow.com/users/12356', 'pm_score': 2, 'selected': False, 'text': '<p>I am not sure this is an anti-pattern. I have commonly seen this used instead of exceptions for performance reasons, or perhaps to make the fact that the method can fail more explicit. To me, it seems to be a personal preference rather than an anti-pattern.</p>\n'}, {'answer_id': 72840, 'author': 'rmaruszewski', 'author_id': 6856, 'author_profile': 'https://Stackoverflow.com/users/6856', 'pm_score': 4, 'selected': False, 'text': '<p>It is called "<a href="http://www.refactoring.com/catalog/replaceErrorCodeWithException.html" rel="nofollow noreferrer">Replace Error Code with Exception</a>"</p>\n'}, {'answer_id': 72846, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': "<p>Well, it's <em>not</em> an antipattern. The C++ standard library makes use of this feature and .NET even offers a special <code>FunctionResult</code> class in the .NET framework. It's called <code>Nullable</code>. Yes, this isn't restricted to function results but it can be used for such cases and is actually very useful here. If .NET 1.0 had already had the <code>Nullable</code> class, it would certainly have been used for the <code>NumberType.TryParse</code> methods, instead of the <code>out</code> parameter.</p>\n"}, {'answer_id': 72884, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>This approach is actually much better than some others that I have seen. Some functions in C, for example, when they encounter an error they return and seem to succeed. The only way to tell that they failed is to call a function that will get the latest error.</p>\n\n<p>I spent hours trying to debug semaphore code on my MacBook before I finally found out that sem_init doesn\'t work on OSX! It compiled without error and ran without causing any errors - yet the semaphore didn\'t work and I couldn\'t figure out why. I pity the people that port an application that uses <a href="http://www.csc.villanova.edu/~mdamian/threads/posixsem.html" rel="nofollow noreferrer">POSIX semaphores</a> to OSX and must deal with resource contention issues that have already been debugged.</p>\n'}, {'answer_id': 72906, 'author': 'ugasoft', 'author_id': 10120, 'author_profile': 'https://Stackoverflow.com/users/10120', 'pm_score': 3, 'selected': False, 'text': "<p>I usually pass the payload as (not const) reference and the error code as a return value.</p>\n\n<p>I'm a game developer, we banish exceptions</p>\n"}, {'answer_id': 72922, 'author': 'Dan Fleet', 'author_id': 7470, 'author_profile': 'https://Stackoverflow.com/users/7470', 'pm_score': 5, 'selected': True, 'text': "<p>I'd agree that this isn't specifically an antipattern.  It might be a smell depending upon the usage.  There are reasons why one would actually not want to use exceptions (e.g. the errors being returned are not 'exceptional', for starters).</p>\n\n<p>There are instances where you want to have a service return a common model for its results, including both errors and good values.  This might be wrapped by a low level service interaction that translates the result into an exception or other error structure, but at the level of the service, it lets the service return a result and a status code without having to define some exception structure that might have to be translated across a remote boundary.</p>\n\n<p>This code may not necessarily be an error either: consider an HTTP response, which consists of a lot of different data, including a status code, along with the body of the response.</p>\n"}, {'answer_id': 72980, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 1, 'selected': False, 'text': "<p>If you don't want to use exceptions, the cleanest way to do it is to have the function return the error/success code and take either a reference or pointer argument that gets filled in with the result.</p>\n\n<p>I would not call it an anti-pattern.  It's a very well proven workable method that's often preferable to using exceptions.  </p>\n"}, {'answer_id': 73016, 'author': 'Paul van Brenk', 'author_id': 1837197, 'author_profile': 'https://Stackoverflow.com/users/1837197', 'pm_score': 1, 'selected': False, 'text': "<p>If you expect your method to fail occasionally, but don't consider that exceptional, I prefer this pattern as used in the .NET Framework:</p>\n\n<pre><code>bool TryMyFunction(out FunctionResult result){    \n\n     //...    \n     result = new FunctionResult();\n}\n</code></pre>\n"}, {'answer_id': 73020, 'author': 'Peter Davis', 'author_id': 12508, 'author_profile': 'https://Stackoverflow.com/users/12508', 'pm_score': 3, 'selected': False, 'text': '<p>Konrad is right, C# uses dual return values all the time.  But I kind of like the TryParse, Dictionary.TryGetValue, etc. methods in C#.</p>\n\n<pre><code>int value;\nif (int.TryParse("123", out value)) {\n    // use value\n}\n</code></pre>\n\n<p>instead of</p>\n\n<pre><code>int? value = int.TryParse("123");\nif (value != null) {\n    // use value\n}\n</code></pre>\n\n<p>...mostly because the Nullable pattern does not scale to non-Value return types (i.e., class instances).  This wouldn\'t work with Dictionary.TryGetValue().  And TryGetValue is both nicer than a KeyNotFoundException (no "first chance exceptions" constantly in the debugger, arguably more efficient), nicer than Java\'s practice of get() returning null (what if null values are expected), and more efficient than having to call ContainsKey() first.</p>\n\n<p>But this <em>is</em> still a little bit screwy -- since this looks like C#, then it should be using an out parameter.  All efficiency gains are probably lost by instantiating the class.</p>\n\n<p>(Could be Java except for the "string" type being in lowercase.  In Java of course you have to use a class to emulate dual return values.)</p>\n'}, {'answer_id': 73706, 'author': 'ARKBAN', 'author_id': 11889, 'author_profile': 'https://Stackoverflow.com/users/11889', 'pm_score': 2, 'selected': False, 'text': '<p>I agree with those that say this is not an anti-pattern. Its a perfectly valid pattern in certain contexts. Exceptions are for <em>exceptional</em> situations, return values (like in your example) should be used in expected situations. Some domains expect valid and invalid results from classes, and neither of those should be modeled as exceptions.</p>\n\n<p>For example, given X amount of gas, can a car get from A to B, and if so how much gas is left over? That kind of question is ideal for the data structure you provided. Not being able to make the trip from A to B is expected, hence an exception should not be used.</p>\n'}, {'answer_id': 74585, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': "<p>In defense of the anti-pattern designation, this code lends itself to being used in a few ways:</p>\n\n<ol>\n<li>Object x = MyFunction().payload;  <em>(ignoring the return result - very bad)</em>  </li>\n<li>int code = MyFunction().result; <em>(throwing away the payload - may be okay if that's the intended use.)</em>  </li>\n<li>FunctionResult x = MyFunction();  //...  <em>(a bunch of extra FunctionResult objects and extra code to check them all over the place)</em></li>\n</ol>\n\n<p>If you need to use return codes, that's fine.  But then use return codes.  Don't try to pack an extra payload in with it.  That's what <em>ref</em> and <em>out</em> parameters (C#) are for.  Nullable types might be an exception, but only because there's extra sugar baked in the language to support it.</p>\n\n<p>If you still disagree with this assessment, DOWNVOTE this answer (not the whole question). If you do think it's an anti-pattern, then UPVOTE it.  We'll use this answer to see what the community thinks.</p>\n"}, {'answer_id': 300661, 'author': 'Mike Dunlavey', 'author_id': 23771, 'author_profile': 'https://Stackoverflow.com/users/23771', 'pm_score': 1, 'selected': False, 'text': '<p>Debates about smells and anti-patterns remind me the "Survivor" TV shows, where you have various programming constructs trying to vote each other off the island. I\'d prefer to see "construct X has such-and-so pros and cons", rather than a continually evolving list of edicts of what should and should not be done.</p>\n'}]
['https://Stackoverflow.com/questions/72682', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3043/']