unclebobmartin @unclebobmartin - 2y
Generally speaking you would use the handler if the function that returns the value is asynchronous. In that case the handler is just a callback from the asynchronous process. You might also pass a handler if the function returning the value might return many values over time. i.e. it runs in a loop and produces intermediate values that have to be handled by the external handler. Still another scenario is the "Tell-Don't-Ask" discipine which says that no function should return a value, rather they should all forward their results to other functions in other objects. This is not a discipline that I adhere to very closely; but others swear by it. From: (benc) at 05/24/23 08:27:50 on wss://relay.damus.io CC: #[3] >--------------- >#[4] I got an interesting question I’d like to hear your take on. > >Is there any wisdom when comparing the pros and cons of returning a value versus passing an interface? > >// two options for handling a value. >class Foo { > int returnValue(); > void passInterface(Handler); >} > >interface Handler { > void handle(int); >}