GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - zerobias/javascript: JavaScript Style Guide

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

31 Commits
31 Commits

Folders and files

NameName
Last commit message
Last commit date
packages
packages
 
 
react
react
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 

Repository files navigation

Axept JavaScript Style Guide

Follow the original Airbnb JavaScript Style Guide.

This document describes only those rules which we decided to extend, to override or to add.

Other Style Guides:

  • React

TODO:

  • Make it as a shareable cards (Twitter, FB and other Social Media)
  • Apply https://google.github.io/styleguide/javascriptguide.xml

Contents

  • Comparison Operators & Equality
  • Comments
  • Naming Conventions
  • Semicolons
  • Line Width
  • Packages
  • Unsorted Yet

Comparison Operators & Equality

Extends: Section 15

  • Link Ternaries should not be as a part of expression, only whole expression.
  • TODO: find or make ESLint rule for this

Why? The following example demonstrate how it could be misunderstood.

// bad
const query = { 'forward_url': '/' }
const targetURL = 'https://qwe.com/auth?' + (typeof query === 'string') ? query : JSON.stringify(query)

// Uncaught TypeError: split is not a function
// because type of targetURL is Object
const partsOfURL = targetURL.split('?')


// may be
const query = { 'forward_url': '/' }
const queryString = typeof query === 'string' ? query : JSON.stringify(query)
const targetURL = 'https://qwe.com/auth?' + queryString


// good
const query = { 'forward_url': '/' }
const queryString = (typeof query === 'string') ? query : JSON.stringify(query)
const targetURL = 'https://qwe.com/auth?' + queryString

⬆ back to top

Comments

Extends: Section 17

  • Link Avoid using obvious comments.

Why? It makes code harder to read because you can think that here is something else than you see.

// bad - it duplicates the function name

/**
 * Update hosts
 */
function updateHosts(list) {
  hosts = list
}


// good - nothing bad is better than something bad
function updateHosts(list) {
  hosts = list
}


// bad - it duplicates the module path
import Drawer from 'react-native-drawer'

// Components
import DrawerMenu from './components/drawer-menu'


// good - see the previous "good" explaination
import Drawer from 'react-native-drawer'
import DrawerMenu from './components/drawer-menu'


// good
if (!root.isAbstract()) {
  // No need to wrap child nodes of a known concrete type.
  return nextChildren;
}

⬆ back to top

Semicolons

Extends: Section 20

  • Link Nope.
  • ESLint rule: semi

Why? Since we always have linting tools we can detect dangerous situations automatically. But this style makes us really faster according to a few years experience.

⬆ back to top

Line Width

  • Link 80 characters is good. But feel free to use up to 100 characters in a line.
  • ESLint rule: max-len

Why? Almost all developers used to 80 characters. It's a very old practice. Now we have a wide screens and we used to seat a little bit more far from monitor. In this case we can use a bit more characters and code will be still readable.

Read this and this for more details.

// bad
if ((this.props.suggestions.length === 0) && (newProps.suggestions.length > 0) && (newProps.autosuggestValue.length > 0) {

  return
}

// good
const hasSuggestions = (this.props.suggestions.length === 0)
const willHaveSuggestions = (newProps.suggestions.length > 0)
if (hasSuggestions && willHaveSuggestions && (newProps.autosuggestValue.length > 0)) {
  return
}

// good
const hasSuggestions = (this.props.suggestions.length === 0)
const willHaveSuggestions = (newProps.suggestions.length > 0)
const willHaveAutosuggest = (newProps.autosuggestValue.length > 0)
if (hasSuggestions && willHaveSuggestions && willHaveAutosuggest) {
  return
}

⬆ back to top

Naming Conventions

Extends: Section 22

  • Link Avoid using reduced names.

Why? It makes code harder to understand. Names are for readability, not to appease a computer algorithm.

// bad - what does it mean "ref"? referals? references? single reference?
const ref1 = fetch('/api/books')


// good
const reference1 = fetch('/api/books')

⬆ back to top

Imports

  • Link Use the following modules import order:

    • React
    • Relay
    • Redux
    • Tools from node_modules
    • Tools from current repo
    • Other components
    • Any kind of static assets
    • Styles
    • Constants

⬆ back to top

Packages

  • Link Try to avoid using Lodash and Ramda.

Why? Underscore and Lodash has been made when ES6 has not been so popular. Ramda do not have performance benefits, but being opinionated custom approach increase unnecessary requirements for Developers skills. Besides of that Ramda did not stay "standard de-facto".

⬆ back to top

Unsorted Yet

Use brackets for expressions

  • TODO: find or make ESLint rule for this
// bad
const useSrcKey = _srcKey !== undefined || this._srcKey !== undefined
const componentAsync = typeof src === 'function' ? src() : import(src)
const isProduction = process.env['NODE_ENV'] === 'production'

// good
const useSrcKey = (_srcKey !== undefined) || (this._srcKey !== undefined)
const componentAsync = (typeof src === 'function') ? src() : import(src)
const isProduction = (process.env['NODE_ENV'] === 'production')

Do not use JavaScript "magic"

It makes source code more portable to other languages and available for reading by other developers.

  • ESLint rule: no-implicit-coercion, no-bitwise
// bad
const b = !!a

// bad
const b = (~a.indexOf('k'))

// bad
const c = !!d.length

// good
const b = Boolean(a)

// good
const b = (a.indexOf('k') >= 0)

// good
const c = (d.length > 0)

Fetch and combinate separation

  • TODO: find or make ESLint rule for this
// bad
const targetURL = `${getHost()}/search/?query=${query}`


// good
const targetHost = getHost()
const targetURL = `${targetHost}/search/?query=${query}`

Don't use excess else for return

  • ESLint rule: no-else-return
// bad
if (rows) {
  return rows[0]
} else {
  return false
}


// good
if (rows) {
  return rows[0]
}
return false

Don't use excess empty line

  • ESLint rule: no-unexpected-multiline, padded-blocks
// bad
const config = loadConfig()

if (config) {
  applyConfig(config)
}


// good
const config = loadConfig()
if (config) {
  applyConfig(config)
}

Use dot syntax only for internal objects

Seperate importable/exportable JSON data objects and internal runtime objects.

  • TODO: find or make ESLint rule for this

Google Closure Advanced Compilation: Whenever possible, use dot-syntax property names rather than quoted strings. Use quoted string property names only when you don't want Closure Compiler to rename a property at all. For example, to export a property you must use a quoted string. However, for properties used only within your compiled code, use dot syntax.

// bad
fetch('/api/users')
  .then(response => response.json())
  .then(json => {
    if (!json.errors) {
      json.data.users.forEach(user => {
        const { id, name } = user
        if (users[id].name !== name) {
          setUserName(id, name)
        }
      })
    }
  })


// good
fetch('/api/users')
  .then(response => response.json())
  .then(json => {
    if (!json['errors']) {
      json['data']['users'].forEach(user => {
        const id = user['id']
        const name = user['name']
        if (users[id].name !== name) {
          setUserName(id, name)
        }
      })
    }
  })

It also means you can't rename stringifed keys because they are not in your code base only - in external API, data files, etc.

Don't mix JS and JSX in one line

  • TODO: find or make ESLint rule for this
// bad
return <ReferenceLink referenceKey={key} id={ref.id} title={ref.title}/>


// bad
const body = items.map(it => <li>{it.title}</li>)


// good
return (
  <ReferenceLink referenceKey={key} id={ref.id} title={ref.title}/>
);


// good
const body = items.map(it => (
  <li>{it.title}</li>
))


// good
return (
  <References>
    {items.map((it, index) => (
      <ReferenceLink referenceKey={index} id={it['id']} title={it['title']}/>
    ))}
  </References>
)

Don't mix JSX and Text node in one line

  • TODO: find or make ESLint rule for this
// bad
return (
  <Text style={style.openInSafariLink}>Open in Safari</Text>
)


// good
return (
  <Text style={style.openInSafariLink}>
    Open in Safari
  </Text>
)


// good - if you need a Text node for styling
return (
  <Text>&nbsp;</Text>
)

One empty line before and after function or class declaration

  • ESLint rule: no-unexpected-multiline

No empty line needed after function or class declaration - if the following line is javascript brackets.

// bad

const x = 0
function a() {
  return 1
}
const b = () => 2


// bad

const x = 0
class A {
  a() {
    return 1
  }
  b() {
    return 2
  }
}


// good

const x = 0

function a() {
  return 1
}

const b = () => 2


// good

const x = 0

class A {

  a() {
    return 1
  }

  b() {
    return 2
  }
}

Static members should be declared in class body

  • TODO: find or make ESLint rule for this
// bad

Profile.propTypes = {
  classes: PropTypes.object.isRequired,
  handleClick: PropTypes.func,
}


// good

class Profile extends Component {

  static propTypes = {
    classes: PropTypes.object.isRequired,
    handleClick: PropTypes.func,
  }

  // ...
}

Do not mix expression in one-line objects as assignment

  • TODO: find or make ESLint rule for this
// bad
this.setState({ hasErrors: true, route })


// bad
const state = { hasErrors: true, route }


// good
this.setState({ hasErrors, route })


// good
this.setState({ hasErrors: true })


// good
const state = { hasErrors, route }

About

JavaScript Style Guide

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.