Why should an API return 'void'?

前端 未结 7 1238
南方客
南方客 2021-02-07 00:23

When writing an API or reusable object, is there any technical reason why all method calls that return \'void\' shouldn\'t just return \'this\' (*this in C++)?

For examp

相关标签:
7条回答
  • 2021-02-07 00:59

    If you intend your API to be called from F#, please do return void unless you're convinced that this particular method call is going to be chained with another nearly every time it's used.

    If you don't care about making your API easy to use from F#, you can stop reading here.

    F# is more strict than C# in certain areas - it wants you to be explicit about whether you're calling a method in order to get a value, or purely for its side-effects. As a result, calling a method for its side-effects when that method also returns a value becomes awkward, because the returned value has to be explicitly ignored in order to avoid compiler errors. This makes "fluent interfaces" somewhat awkward to use from F#, which has its own superior syntax for chaining a series of calls together.

    For example, suppose we have a logging system with a Log method that returns this to allow for some sort of method chaining:

    let add x y =
        Logger.Log(String.Format("Adding {0} and {1}", x, y)) // #1
        x + y                                                 // #2
    

    In F#, because line #1 is a method call that returns a value, and we're not doing anything with that value, the add function is considered to take two values and return that Logger instance. However, line #2 not only also returns a value, it appears after what F# considers to be the "return" statement for the function, effectively making two "return" statements. This will cause a compiler error, and we need to explicitly ignore the return value of the Log method to avoid this error, so that our add method has only a single return statement.

    let add x y =
        Logger.Log(String.Format("Adding {0} and {1}", x, y)) |> ignore
        x + y
    

    As you might guess, making lots of "Fluent API" calls that are mainly about side-effects becomes a somewhat frustrating exercise in sprinkling lots of ignore statements all over the place.

    You can, of course, have the best of both worlds and please both C# and F# developers by providing both a fluent API and an F# module for working with your code. But if you're not going to do that, and you intend your API for public consumption, please think twice before returning this from every single method.

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