GithubHelp home page GithubHelp logo

js-style-guide's Introduction

JS Style Guide

Table of Contents

  1. Whitespace
  2. Variables
  3. Quotation Marks
  4. Conditionals
  5. Semi-Colons

Whitespace

  • Set tabs to "2 Spaces"

Variables

  • Use const declarations for all variable and function names unless there is a possibility that it will be reassigned. In the case of reassignment, use let.
  • All variables should be in lowerCamelCase, with exceptions to variables and functions that come from packages and may contain snake_case
  • When naming variables and functions, make sure to to use clear descriptions of what that variable stores or does.
    • Boolean variables should begin with is. For example, isRetro reads as "is retro(grade) true?"

Quotation Marks

  • Single quotes are used throughout JS code, with the acception of
    • Template literals
    • JSON test bodies
    • Strings containing an apostrophe or single quotes

Conditionals

if-else Statements

  • Short one-liner if statements can be written in one line.
if (pofFullDegree >= 360) pofFullDegree -= 360;

// as opposed to
if (pofFullDegree >= 360) {
  pofFullDegree -= 360;
}
  • else if and else statements should begin on the same line as the previous block's closing curly bracket.
if (distanceBetween >= 0 && distanceBetween < 45) {
  moonPhaseName = "New Moon";
} else if (distanceBetween >= 45 && distanceBetween < 90) {
  moonPhaseName = "Crescent Moon";
} else if (distanceBetween >= 90 && distanceBetween < 135) {
  moonPhaseName = "First Quarter Moon";
} else {
   // ...
}

Long Control Statements

  • When a control statement becomes too long, break each group of conditions into a new line
    • For readability, keep logical operators at the beginning of each new the line
    • Closing parentheses and opening curly are also on their own new line
// Short Control
if (nice === 123 && short === 'abc') {
  someFunction();
}

// Long Control
if (
  (nice === 123 || short === 'abc')
  && (jk > 0 && iLied < 10)
  && haha === 'sorry'
) {
  someFunction();
}

Ternaries

  • Similarly to the above stated rules,
    • Ternaries with short controls and one line functions, can be written as one-liners.
    • Ternaries with long controls and/or multi-line code blocks will be broken into nested blocks beginning with ? and :, and followed by a space.
const planet = 'mercury';
const mercury = isRetro ? true : false;
const moonPhase = getMoonPhase();

{planet === 'mercury && isRetro
  ? <div>
    <h1>Hello world,</h1>
    <p>Mercury is in retrograde.</p>
  </div>
  : moonPhase === 'full'
    ? <div>
      <h1>Good evening,</h1>
      <p>The moon is full.</p>
    </div>
    : <div>
       <h1>NO!</h1>
       <p>This is patrick!</p>
    </div>
}

⬆ back to top

Semi-Colons

  • Imports, variable declarations, and lines of code all end in a semi-colon.
  • However certain blocks of code do not. This includes:
    • Functions
    • If-else
    • For loops
const sweph = require('sweph');

function celestialBodies(julianDayET, houseOrder) {
  for (let i = 0; i < cbData.length; i++) {
    const fullDegree = swephData[0];
    const speed = swephData[3];
    const cBody = ckObj(fullDegree, speed, cbData[i], houseOrder);
    celestialBodies.push(cBody);
    
    if (cbData[i].name === "true node") {
      let snFullDegree = cBody.fullDegree + 180;
      if (snFullDegree >= 360) snFullDegree -= 360;
      const snData = { name: "south node", type: "point" };
      let southNode = ckObj(snFullDegree, 0, snData, houseOrder);
      celestialBodies.push(southNode);
    }
  }

  return celestialBodies;
}

⬆ back to top

js-style-guide's People

Contributors

h-b8 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.