Andrew Ducker (andrewducker) wrote,
Andrew Ducker
andrewducker

Musings

I've been coming to the conclusion that I don't like the way that methods return variables in C-like languages.  Having a single prioritised output parameter seems counterintuitive to me, and leads to two outcomes:
1) It's very easy to ignore/forget the return value.
2) If you have multiple return values then either one of them is the "proper" return value and the other one is passed back as a reference, or you pass back a tuple of some kind, or you have no return value and they both come back as references. 

It seems to me that it would be clearer and simpler if you always specified what parameters were in and out.

Rather than
string Reverse(string inputString)
you'd have
Reverse (string inputString, out string outputString)

This doesn't make a lot of difference when it comes to a simple method like that.  But if we take one with two outputs:

Do we use:
1) int GetMonthAndDay(DateTime inputDate, out int day)
2) MonthDayPair GetMonthAndDay(DateTime inputDate)
3) void GetMonthAndDay(DateTime inputDate, out int month, out int day)

The first one is clearly awful, the second one requires me to create an extra class, and the third one seems entirely clear.

Also, the use of different parameters means that you have smart method selection going on.
Convert(DateTime inputDate, out long seconds)
Convert(DateTime inputDate, out string nicelyFormatted)
makes it obvious from the parameters exactly what you're expecting to get back from it.

Of course, it does complicate very simple things:
DateTime tomorrow = today.AddDay();
would become
DateTime tomorrow;
today.AddDay(out tomorrow);

I suspect I'm just working on stuff where there are always multiple ins and outs, and also using generics and type inference, which doesn't work on return values, just parameters, and this is leading me to odd conclusions.
Subscribe

  • Interesting Links for 19-10-2021

    Lord Chancellor, there is already a mechanism for the law to be changed: it is called Parliament (tags: law uk fascism ) Dolphins living off…

  • Interesting Links for 18-10-2021

    Six things the UK could do to tackle climate change (tags: globalwarming uk ) Real Names: the wrong tool for the wrong problem (tags: names…

  • instagram cross-post

    Waiting for dance class to begin. Original is here on instagram. Original post on Dreamwidth - there are comments there.

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 33 comments

  • Interesting Links for 19-10-2021

    Lord Chancellor, there is already a mechanism for the law to be changed: it is called Parliament (tags: law uk fascism ) Dolphins living off…

  • Interesting Links for 18-10-2021

    Six things the UK could do to tackle climate change (tags: globalwarming uk ) Real Names: the wrong tool for the wrong problem (tags: names…

  • instagram cross-post

    Waiting for dance class to begin. Original is here on instagram. Original post on Dreamwidth - there are comments there.