Returning values is harder than it could be. #142
Replies: 14 comments
-
I can see this being useful when encapsulating an existing public bool TryAdd(int key, string value)
{
returning bool success = _encapsulatedDictionary.TryAdd(key, value);
if (success)
{
Console.WriteLine($"Added value: {value}");
}
} |
Beta Was this translation helpful? Give feedback.
-
How about |
Beta Was this translation helpful? Give feedback.
-
yield int result = 1; is not valid code. |
Beta Was this translation helpful? Give feedback.
-
@michaelcheers I know it's invalid code today but it can be valid tomorrow though much like your code, also, just because atm it's associated with one thing doesn't mean that tomorrow it can't be associated with another thing. :) |
Beta Was this translation helpful? Give feedback.
-
@eyalsk, oh I thought you meant that this was feature duplicate, yeah |
Beta Was this translation helpful? Give feedback.
-
Why not go the whole way and support embedding the entire thing in the for-loop: static int Fibonacci(int upTo) =>
for (var result = 1, var lastNum = 0, n = 0;
n < upTo || return result;
oldResult = result, result += lastNum, lastNum = oldResult, n++); Or to put it another way, IMO you are making the code less readable, just to save typing four extra characters. |
Beta Was this translation helpful? Give feedback.
-
I don't mind this feature but I agree with @DavidArno it can make the code less readable, especially for people that are too used to have everything that needs to be returned at the return statement. |
Beta Was this translation helpful? Give feedback.
-
VB used to have a feature similar to this. Instead of designating a specific variable as the value to be returned it would allow you to assign values to the function name and that value would be returned from the function. Having been a VB programmer at that time I can vouch that this feature definitely made code more difficult to read. The calculation that would produce the result could be far removed from the function exit. Worse, just like this proposal, that feature allowed for the Fortunately with VB.NET that feature had been removed from the language and now you have to |
Beta Was this translation helpful? Give feedback.
-
@HaloFour No, this feature is still in VB.Net. |
Beta Was this translation helpful? Give feedback.
-
Yes, I just noticed. I had (blissfully) forgotten the syntax when I tried it again in VB.NET so I was trying to blend it with Either way, from my experience with this feature in VB, I'd argue against implementing it in C#. It's simple enough to declare a tallying variable if you want one, and you should have to be explicit with when and where your function may exit, particularly if it's supposed to be producing a value. |
Beta Was this translation helpful? Give feedback.
-
As to the other element of the proposal, that is the implicit return type. In my opinion the method signature represents the contract which is much more important than the method body itself. Having the return value derived from whatever combination of these public auto Foo(bool condition) {
if (condition) {
returning int result = 123;
}
else {
returning int result = 456;
}
} Does that method return Probably the only time I could see allowing for inference of the return value of a method might be when it has an expression body as at least the compiler could determine the type of that expression. |
Beta Was this translation helpful? Give feedback.
-
After posting my comments about "reducing readability just to save a few characters", I felt a twinge of hypocrisy, as I often bang on about "reducing noise" by reducing the amount that must be typed... Thinking about it though, I've realised there is no hypocrisy. For me, the key is the old adage that "code is written once and read a thousand times" (or some such, I just made those words up). For me, good proposals aren't about "I often find myself having to write...", but "I often find myself having to read...". Proposals for language enhancements that reduce the amount I must read to understand the code are good. Those that simply reduce the amount I must type, yet increase the difficulty of reading the resultant code, are not so good. I feel that both the use of |
Beta Was this translation helpful? Give feedback.
-
@HaloFour, I think that code would be invalid because |
Beta Was this translation helpful? Give feedback.
-
They are being declared, each within a separate block within the method. Are you saying that these variables have to be declared at the top of the method? That goes against the C# convention of only declaring variables where they are being used. |
Beta Was this translation helpful? Give feedback.
-
I find myself writing
a lot. So, I think there should be a new feature which lets a variable be a result.
So, because we put returning, it returns it at the end of the function. Also, so if you have to return something somewhere else: you could use
So,
return;
would return the "returning" values.Beta Was this translation helpful? Give feedback.
All reactions