![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
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.
no subject
Date: 2009-04-09 02:51 pm (UTC)procedure GetMonthAndDay(inputDate: in DateTime; month: out Integer; day: out Integer) is
begin
-- do stuff here
end GetMonthAndDay;
no subject
Date: 2009-04-09 02:55 pm (UTC)no subject
Date: 2009-04-09 07:53 pm (UTC)Just as with the C'ish languages, you need to have an enforced coding standard.
no subject
Date: 2009-04-09 03:18 pm (UTC)f(g(x))
there is a reason we use that notaton as it's easy to combine functions.
p.s. in prolog does something very similar, and it's annoying as hell sometimes.
no subject
Date: 2009-04-09 03:51 pm (UTC)no subject
Date: 2009-04-09 04:19 pm (UTC)The problem is that function composition is a bitch
instead of doing
y = f(g(x)), you have to do f(x,x1), g(x1,y)
this gets very clumsy and annoying after a while.
Functions are useful for the common case of a single return, and in more mature languages you can return multiple values as pairs.
no subject
Date: 2009-04-09 04:54 pm (UTC)Is there a nice way of returning pairs of values? Or do you just get back a class with two things in it, and have to hope that they're the right way round?
no subject
Date: 2009-04-09 05:25 pm (UTC)To be frank, I haven't encounted such a problem with python, named parameters and easy tuples make this sort of problem a breeze.
Oh, and you're confusing strict and strong typing. Strict typing is type definition at compile time, and dynamic is at runtime.
Strong typing is the lack of implicit casts, and weak typing has implicit casting.
Oh, and saying 'hey intellisense makes this easy' is akin to 'hey this crutch makes it easy to walk with a broken leg'.
no subject
Date: 2009-04-10 09:44 am (UTC)You're right about the casting - I meant static analysis, which is the "strict" side of things.
I don't understand your problem with intellisense. Care to elucidate?
no subject
Date: 2009-04-10 10:33 am (UTC)It's a shame this is missing from Java, it seems it was planned originally: http://gbracha.blogspot.com/2007/02/tuples.html
I gather that adding this to C# would be difficult now because essentially the CLR type system doesn't have the underlying support. F# has tuples in the language but the underlying implementation is a hack.
Incidentally, has
no subject
Date: 2009-04-10 10:50 am (UTC)no subject
Date: 2009-04-10 11:50 am (UTC)If you had them in a statically typed C-like language you could do this:
And then call like this:
Note that as first-class types, tuples would also be usable for input parameters, variables, etc.
no subject
Date: 2009-04-10 12:12 pm (UTC)To put it mildly - ick!
no subject
Date: 2009-04-10 12:28 pm (UTC)The function in that style:
I'd think of them rather as anonymous C-style structures than as classes - there's nothing specifically object-oriented here.
no subject
Date: 2009-04-10 12:46 pm (UTC)That's more useful, definitely. And would do what I want.
Sold!
Now, someone needs to get on the phone to the C# boys and tell them that their anonymous classes need to work better so that they can be used this way!
no subject
Date: 2009-04-09 03:19 pm (UTC)DateTime MyDateTime = New DateTime(inputDate)
MyMonth = MyDateTime.Month
MyDay = MyDateTime.Day
no subject
Date: 2009-04-09 03:31 pm (UTC)I'll take the flexibility of being able to add properties/methods to a class (and not break existing calling-code when the class is expanded) any day, despite the extra work required to create the class.
no subject
Date: 2009-04-09 03:54 pm (UTC)no subject
Date: 2009-04-09 03:51 pm (UTC)no subject
Date: 2009-04-09 03:35 pm (UTC)It's a class of problem, though, that can be easily dealt with as a coding standards issue. So long as there's some clear standard for how input/output parameters are indicated, and that standard is followed (with the help of linting tools).
It's also possible to partially solve the "I need another class for a single return type" problem in C with a macro hack to give the equivalent of anonymous tuple types, which is sometimes handy.
no subject
Date: 2009-04-09 03:51 pm (UTC)no subject
Date: 2009-04-09 04:07 pm (UTC)no subject
Date: 2009-04-09 04:09 pm (UTC)Of course, if you have 50 methods that all do very similar things (go off and call a business service) and half of them use return values while the other half use outs then it gets confusing. Which is pretty much where my thinking started.
I'll break out the stick when I get into the office tomorrow :->
no subject
Date: 2009-04-09 03:46 pm (UTC)I'm almost tempted to say have one input parameter and one output parameter, where both of those parameters are a list of named variables. You could still deal with default variable values (if your flavour of C has them) in the header file (if your flavour of C has them) and you could still call f(g()) because the output named parameter list of g would contain the input parameter list of f.
Though I'm sure this would get messy as you'd have to then remember the actual names of all the named parameters you wanted to pass.
This might also be more difficult to optimise in terms of putting parameters into registers, though I'm sure compiler writers would find a way (maybe by having JIT recompilation based on code use like C# etc.)
And I'm sure you can do this in Lua already.
no subject
Date: 2009-04-09 03:53 pm (UTC)Which is where intellisense comes in handy. Can't remember the last time I had to look up a method call.
Lists of parameters would lose you strong typing. And I'm _so_ not giving that up.
no subject
Date: 2009-04-09 03:59 pm (UTC)Obviously you're right about intellisense, though it's not as good in C++ as in C# (or maybe MSDev 2005 isn't as hot as 2008).
A bag of input parameters would mean you'd no longer have to put all the default variables at the end and fill them in explicitly if you merely wanted the last variable to be a non-default value.
no subject
Date: 2009-04-09 04:05 pm (UTC)And C# is entirely strongly typed - which makes intellisense a lot easier. I believe C++ has issues with that.
no subject
Date: 2009-04-09 04:02 pm (UTC)Which starts to get overly complex and doesn't sound worth it :)
no subject
Date: 2009-04-09 04:23 pm (UTC)no subject
Date: 2009-04-09 06:07 pm (UTC)no subject
Date: 2009-04-10 09:43 am (UTC)no subject
Date: 2009-04-10 10:34 am (UTC)It should surely be possible to do something like
If something like this isn't possible, then there's something deeply wrong with their generics implementation.
no subject
Date: 2009-04-10 10:53 am (UTC)