- Notifications
You must be signed in to change notification settings - Fork14
Pragmatic rules for writing JavaScript
License
madrobby/pragmatic.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- 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
- Use one var statement per function, at the top
function name() { }
for named functionsfunction(){ }
for anonymous functions- Don't use more than 10 LoC per method (except for closures)
- 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
andwhile
loops - No curly braces for single-line control flow statements such as
if
& friends - Don't writesemicolons that are optional
- Put a single semicolonbefore 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
andcatch
unless absolutely required (like an API forcing you to do so)
The word pragmatic means"Practical, concerned with making decisionsand actions that are useful in practice, not just theory."1 Writing pragmaticJavaScript is all about optimizing the process of writing JavaScript for youas a programmer, using all the facilities the language provides.
Writing less code is good; emphasized by the no optional semicolons rule, byno curly braces where not necessary and by using functional programming constructswhereever 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 deservesto be treated as such. Pragmatic.js is also about discovering and learning the languagestrengths as well as its quirks so you can make full use of it.
Here are some examples fromZepto.js.
// typical helper functionfunctioneachEvent(events,fn,iterator){if($.isObject(events))$.each(events,iterator)elseevents.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){returnthis.bind(event,callback)}})// from Zepto core, $.fn.siblings implementationfunctionfiltered(nodes,selector){returnselector===undefined ?$(nodes) :$(nodes).filter(selector)}$.fn.siblings=function(selector){returnfiltered(this.map(function(i,el){returnslice.call(el.parentNode.children).filter(function(child){returnchild!==el})}),selector)}
I welcome contributions—this guide is very basic at the moment and should probably have some moreguidelines for specific situations and how to get started. You know where the fork & pull requestbuttons are.
Pragmatic.js is licensed under the terms of the MIT License.