• Stars
    star
    381
  • Rank 108,473 (Top 3 %)
  • Language
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pragmatic rules for writing JavaScript

Pragmatic.js code style guidelines

TL;DR

General coding:

  • Use long, descriptive variable and method names
  • Use blank lines to separate "paragraphs" of code for readability
  • Use comments to describe non-obvious code behavior
  • Don't write comments that do not add to code understanding
  • Optimize for performance only if there's a measurable problem
  • If a source file is longer than 200 LoC, it's time to split it up

Functions:

  • Use one var statement per function, at the top
  • function name() { } for named functions
  • function(){ } for anonymous functions
  • Don't use more than 10 LoC per method (except for closures)

Statements & expressions:

  • Use short and concise expressions
  • Use duck-typing and rely on unit tests rather than testing for types of arguments
  • Prefer functional programming over for and while loops
  • No curly braces for single-line control flow statements such as if & friends
  • Don't write semicolons that are optional
  • Put a single semicolon before statements that start with ( or [ (see above article as for why it's needed)
  • Use ternary when it is simple enough as to not make code difficult to understand
  • Do not use try and catch unless absolutely required (like an API forcing you to do so)

Pragmatic JavaScript

The word pragmatic means "Practical, concerned with making decisions and actions that are useful in practice, not just theory."1 Writing pragmatic JavaScript is all about optimizing the process of writing JavaScript for you as a programmer, using all the facilities the language provides.

Writing less code is good; emphasized by the no optional semicolons rule, by no curly braces where not necessary and by using functional programming constructs whereever possible.

At the same time, when you come back to your code later, you want to understand it— thus long, descriptive variable and method names, and comments where necessary.

JavaScript is a modern, flexible and malleable scripting language, and it deserves to be treated as such. Pragmatic.js is also about discovering and learning the language strengths as well as its quirks so you can make full use of it.

Examples

Here are some examples from Zepto.js.

// typical helper function
function eachEvent(events, fn, iterator){
  if ($.isObject(events)) $.each(events, iterator)
  else events.split(/\s/).forEach(function(type){ iterator(type, fn) })
}

// shortcut methods for `.bind(event, fn)` for each event type
;('focusin focusout load resize scroll unload click dblclick '+
'mousedown mouseup mousemove mouseover mouseout '+
'change select keydown keypress keyup error').split(' ').forEach(function(event) {
  $.fn[event] = function(callback){ return this.bind(event, callback) }
})

// from Zepto core, $.fn.siblings implementation
function filtered(nodes, selector) {
  return selector === undefined ? $(nodes) : $(nodes).filter(selector)
}

$.fn.siblings = function(selector){
  return filtered(this.map(function(i, el){
    return slice.call(el.parentNode.children).filter(function(child){ return child!==el })
  }), selector)
}

Contribute

I welcome contributions—this guide is very basic at the moment and should probably have some more guidelines for specific situations and how to get started. You know where the fork & pull request buttons are.

License

Pragmatic.js is licensed under the terms of the MIT License.

More Repositories

1

zepto

Zepto.js is a minimalist JavaScript library for modern browsers, with a jQuery-compatible API
HTML
15,001
star
2

keymaster

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
JavaScript
6,528
star
3

scriptaculous

script.aculo.us is an open-source JavaScript framework for visual effects and interface behaviours.
HTML
2,320
star
4

vapor.js

The World's Smallest & Fastest JavaScript Library
JavaScript
1,364
star
5

dom-monster

JavaScript
563
star
6

emile

emile.js is a no-frills stand-alone CSS animation JavaScript framework, named after Émile Cohl, early animator.
JavaScript
530
star
7

scripty2

scripty2: for a more delicious web
JavaScript
514
star
8

textorize

Ruby
236
star
9

semicolon.js

A more secure. stable and reliable version of vapor.js
JavaScript
227
star
10

zaru

Filename sanitization for Ruby
Ruby
221
star
11

creditcard_js

A creditcard validation library for JavaScript using Prototype
JavaScript
184
star
12

downtime

Standardized JSON API to indicate downtime for apps and services
JavaScript
90
star
13

prototype_helpers

Various bits and helpers for Prototype and scriptaculous, that might prove useful or not. :)
JavaScript
75
star
14

bitarray.js

Pure JavaScript bit array/bitfield implementation
JavaScript
53
star
15

spark_pr

Pure Ruby sparklines.
Ruby
41
star
16

purrson-icon

39
star
17

creditcard_js_underscore

A port of creditcard_js to underscore.js
JavaScript
23
star
18

secure.js

JavaScript
20
star
19

jive

HTML
17
star
20

js-animation-course

JavaScript
15
star
21

gifs

Shell
14
star
22

blink

12
star
23

zeptojs.com

JavaScript
12
star
24

box2d-js

Sprocketized version of box2jd-js
JavaScript
11
star
25

dotvim

Vim Script
11
star
26

ponymizer

Generator of random pony names to be used as friendly and fun test data
Ruby
10
star
27

jslogocrazyness

6
star
28

js1k-finals

6
star
29

madrobby.github.com

5
star
30

acornkittens

JavaScript
5
star
31

yotta

A heavily-optimized JavaScript library with a large impact
HTML
5
star
32

plastic-clock

CSS
3
star
33

schnitzelconf-intro

1
star