Coding standards can be hard to document and make everyone follow. Prompted by this tweet: https://twitter.com/HenrikJoreteg/status/741283595105402880 We should discuss following JavaScript Standard Style.

  • 2 spaces – for indentation
  • Single quotes for strings – except to avoid escaping
  • No unused variables – this one catches tons of bugs!
  • No semicolons – It's fine. Really!
  • Never start a line with (, [, or `
    • This is the only gotcha with omitting semicolons – automatically checked for you!
    • More details
  • Space after keywords if (condition) { ... }
  • Space after function name function name (arg) { ... }
  • Always use === instead of == – but obj == null is allowed to check null || undefined.
  • Always handle the node.js err function parameter
  • Always prefix browser globals with window – except document and navigator are okay
    • Prevents accidental use of poorly-named browser globals like open, length,
      event, and name.
  • And more goodness – give standard a try today!

There are a few things I don't like but the point is that they're developing a set of tools to check style based on eslint, what we're using today. I see that there is already a Semi standard style. Currently we have a pretty strict eslint config we'd need to add some extra rules on top of the standard style to make sure people don't stray away from the coding standards.

Anyway, it's up for discussion.

Comments

nod_ created an issue. See original summary.

droplet’s picture

This is just another code style with a special keyword "Standard" in their name. If we're considering the popularity that should be https://github.com/airbnb/javascript.

One style can't rule them all. We can see in PHP world that many projects started to leave PHP-FIG, eg Larval, Doctrine..

dawehner’s picture

+100 to use one of the standards out there. I guess till the time of Drupal 9 we could actually judge whether a given standard took off.

After reading both standards the airbnb one seems to take care of more stuff. It also seems to be about good practise.

We can see in PHP world that many projects started to leave PHP-FIG, eg Larval, Doctrine..

This itself though is unrelated with adopting a given PSR standard. I think they both comply to PSR-2 without thinking about it, which is well, from my point of view the entire point of coding standards: Not longer having to discuss about them and have some consistency in place.

droplet’s picture

nod_’s picture

Parent issue on the way, looks like we'll go with airbnb coding standard when we go to ES6.

nod_’s picture

Status: Active » Closed (won't fix)

Airbnb it is.

Version: 9.x-dev » 9.0.x-dev

The 9.0.x branch will open for development soon, and the placeholder 9.x branch should no longer be used. Only issues that require a new major version should be filed against 9.0.x (for example, removing deprecated code or updating dependency major versions). New developments and disruptive changes that are allowed in a minor version should be filed against 8.9.x, and significant new features will be moved to 9.1.x at committer discretion. For more information see the Allowed changes during the Drupal 8 and 9 release cycles and the Drupal 9.0.0 release plan.