Modernization of (static) code examples #143
-
Hi all! Our JS documentation dates back to ES3. We completely revamped it around 2014 (to ES5), and since then, we have kept up with new features in JS. Keeping up means documenting the new features, but not necessarily updating all the previous docs to use these. Note that we don't want to use the fancy new features immediately everywhere as it takes a few years before they really become mainstream. Nevertheless, a lot of our examples feel outdated, and beginners reading or using them will become used to outdated habits, which is not our goal. As an example, if you don't use We at Open Web Docs have started to update these examples, feature by feature (e.g. using I would like to launch the discussion here on the JS features we should use everywhere it is possible (and how to detect MDN pages that need to be updated). The idea is to create small-to-medium-size projects to fix them little by little. What are the drawbacks? Given we will only use features that are widely supported, and not jump on the latest addition right away, there are very few negative points: mostly it will make the life of devs needing to support IE a bit more difficult, but with its obsolescence now officially acted, and most of these features already used in every blog and all over the web, it shouldn't bother us much. I have a few entries to launch the discussion (I will update the list as the discussion progresses).
Additions from the discussion here:
Already in our guidelines, but not 100% enforced:
Rejected:
Proposed:
Note: Once we agree on a set of modernization, we must update our generic JS writing guidelines. Principles. As the discussion progressed, we surfaced a few principles that we use to guide our decision. It will be useful to list them explicitly in the guidelines:
There are probably other modern features we should start using. Any proposals? |
Beta Was this translation helpful? Give feedback.
Replies: 23 comments 81 replies
-
Also
|
Beta Was this translation helpful? Give feedback.
-
One question that comes to mind is, How much of this might we be able to check with existing linters or static-analysis tools? I’m not familiar enough with the relevant JavaScript tools to know what the current capabilities are — but as for a prioritizing what things we should try to modernize first, I think it’d make sense to first do the things that we can check for with automated tools |
Beta Was this translation helpful? Give feedback.
-
This really depends on "what" kind of feature it is. Modern JavaScript tooling almost always assumes existence of transpilers/polyfills, so using bleeding-edge features is not a big deal—e.g. |
Beta Was this translation helpful? Give feedback.
-
Just a question (maybe I'm not yet awake). When should we use I think the advantage Is there a difference in performance? |
Beta Was this translation helpful? Give feedback.
-
Should we also fix function definitions that could use the method definition syntax [ES6/2015]? |
Beta Was this translation helpful? Give feedback.
-
We don't have many cases of these, but I think we should explicitly ban – and fix the few occurrences – variables named using the Hungarian notation. I used it in C++ (during another life) and with static typing, it was a pain in the neck. Not something for dynamically typed language, IMO. |
Beta Was this translation helpful? Give feedback.
-
I propose to add: |
Beta Was this translation helpful? Give feedback.
-
With @Josh-Cena, we found a place where the new Nullish coalescing operator was handy and clearly a new idiom. link to the concrete example It replaces some occurrences of the two idioms that were previously used:
by
A usual case is combined with the optional chaining operator:
It is a much newer feature though: ES2020, but well-supported: The optional chaining operator is also ES2020 and well-supported: |
Beta Was this translation helpful? Give feedback.
-
I don't think there is much work to be done, and I think it is already in use on MDN, but I think we should list (to explicitly allow it):
|
Beta Was this translation helpful? Give feedback.
-
@hamishwillee, I noticed you used object destructuring and I think we should add it to the list of modernization we would like to perform (even if it is less common): Your example, combined with an implicit return in an arrow function.
|
Beta Was this translation helpful? Give feedback.
-
I would like to suggest the logical assignments ( |
Beta Was this translation helpful? Give feedback.
-
I also would like to use the numerical separators where possible: |
Beta Was this translation helpful? Give feedback.
-
What about optional catch binding: instead of |
Beta Was this translation helpful? Give feedback.
-
Yes, the three syntaxes above are definitely worth using. |
Beta Was this translation helpful? Give feedback.
-
Strangely, I think we have never stated this...
A linter will find and mark them, but I think we should explicitly state it. (This discussion will lead to some meta-docs describing what we discuss here and some actions to fix the actual content. |
Beta Was this translation helpful? Give feedback.
-
Not sure if this came up: Lebab is the opposite of Babel and might help with some of the mechanical parts. |
Beta Was this translation helpful? Give feedback.
-
has anybody verified the claim that you will not get the job nowadays if you use I have no objection to using modern syntax in the examples but I would (personally) want that claim to be backed up a bit before I did a ton of work to manually convert and test the current correctly-written examples using still-perfectly-valid javascript. It seems more sensible to use modern syntax when adding or updating examples but accepting the old examples as they are. Updating pages for ancient apis to reflect modern coding styles does no good for the job applicants - would you also not get the job if you use |
Beta Was this translation helpful? Give feedback.
-
To be honest most of the things listed in the original post are unfounded, especially this statement: As an example, if you don't use for...of when possible during a job interview, you don't get the job nowadays; For example, arrow functions and "traditional" functions are two separate things with different behavior, or This whole post seems to be more like a style guide than anything, it's not like some of the old syntaxes are deprecated (or will be in the near future) and can't be used anymore, if that would be the case I could see a reason for this post. |
Beta Was this translation helpful? Give feedback.
-
This doesn't sound like a good idea for beginner's documentation. |
Beta Was this translation helpful? Give feedback.
-
I think we should update the pattern For example:
Would become the following, which is much easier to read and understand, even for a beginner:
I think a significant amount of usage for |
Beta Was this translation helpful? Give feedback.
-
I'd like to vote for linting to require argument parentheses, sometimes having them and sometimes not is an unnecessary confusion that serves no purpose. They should be there all the time for consistency and let the build processes deal with them as necessary. |
Beta Was this translation helpful? Give feedback.
-
The ESLlint enforces the use of double quotes |
Beta Was this translation helpful? Give feedback.
-
We have completed this project. The Guidelines have been updated, and we have fixed most errors in mdn/content. There is always more to be done, so feel free to submit PRs! Thank you all for your help. It was an amazing journey! From this vivid discussion to the 50+ PRs and the code guideline revisions. At some points, we'll want to fix code outside the mdn/content repository, but this will be for a separate project. Thank you all. You rock! 🎉 |
Beta Was this translation helpful? Give feedback.
We have completed this project. The Guidelines have been updated, and we have fixed most errors in mdn/content.
There is always more to be done, so feel free to submit PRs!
Thank you all for your help. It was an amazing journey! From this vivid discussion to the 50+ PRs and the code guideline revisions.
At some points, we'll want to fix code outside the mdn/content repository, but this will be for a separate project.
Thank you all. You rock! 🎉