Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Pragmatic rules for writing JavaScript

License

NotificationsYou must be signed in to change notification settings

madrobby/pragmatic.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

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 overfor andwhile loops
  • No curly braces for single-line control flow statements such asif & 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 usetry andcatch unless absolutely required (like an API forcing you to do so)

Pragmatic JavaScript

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.

Examples

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)}

Contribute

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.

License

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

About

Pragmatic rules for writing JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp