-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Current styleguide conventions with modern JS #7435
Conversation
Several of these can be covered by a linter. Documentation tends to get out of date. Maybe either remove those sections that are covered by the linter, or move the documentation into the lint config? (That way it's easier to see what needs to change when we change our practices?) |
That |
Great point about the linter, but let's take care of that as part of the followup "reorganization" instead. This way we at least get consensus on intentions from this PR and then the reorganization PR can be focused purely on the logistics. Good catch on |
726a34e
to
5499660
Compare
I added a new section to describe appropriate import scenarios: https://github.com/epixa/kibana/blob/styleguide/style_guides/js_style_guide.md#import-only-top-level-modules |
My dimes:
|
Do you mean the linux line-endings rule? What's pointless about that?
I don't have a strong opinions about it, but that does seem a little overkill. Maybe other people do feel strongly about it.
I disagree, though this whole thing will be removed when we follow up with removing items that the linter will catch.
Again, I have no strong opinions about this one. I'll make the change unless other people disagree.
It isn't a multi-line scenario, though. That was kind of the point. // good
if (true) return;
// bad
if (true)
return;
I have that in the second Arrow Functions example.
👍
👍
It's point in that context is to make up for the fact that the previous example is garbage. The previous example gets the point across, but it does so with a pretty terrible implementation. So I really should just fix the first example, so we can ditch the second.
Yeah... I'm more than happy for But given a newer, less established project, I'd happily disallow classes altogether, and I hope we'll be able to move away from them ourselves some day.
👍 |
It's redundant, I haven't seen
How is it any more overkill than using
👍 |
... | ||
}; | ||
// good | ||
[1, 2, 3].map((n) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks your no () rule.
I agree with @bevacqua. Allowing braceless conditionals only saves two characters of typing (1 or 0 if you're using a proper editor) and increases the chances of accidental breakages down the road. Re: Importing modules - we should also explicitly require the use of relative paths and disallow things like webpack aliases. Classes - This seems like a good start. Based on the feedback from my previous PR, I have some thoughts on offering users more guidance towards options that are better than classes. I can address it in another PR, unless you'd like to have that discussion here. Chaining indentation - I used to prefer the recommended style, these days I don't have a strong preference. Just wanted to say that if we make this change, our entire codebase is gonna be in violation. Staying consistent might be a lesser evil. |
How does everyone feel about deferring to the AirBnB style guide, and using this doc to only document the cases in which we deviate or extend those guidelines? The AirBnB docs are really well-written, commonly used, and contain customizable linting rules. I took a look at how closely it matches Court's revisions, and I think the two are very similar (some parts of our style guide have actually been copy/pasted from the AirBnB style guide). Here are the sections I suggest we keep, since they address topics which AirBnB doesn't:
Here are the sections which somewhat deviate or contraindicate from their style guide, so we can decide to keep them or remove them:
|
I wrote my own take as well (bevacqua/js), but I think we should just iterate over what we have rather than start again from scratch because maintainability. This sort of bikeshedding is helpful up to a point, but then it just gets in the way. |
++
can someone please enlighten me on what's wrong with classes and using |
@uboness ES6 classes and In addition to those foundational problems, ES6 classes have another huge implementation issue. Classes make encapsulation impossible. Not only do they not provide a way to create private members, they also prevent you from hiding private instance members inside a closure. To me, this is reason enough to avoid them. What's OOP without encapsulation? This issue will probably be resolved in future versions of the spec. But it won't address the above points. |
And a few others:
|
My 2 grains of sand:
|
@bevacqua Partial application in the functional programming sense: you can't create a new operation that already has some of the arguments applied. Though, as you pointed out, you can invoke the constructor with variable arguments now. |
Well, pre-ES5 if you really really wanted you could do new (Date.bind.apply(Date, [null,2015,31,8])) See:
When it comes to partial application you could consider function curried (...rest) {
return new Foo('a', 'b', ...rest);
} |
A factory has its place where/when it's needed. Having a general rule of "replace all ctors with factory methods" is plain nonsense (sorry). A factory is good when you potentially have multiple implementation of a class and you want to hide those away. Another reason to have factory methods is for convince and readability... at times a class can be instantiated in multiple ways with multiple args, and factory methods better document the nature of those.
+1 on that... has nothing to do with classes vs. functions/factories
not everything in java needs to be exposed as a class - you can still hide things behind static methods. That said, having classes provide structure to your constructs (with or without having factory methods around)
I can live with this argument (as opposed to the previous ones). That said, one could argue to prefer clearer structure over language level enforcement of visibility (not saying I do... just saying it's an option). Also, you could still use classes and work with module level variables (they are bound to the module itself, don't they?)
as mentioned above, factories serve their purpose when needed... no (and never did) arguing factories should never be used
Indeed... today these are glorified factory function and (as mentioned above) visibility is broken. The reason I raise it though is that it seems to me that JS is moving more towards adding those:
Look, I've done enough Java, OOP and built a few large scale extensible systems to know the pros & cons of classes, constructors and inheritance. I'm not here to start a war, not on JS as a language, or with JS developers or on Java & OOP. Javascript is historically notorious (whether ppl admit to that or not) for its pitfalls as a language. Traditionally, entire large JS code bases, very quickly, reached an unmaintainable state (call it "function calls and variables a la bolognese"). In recent years you clearly see a shift though... with nodejs came great responsibility and what you actually see today is JS following a very similar path to that that Java did 15 years ago. Where it tries to figure out itself and its platform/echo-system. It's not for nothing you have 50+ JS logging frameworks out there, 50+ http frameworks, 50+ IoC frameworks and every month someone declares they found a better way to develop web apps (react or not to react... that's the question.. no... angular or not to angular... wait, angular 1.x or 2.x?... ayayay... ember anyone?). ECMAScript is being enhanced all the time and following the direction it's all going now, I can't help but feeling "I've seen this before". Also, asking myself, why are modern frameworks like angularjs & react move towards the notion of classes? So with all this rumble jumble and whathaveyou... for me, the questions are very simple. Are we sure we want to ignore classes and keep using old JS style, just because it's not perfect yet? In a year time, will we regret it? Will we have this urge to rewrite the whole thing to keep it somewhat modern? Or... alternatively, we can accept the changes, accept the pros as well as their cons that come with them, but with that, make it somewhat easier for us to enhance over time, along with JS & Nodejs. |
Not to nitpick, but this PR doesn't propose disallowing the use of classes, and it makes it clear that if you are using a class, it should be a native class. |
As @epixa said this is really beyond the scope of this PR since it doesn't ban classes, but I would like to clarify a few points.
In Javascript today, I disagree. There's no use case where an ES6 class is a superior option to a factory function.
It does, because classes encourage classical inheritance which couples sub-classes to their super classes.
Yes, but there's no way to implement private instance members without some really convoluted code involving symbols or weakmaps.
I agree 100%, we should try to minimize future regret and that's why we should avoid classes for now. The beauty of factory functions is that they give us flexibility in the future. If classes turn into something awesome, we just update the factory functions to return class instances. If we use classes extensively today and classes evolve into something terrible (or a better option appears), we're screwed, because classes cannot be used without the |
That shed would look much nicer in red, don't you think? |
Red would be too prominent. Bikes wouldn't stand out. |
it's not about superiority (at least today), it's about coding style and consistency (see below)
wrong. the code need to be consistent. (as in
I definitely accept that as an argument... I'm all for less convoluted code
I can live with this statement. I don't necessarily agree one thing is easier than the other though. converting from one paradigm to another will come with a cost, potentially high cost either way. Again, I'm raising this simply as I see a pattern of where JS is heading... nothing more. This discussion could not be more relevant to this issue. This IS style... and here we should sort it out and explicitly explain (in words, in the guideline doc) why we choose one thing over another. And no... the shed would not look good in red, unless you plan to park a firetruck under it. |
@epixa We could just use On Fri, Sep 16, 2016 at 3:49 PM, Court Ewing [email protected]
https://ponyfoo.com/ · github https://github.com/bevacqua · twitter |
@bevacqua |
The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices.
Inline comments addressed, but airbnb styleguide is a separate effort than this
Alright, I've updated this to address the feedback from the inline comments, which were all about adding more clarity/rationales behind the guidelines. Even though we will be switching to the airbnb styleguide, I'm still going to push this change in the meantime so our styleguide isn't outright incorrect like it is right now. I'll push this on Monday barring any additional feedback. |
LGTM |
This looks like a great update, LGTM. Does this mean we can restart work on #8101? |
Backports PR #7435 **Commit 1:** Current styleguide conventions with modern JS The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices. * Original sha: 13e501b * Authored by Court Ewing <[email protected]> on 2016-06-12T14:03:13Z
Backports PR #7435 **Commit 1:** Current styleguide conventions with modern JS The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices. * Original sha: 13e501b * Authored by Court Ewing <[email protected]> on 2016-06-12T14:03:13Z
Backports PR #7435 **Commit 1:** Current styleguide conventions with modern JS The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices. * Original sha: 13e501b * Authored by Court Ewing <[email protected]> on 2016-06-12T14:03:13Z
Backports PR #7435 **Commit 1:** Current styleguide conventions with modern JS The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices. * Original sha: 13e501b * Authored by Court Ewing <[email protected]> on 2016-06-12T14:03:13Z
The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices.
Backports PR elastic#7435 **Commit 1:** Current styleguide conventions with modern JS The existing styleguide was in great need of a rewrite as it did not reflect the conventions we're using in the codebase or even the best practices that we follow. In some cases, the guidance it provided was outright contrary to our current practices. * Original sha: 13e501b * Authored by Court Ewing <[email protected]> on 2016-06-12T14:03:13Z Former-commit-id: e967153
`v91.3.1`⏩`v92.0.0-backport.0` --- ## [`v92.0.0-backport.0`](https://github.com/elastic/eui/releases/v92.0.0-backport.0) **This is a backport release only intended for use by Kibana.** **Bug fixes** - Fixed an `EuiTreeView` JSX Typescript error ([#7452](elastic/eui#7452)) - Fixed a color console warning being generated by disabled `EuiStep`s ([#7454](elastic/eui#7454)) ## [`v92.0.0`](https://github.com/elastic/eui/releases/v92.0.0) - Updated generic types of `EuiBasicTable`, `EuiInMemoryTable` and `EuiSearchBar.Query.execute` to add `extends object` constraint ([#7340](elastic/eui#7340)) - This change should have no impact on your applications since the updated types only affect properties that exclusively accept object values. - Added a new `EuiFlyoutResizable` component ([#7439](elastic/eui#7439)) - Updated `EuiTextArea` to accept `isClearable` and `icon` as props ([#7449](elastic/eui#7449)) **Bug fixes** - `EuiRange`/`EuiDualRange`'s track ticks & highlights now update their positions on resize ([#7442](elastic/eui#7442)) **Deprecations** - Updated `EuiFilterButton` to remove the second `.euiFilterButton__textShift` span wrapper. Target `.euiFilterButton__text` instead ([#7444](elastic/eui#7444)) **Breaking changes** - Removed deprecated `EuiNotificationEvent`. We recommend copying the component to your application if necessary ([#7434](elastic/eui#7434)) - Removed deprecated `EuiControlBar`. We recommend using `EuiBottomBar` instead ([#7435](elastic/eui#7435))
`v91.3.1`⏩`v92.0.0-backport.0` --- ## [`v92.0.0-backport.0`](https://github.com/elastic/eui/releases/v92.0.0-backport.0) **This is a backport release only intended for use by Kibana.** **Bug fixes** - Fixed an `EuiTreeView` JSX Typescript error ([elastic#7452](elastic/eui#7452)) - Fixed a color console warning being generated by disabled `EuiStep`s ([elastic#7454](elastic/eui#7454)) ## [`v92.0.0`](https://github.com/elastic/eui/releases/v92.0.0) - Updated generic types of `EuiBasicTable`, `EuiInMemoryTable` and `EuiSearchBar.Query.execute` to add `extends object` constraint ([elastic#7340](elastic/eui#7340)) - This change should have no impact on your applications since the updated types only affect properties that exclusively accept object values. - Added a new `EuiFlyoutResizable` component ([elastic#7439](elastic/eui#7439)) - Updated `EuiTextArea` to accept `isClearable` and `icon` as props ([elastic#7449](elastic/eui#7449)) **Bug fixes** - `EuiRange`/`EuiDualRange`'s track ticks & highlights now update their positions on resize ([elastic#7442](elastic/eui#7442)) **Deprecations** - Updated `EuiFilterButton` to remove the second `.euiFilterButton__textShift` span wrapper. Target `.euiFilterButton__text` instead ([elastic#7444](elastic/eui#7444)) **Breaking changes** - Removed deprecated `EuiNotificationEvent`. We recommend copying the component to your application if necessary ([elastic#7434](elastic/eui#7434)) - Removed deprecated `EuiControlBar`. We recommend using `EuiBottomBar` instead ([elastic#7435](elastic/eui#7435))
The existing styleguide was in great need of a rewrite as it did not
reflect the conventions we're using in the codebase or even the best
practices that we follow. In some cases, the guidance it provided was
outright contrary to our current practices.
For reviewers: For the most part, I recommend just looking directly
at the updated styleguide and reviewing it as if it were a new document
rather than trying to make sense of this diff.
This document really needs to be reorganized with jump links as well,
but for this first pass, please just review the content itself. We can
follow up with an additional PR that reorganizes everything.
Link directly to JS styleguide from this branch: https://github.com/epixa/kibana/blob/styleguide/style_guides/js_style_guide.md
Closes #4949