-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Beginner friendly docs: the syntax notation for optional parameters #2202
Comments
🙏 I've been considering this too @Elchi3 as it seems to be repeatedly coming up in issues. I like the multiple line solution. Clarifying:
I'm assuming (and it's not at all common but) if you have two optional params where you use one and the other is required it would go something like:
|
I'd agree with this as a solution. Expecting people to understand the square bracket notation is really just adding an extra layer of complexity for them to understand. In the CSS docs we have syntax (which is equivalent to this approach) and then formal syntax (which is useful if you know how to read specs). Can we do the same sort of thing here? Example: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area |
For what it's worth, I completely agree with this solution. |
Okay, given we seem to be on the same page … |
My understanding of this proposal is that
Yes to "1". I don't really like throwing away the more compact BNF syntax (because I personally find it useful) and the inconsistency of sometimes having it and sometimes not. Just an idea, but could we include it as well? So Syntax section might look like this: // Formal syntax (BNF)
arr.slice([start[, end]]) arr.slice(); // Note sure what this means
arr.slice(start); //Slice from start index (0 based) to end
arr.slice(start, end) //Return slice from start to end-1 CSS similarly - or at the end ... /* Keyword values */
grid-area: auto;
grid-area: auto / auto;
grid-area: auto / auto / auto;
grid-area: auto / auto / auto / auto;
/* <custom-ident> values */
grid-area: some-grid-area;
grid-area: some-grid-area / another-grid-area;
/* <integer> && <custom-ident>? values */
grid-area: 4 some-grid-area;
grid-area: 4 some-grid-area / 2 another-grid-area;
/* span && [ <integer> || <custom-ident> ] values */
grid-area: span 3;
grid-area: span 3 / span some-grid-area;
grid-area: 2 span / another-grid-area span;
/* Global values */
grid-area: inherit;
grid-area: initial;
grid-area: unset;
@rachelandrew FMI, in https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area is it intentional that the syntax values refer to |
I don't really see the point of this. I think we should just stick to one way of doing it. We get so many bugs filed by people not understanding this syntax, and PRs filed to remove the brackets because people think they are a mistake... |
|
What's the general feeling on commenting the syntax itself or leaving that to the description (I decided to test this with #2351). slice() // Copy (shallow) whole array
slice(start) // Slice from start index (0 based) to end of array
slice(start, end) // Slice from start index to element before 'end' index (0 based) OR slice()
slice(start)
slice(start, end) I very slightly lean towards "no comments". I like the "at a glance" nature of having them, but then this does omit some detail from the docs that people might then not read. |
I'm also leaning towards no comments. (Ideally this would be something to user test if we had the capacities like we used to 😞.) In my view the syntax box is really just meant to lookup how to use the function if you need a reminder about the parameters. If you then see "start" and "end" and don't know what these are, well, then you need to go on reading the page, specifically the "Parameters" section. If you know about "start" and "end", well, then go back to your code editor as quickly as possible :) |
No comments. |
As discussed in mdn#2202 (comment)
OK, that all makes sense. I am thinking that perhaps we should still show the return type -ie I've tried to capture this in #2376 |
I think it is an interesting idea to add return types. It adds essential information to the syntax box which wants to be a useful, quick look-up. However, if we do this, it should be as readable as possible. I looked at Python, Rust, PHP docs, and I guess there are a at least two different ways how people do this: Add the return type in front (with or without colon, like you proposed). Add the return type at the end (to represent what comes out of the call, I assume) My feeling is that we start with making the change to multiple lines now and iterate on the syntax box in a second phase to add return types? I'd like to gather more feedback/experience with return type syntax first, before recommending something here. Does that make sense? |
The second version would lean more towards TypeScript. But if we start with using type annotations - should the arguments get annotated as well? |
I agree that this is interesting. My main concern here is that it might confuse beginners more, as they might think that the return type is part of the syntax. Surely putting the return type in a separate section like we do already is a safer bet? But yes, let's proceed with multiple lines now, and discuss this more later. |
* array.slice syntax * Remove comments from syntax As discussed in #2202 (comment)
Since we agreed upon multiple-lines, I feel like another issue for type annotations might be apt. |
I personally prefer the multi-line format instead of the cryptic Go get 'em folks! |
FWIW I would prefer to resolve whether we want type annotations in the syntax before we go forth and mass edit. Otherwise I fear we will end up with documentation with multiple formats.
This is not like confusing beginners by mixing BNF and legitimate programming language syntax. The return type and types of the arguments are part of the programming language syntax. Many docs provide a full function signature, because that is what a programmer needs to understand how to use the API. IMO it doesn't matter too much though, as long as we make sure that we explicitly always list the return types and types of each of the arguments. I prefer types to be listed, because reading signatures is an important skill for beginners to learn (much faster and easier to grok an API than wading through docs). But it is a hell of a lot more work, and perhaps not all that relevant for languages like JavaScript that wouldn't know a type if it bit them on the face :-) |
Just to confirm, the intent is to also follow the same model for single lines too right? So
would become
plus whatever we end up doing for type notation. |
@hamishwillee wrote:
The syntax values provide examples on how the CSS property is used, similar for the syntax examples at the top but for all different kinds of values. I.e. they include complete property declarations including the name and value. The formal syntax, on the other hand, reflects the BNF-like syntax used in the specifications. It only defines the valid values and doesn't include the property name. In the case of @chrisdavidmills wrote:
I'd personally be very sad if the formal syntax went away, less because I put a lot of effort into their generation and display back then (which obviously got broken by the switch to the new platform) but rather because it provides useful information on what values are allowed in a very compact way. And that information is not always reflected in the syntax values. Though I can understand that this syntax can be quite confusing if you are not used to read it. Regarding the issue about the display of optional parameters, I still prefer a condense display including square brackets. Like I wrote in the old discussion, it would then be good to have some kind of info icon near the syntax that explains its meaning. But, like with the formal syntax in CSS, it might be easier for people not familiar with that special syntax if the different possible versions are listed like you did it in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#syntax. Sebastian |
Hi @SebastianZ The consensus of the discussion was that the BNF form is deprecated and we should use the expanded form "by default". Using BNF is allowed in a separate section. See https://developer.mozilla.org/en-US/docs/MDN/Structures/Syntax_sections For myself I prefer the BNF format, but I have come around to the idea that this change is "for the better". The main reason being that we keep on getting people "correcting" the BNF in various ways. It isn't just that they confuse the brackets for array syntax, they confuse where the comma should be, and even that the brackets are some kind of odd emphasis. Now people rarely contribute, so if we have 6 changes in a month then we probably have 6000 people scratching their heads on what this means. Given that the BNF is still "allowed" an icon or something to link to an explanation would not hurt, but given it isn't the preferred way, not sure it is "a priority". Just my opinion though. PS I will close this discussion next time I come around to this, because I think it completed and further discussions on the topic - e.g. to somehow revert this, should be separate. |
Hi @hamishwillee, Right, the expanded form is the preferred way to present the syntax as it is easier to understand. It's bad that contributors try to "correct" the formal syntax as they misinterpret it. That's why I wanted to have that info icon. Before Yari, the parts of the formal syntax were actually linked to https://developer.mozilla.org/en-US/docs/Web/CSS/Value_definition_syntax, which made it somewhat easier to understand. And users will have to understand the formal syntax to some extent when they want to define custom properties via the CSS Properties and Values API. Anyway, the discussion is far beyond @Elchi3's initial intent to clarify the syntax notation for optional parameters in JS, at this point. So I'll open a separate issue to discuss that. Sebastian |
Just saw that there's already mdn/yari#2963 to fix that. Sebastian |
@SebastianZ I'd have to see some examples of that to understand why the formal syntax is required rather than expanded format for the CSS case you point to. I'm completely open to the discussion, just need an agreed set of rules I can work to. Closing this issue. |
I'm closing this issue. We've agreed on new guidelines and updated the MDN contribution docs. Thanks all! 🎉 |
Amazing job @Elchi3. I love consistency. Now we just need to get someone to do the web api docs .... |
On 30.10.2016, 18:44:36, I wrote to dev-mdc
https://groups.google.com/g/mozilla.dev.mdc/c/eS12O5mAI-A/m/J1Qd-HajBgAJ
Sadly, given MDN was a wiki, the square bracket notation is back on pages like slice and friends. Opening this issue to restart the discussion and advocating again for using multiple lines instead. This came up in #2176 where folks seem to be positive about this proposal, too.
The text was updated successfully, but these errors were encountered: