Repository navigation
Description
dotnet/corefx#26582 Added a new 'Zip' extension for IEnumerables with teh following signature:
public static IEnumerable<(TFirst First,TSecond Second)> Zip<TFirst, TSecond>(
this IEnumerable<TFirst> first, IEnumerable<TSecond> second)The capitalization of the tuple members names (i.e. First/Second) was somewhat surprising to me as that wasn't the naming pattern that I thought we generally followed when creating and presenting tuples, and was not the naming pattern we've used in dotnet/roslyn itself.
The intuition here was that tuples very commonly act as a shorthand way to bundle lightweight data together so they can easily be grouped and passed in and out of methods without hte heavyweight need to define an entire struct (and all the rest of the ceremony you would need there).
This naturally leads to two reasonable interpretations of things that could influence naming decisions:
- this is just a simpler way of dealing with a named struct. These are fields, and as such should likely be named similar to public mutable fields/properties (so presumably
PascalCased). - these are a collection of parameters. The tuple itself is intended to fade out of the way, and people will most naturally think of working with these as a bunch of parameters/locals. As such, they should be named similar (so presumably
camelCased).
IMO, from my recollection of the design here in the LDM, we'd been pushing harder on the second position. We've put a lot of effort into making it feel very natural to just 'splat/deconstruct' tuples, pushing the positoin more that the tuple is less relevant, and it's just a bundle of variables you'll be working with locally in your methods. While it's true that that doesn't necessarily mean the tuple field names have to match the expected usage names, it feels unfortunate that these names would now have to differ in case. This goes for both consumption, where field names are being splatted into different local names, as well as construction. It's extremely natural for someone to just construct a tuple like so: (name, age), but this would, for example, now create a tuple with names that don't actually match a method that returns something like (string Name, int Age). This lack of symmetry obviously is not a problem in terms of compilation (the language allows and converts here). However, it feels problematic to me because the code is now no longer working consistently with the same names that flow in, get worked on, and flow out.
This is not a huge deal. But i think it warrants discussion as any decision made here will effectively impact all future usages of tuples across our APIs and our customers' APIs.
Thanks very much.
--
Note: additional data points. Both swift and go allow one to return multiple named values from a method/function. In both those cases, naming of those multiple-named values matches parameter naming.
Activity
CyrusNajmabadi commentedon Nov 16, 2018
Tagging @stephentoub @agocke @sharwell @bartonjs
CyrusNajmabadi commentedon Nov 16, 2018
Also curious what @MadsTorgersen @DustinCampbell have thought on this topic. You two have demo'd this stuff a bunch at conferences. Do either of you have a preference on how these things are most naturally thought about and named? Thanks!
[-]Determine recommended casing/naming for tuple members in corefx.[/-][+]Reconsider recommended casing/naming for tuple members in corefx.[/+]stephentoub commentedon Nov 16, 2018
I'm not sure why that's unfortunate. It's exactly the case today when storing the value of a public field/property on a type returned from an API into a local: the former is PascalCased, the latter camelCased.
I'm not sure who "we" is here, but we've never pushed on a position here for public .NET APIs. Folks can do what they want in the internals of their code/repos, but when we start talking about public APIs, there needs to be crisp, clear guidance, which is what we tried to do as part of this discussion.
There are three possible outcomes here from a framework design guidelines perspective:
I really, really, really dislike (3); I literally would rather we not ship the API at all (e.g. the new overload of Enumerable.Zip) than do that. From an API perspective, these are structs with public named property/fields, and the only reason to use a ValueTuple instead of a custom struct is to save on some effort in defining another one (and maybe there's an argument about binary size of the same generic instantiation could be reused). From that perspective, camelCasing completely breaks with every other public .NET API, where types returned from methods expose their data with PascalCasing, period.
I don't have strong preference between (1) and (2), though I generally lean towards (1).
stephentoub commentedon Nov 16, 2018
cc: @terrajobst
stephentoub commentedon Nov 16, 2018
ps @CyrusNajmabadi, thank you for opening the new issue. :)
stephentoub commentedon Nov 16, 2018
cc: @KrzysztofCwalina
CyrusNajmabadi commentedon Nov 16, 2018
I tried to explain that bit, but probably did a poor job. Sorry!. In this case, because the idea (to me at least) is that really is strongly just a bundle of these values. I've definitely acknowledged that these can just be thought of as nothing more than a simpler way to right a named-struct. But i think that's losing out somewhat on what tuples are really trying to get across, which is that they really can just 'fade out' and you aren't thinking of them at that level.
i.e. without question, under the covers this is a real struct. Not question about it. But tuples are intended for lots of code to help you avoid even needing to think about that. Instead of thinking about it as a struct with these public fields, it's more like "here's just these few pieces of data" and the tuple fades out.
The 'fading out bit' was a pretty intentional part of the design, and why we def invested in this like deconstruction. It wasn't necessary, and it's def not even that bad to just use the actual tuple instance value. But there was a strong enough push on the belief that the tuple itself wasn't relevant, but what was inside was what mattered the most. In this view of the world, the conception of the world is not that there's this struct with fields that you're using. It's just that you're flowing individual values along. And, just as parameters flow in as lower-cased-named data into a method, a tuple would allow you to flow lower-cased-named data out of the method.
Another way of thinking about it: if we allowed you to name a return value of a method, would we intuit that that name should match how we do parameters (since it's just named data flowing in/out of the method)? Or would we name the data that flows out with a capitalized name? I honestly don't know :) But my gut tells me we would cameCase. And since the tuple is just a bundle of those named-return-data values, camel-casing makes the most sense there to me.
Sorry, i meant: during teh original designs of tuples and the general philosophy around them when we did things in the LDM. But i could be totally off base here and this might have just been my own perception on how I personally thought about tuples. Would def like @MadsTorgersen @DustinCampbell to weigh in. @jcouv and @gafter were also heavily involved IIRC. i'm curious their thoughts too :)
Absolutely. No question :) That's why i wanted to have this discussion. I'd love totally clear guidance here. And i'm 100% willing to accept that if corefx just cements the position of "they're pascal cased. discussion done" :)
I'm just wanting to bring up the topic because i personally feel that this warrants at least a second look and some thought as to how we expect people to use these API, and what might feel more natural within those expectations. I personally have felt that camelCasing fits best here (and the adoption across the roslyn codebase itself has seemed to back that up), and i def think ti would make sense for corefx as well. If you guys agree, great. If you think they should be PascalCased, that works for me as well :)
Thanks!
stephentoub commentedon Nov 16, 2018
Thanks.
CyrusNajmabadi commentedon Nov 16, 2018
Note: since there are two seemingly valid (in my mind at least) ways to think about things, i'm not sure i can add anything else helpful. There's no right/wrong answer here, so it's probably better not that you've heard my perspective to maybe re-assess and make a decision if anything needs to change here. If you land on keeping the status-quo have no problem on that.
This will hopefully prevent a ton of posts that are just going in circles :D
If you need any more info/perspective from me, don't hesitate to ask. But i'll leave it in your hands now since i don't think i have much more to add. Thanks!
agocke commentedon Nov 16, 2018
The only thing that doesn't seem an option to me is
The point of convolution is to return a tuple. I originally had the tuple unnamed. I'm not sure why we needed to get into this debate at all.
stephentoub commentedon Nov 16, 2018
From a consumption standpoint, I don't know what "unnamed" means. The properties/fields would then be called Item1 and Item2, so they have names (and they're PascalCased).
agocke commentedon Nov 16, 2018
C# refers to a tuple without names in the syntax as an "unnamed" tuple. It has nothing to do with property names.
stephentoub commentedon Nov 16, 2018
Ok. But this is about how the API is consumed, and whether the thing that's returned has members:
I don't see how (1) is better than (2), and I don't like (3) :)
agocke commentedon Nov 16, 2018
If it's unnamed in the syntax we can't have this debate as the only thing you can have is (1). The point is to not talk about this.
52 remaining items
gafter commentedon Nov 19, 2018
What's wrong with using
KeyValuePair?yaakov-h commentedon Nov 19, 2018
Well, named tuples are a language feature, so they would be specific to the language that defines them, no?
AFAICT:
I can understand a "do not use names" or "do not use VT" rule for reasons to do with the BCL having to support multiple languages... but I don't see any language pushing for PascalCased(-only) tuple names.
It appears to be only BCL developers, which puts CoreFX at odds with all it's major consumers... for ideological reasons not adopted by any language design team?? 😕
jnm2 commentedon Sep 12, 2019
Looks like precedent has now been set that tuple element names should be pascal-cased when used as a return type: dotnet/corefx#35595
TonyHenrique commentedon Sep 12, 2019
I like Pascal Case. Everywhere.
jnm2 commentedon Sep 12, 2019
I take it we shouldn't call you @tonyHenrique then =)
terrajobst commentedon Sep 13, 2019
Yes,, @bartonjs is working on guidelines for tuples, it basically is:
ValueTuple<>bartonjs commentedon Sep 14, 2019
It's more "If you use tuples, use named tuples." (which happens to mean ValueTuple + attributes). Returning (unnamed) ValueTuple is a no-no.
DavidArno commentedon Sep 14, 2019
@terrajobst,
It’s extremely disappointing that the BCL team are in any way trying to offer guidelines on a language feature. All three guidelines are unhelpful and miss the point of tuples completely.
Whether to use a tuple or custom struct/class is both subjective and dependent on many factors. A simple “prefer one over the other” guideline is a nonsense.
If you use tuples, use the
()tuple notation. What the underlying type is for that notation is not relevant to 99% of use cases and so should not be mentioned in any guidelines.And as this thread shows, the BCL desire to
name tuple elements using PascalCase runs against convention and just creates unnecessary conflict. At best you should use camelCase, failing that just keep quiet on the subject and leave folk to choose.
NickCraver commentedon Sep 14, 2019
There must be guidance for their usage in the BCL to keep the BCL APIs consistent. That is the BCL team's job. This a naming convention, the BCL already defines many of them (his is nothing new) to keep the BCL consistent.
It should be noted that "using PascalCase runs against convention" is not a given - it may go against your convention but as this thread shows there isn't one. For example, we use Pascal internally. There needs to be a defined convention in the BCL, which is why this was a blocker to exposing APIs using them.
Try to imagine for a moment if any other external API in the BCL didn't have a naming convention and where we'd be.
danmoseley commentedon Sep 14, 2019
@DavidArno it has always been part of the role of the .NET Libraries team (going back to the original Framework Design Guidelines) in collaboration with others such as the language designers and the community, to provide guidelines intended to help create better, more consistent.NET libraries.
jcouv commentedon Sep 14, 2019
@DavidArno I can attest that this is the result of a joint discussion of representatives within the .NET team, including language/compiler and BCL. Like many such decisions, it is not easy, but I think it is ultimately helpful to have an overall agreement and consistent approach. So even though we didn't land on my initially preferred approach, I'm glad we landed on a decision and I'm satisfied with it.
CyrusNajmabadi commentedon Sep 14, 2019
I agree with @jcouv . This is definitely not my preferred approach. However, my major desire was that there be a joint conversation with the relevant stakeholders on the matter. That appears to have been done. And, like with many decisions, not everyone would be satisfied. However, everyone got to have their voice heard and the decision was made understanding the ramifications. That's good enough for me.