Giter VIP home page Giter VIP logo

style-guides's Introduction

Khan Academy Coding Style Guides

We implement a style guide for our code with the intention of keeping things readable and consistent. Please do your part on the team to help keep the spirit of this consistency in both your own code, as well as politely pointing out violations in other people's code when doing their code reviews. While code prettiness should never be valued over launching or any user-visible impacting changes to the code, the idea is that maintaining a readable codebase helps things be more maintainable, and in the long run will make it easier to do the real changes that do make user-visible changes.

There may be lots of legacy files that do not adhere to the current style guide; if you're editing an old file, be consistent with what's around you.

To help adhere to these rules, some tools are available to automatically catch, and in some cases fix, style violations. See the per-language guides below for more info.

TODOs

If there is something that you want to deal with later, it’s appropriate to mark it in code. There’s an advantage to using a standard format, which is this:

# TODO(your_username): Fix this to work with frobnozzes too
# TODO(your_username): Remove this once we support quxxes (at least by Dec 2012)

The text TODO is followed by your username in parentheses. This does not mean that you are on the hook to follow through on the TODO. Rather, it means that you are the person most knowledgeable about it, so if others run across the TODO and have questions about it, they know who to talk to.

In code reviews, it is common to put in TODOs when a reviewer points out some thing in the code that could be improved, but is not necessary to do right away.

Language Style Guides

style-guides's People

Contributors

alangpierce avatar alopatin avatar andymatuschak avatar ariabuckles avatar benjaminjkraft avatar benkomalo avatar bryanjclark avatar csilvers avatar dmnd avatar elkebirmed avatar fbessho avatar hblumberg avatar iczero avatar jaredly avatar jeresig avatar jlfwong avatar lipis avatar mlsteele avatar mpolyak avatar nachosoto avatar nedbat avatar pertrai1 avatar prayagverma avatar rileyjshaw avatar samlau95 avatar siutsin avatar skeller88 avatar thenickcox avatar tzi avatar xymostech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

style-guides's Issues

Python from...import advice needs concrete example

The from...import section of the Python style guide says:

Rationale: This is the single best -- and easiest -- way to avoid the circular-import problem. To simplify, when you say import x, Python executes the x.py file, but doesn't need to get any attributes from it. When you say from x import foo, Python needs x.py to be executed enough that foo is defined in it. When x does a 'from-import' from y, and y does a 'from-import' from x, the circular import can succeed if a partial module is enough (as it will be with import x), but it can fail if the circularity happens before the needed attribute is defined (as it might be with from x import foo).

I had trouble proving this to myself with manual testing; I think this section could really benefit from a link to some concrete example code that showcases a successful example of this behavior.

Why no AJAX on server?

Just wondering. I am used to next.js, which has a getInitialProps function to AJAX on server render.

Trying to write best practices at our company, so just wanted that clarification.

update react.md to show flow usage

  • replace propTypes with props: <Props>
  • how to type children properly, e.g. string | React$Element<any> | React$Element<any>[]
  • describe order that we current lint against

update require/import section of javascript.md

  • remove sorting of require/import statements
  • blocks continues to be the same with the addition of a new block, flow type imports which should come last
  • prefer ES6 import syntax over require
  • destructuring should happen on the same line as the import, e.g. import React, {Component} from 'react'

add ternary style to javascript.md

The preferred ternary style is the following:

  • if it can fit on a single line, put it on a single line, e.g. const color = selected ? 'green' : 'orange'
  • if it can't fit on a single line, put the ? and : at the start of each line, e.g.
const result = reallyVeryLengthConditional
    ? superLongComputationOfPositiveResult()
    : superLongComputationOfNegativeResult();

const style = selected
    ? {
        color: 'green',
        fontWeight: 'bold',
    }
    : {
        color: 'orange',
    }

underscore comparison nits

Sorry for the rapid fire comments, needed to get it out before jumping into a meeting and forgetting.
Continuing the feedback from #28.

There are a couple of methods that are sufficiently complicated and don't have a direct equivalent so instead we have a custom-built copy of lodash containing only those specific methods. You can find this file at: third_party/javascript-khansrc/lodash/lodash.js along with instructions on how to build it and exactly what methods are included.

In addition, Lodash is totally modular & works great with browserify / webpack to create smaller bundles. There's even a babel plugin to avoid mucking with module paths.

var cloneDeep = require('lodash.clonedeep');
// or
var cloneDeep = require('lodash/lang/cloneDeep');

Object.assign(json, this.model.toJSON()) | _.extend(json, this.model.toJSON())

It would be better to compare to _.assign as that's closer to Object.assign
(iterating only own properties of source values instead of own+inherited in _.extend).

for (const [key, val] of Object.entries(obj)) {} | _.each(obj, fn)

In Lodash there's _.forOwn and _.forIn for object iteration too

defer | setTimeout(fn, 0); | _.defer(fn);
delay | setTimeout(fn, 2000); | _.delay(fn, 2000);

The useful bit for _.defer and _.delay is they allow partially applying arguments to the deferred/delayed function, which older environments lacked in setTimeout (may be worth noting).

bindAll | `obj.method = obj.method.bind(someObj);

Typo, fix to obj.method = obj.method.bind(obj);

once

{
    method: () => {
        if (this._initDone) { return; }
        this._initDone = true;
        ...
}

This will assign _initDone to the outer this so not really appropriate for per method state.

Update react.md to use imported `Component`

I believe it's safe to say, after the internal vote, that people prefer:

import React, {Component} from "react";

class Thing extends Component { ... }

over

import React from "react";

class Thing extends React.Component { ... }

The examples in react.md all use the React.Component style, so we should update them to just extend Component. We can also add a blurb about the import convention?

cc @kevinbarabash

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.