Is there a name for this anti-pattern/code smell?

前端 未结 13 2458
轻奢々
轻奢々 2021-02-12 16:00

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 h

相关标签:
13条回答
  • 2021-02-12 16:18

    I agree with those that say this is not an anti-pattern. Its a perfectly valid pattern in certain contexts. Exceptions are for exceptional 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.

    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.

    0 讨论(0)
  • 2021-02-12 16:23

    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.

    0 讨论(0)
  • 2021-02-12 16:31

    Well, it's not an antipattern. The C++ standard library makes use of this feature and .NET even offers a special FunctionResult class in the .NET framework. It's called Nullable. 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 Nullable class, it would certainly have been used for the NumberType.TryParse methods, instead of the out parameter.

    0 讨论(0)
  • 2021-02-12 16:31

    I usually pass the payload as (not const) reference and the error code as a return value.

    I'm a game developer, we banish exceptions

    0 讨论(0)
  • 2021-02-12 16:34

    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.

    I would not call it an anti-pattern. It's a very well proven workable method that's often preferable to using exceptions.

    0 讨论(0)
  • 2021-02-12 16:34

    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.

    0 讨论(0)
提交回复
热议问题