-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Player Code JavaScript Style Guide
Nick Winter edited this page Oct 20, 2016
·
8 revisions
We're teaching people JavaScript. Therefore it is important that we have a consistent and clear coding style that is as close to standard JavaScript as possible. Every Artisan should apply this coding standard in her levels to keep the code through all the levels consistent.
Just basically follow the Node style guide, but with these exceptions and additions:
- The basic indention is four spaces. Tabs are not to be used at all.
- Try to keep lines to 60 characters or less. (We are working with very narrow code editors sometimes.)
- Use double quotes.
- Single spaces between binary operators, like
food < 5
. - No extra spaces in array or object literals, except between object keys and values.
// Bad
var array = [ 1, 2, 3 ];
var object = { x:30, y:30 };
// Good
var array = [1, 2, 3];
var object = {x: 30, y: 30};
- Always put
else
on its own line. - Use
index++
, not++index
. - Don't use the ternary operator.
- Capitalize comments like sentences.
- Avoid single letter names (
x
,i
, etc.), unless you need to do a for-loop on one line and a longer name won't fit into 60 characters. Be descriptive with your naming. - Event handler functions should be prefixed with the word
on
, likeonPetSpawn
- Try to declare local variables as near to their use as possible; try to initialize every variable.
// Bad
function foo(x) {
var a = 5;
if(x < 5) {
return x * x;
}
return x + a;
}
// Good
function foo(x) {
if(x < 5) {
return x * x;
}
var a = 5;
return x + a;
}
- Do not compare
x == true
orx == false
. Usex
or!x
instead. - Compare objects to
null
, numbers to0
or strings to""
if there is chance for confusion. - Use
==
and!=
instead of===
and!==
. - Use shortcuts.
// Bad
if (name != '') {
// ...stuff...
}
// Good
if (name) {
// ...stuff...
}
// Bad
if (collection.length > 0) {
// ...stuff...
}
// Good
if (collection.length) {
// ...stuff...
}
Summary of the Node style guide rules we're using
- No trailing whitespace.
- Use semicolons.
- Opening braces go on the same line.
- Declare one variable per var statement.
- Use lowerCamelCase for variables, properties and function names.
- Use UpperCamelCase for class names.
- Use UPPERCASE for Constants.
- Use trailing commas in Object/Array creation.
- Put short declarations on a single line.
- Only quote Object keys when necessary.
- Use descriptive conditions.
- Write small functions.
- Return early from functions.
- Name your closures.
- No nested closures.
- Use
//
for comments, not/* */
.
CodeCombat | Home | Blog | Forum | Teachers | Legal | Contribute
- Home
- Archmage Home
- Artisan Home
- Diplomat Home
- Archmage General
- Mission statement
- Coco Models
- Coding Guidelines
- Cookbook
- File system
- JSON Schema
- Technical overview
- Testing
- Third party software and services
- Artisan General
- Building A Level
- Coding Guidelines for Artisans
- Editing Thang Components
- Important Artisan Concepts
- Keyboard Shortcuts
- Debugging
- Artisan How-To Index
- Diplomat General
- i18n
- i18n Glossary nb
- i18n Glossary ru
- i18n Glossary es-419
- Dev Setup
- Dev Setup: Linux
- Dev Setup: Windows
- Dev Setup: Mac
- Dev Setup: Vagrant
- Dev Setup: Issues
- Game Engine
- Component
- Multiplayer
- Surface
- System
- Thang
- Thang Component System
- Tome
- World
- Artisan Tabs
- Components And Systems
- Scripts
- Settings
- Thangs
- Other
- Aether
- Client models
- Developer organization
- Educational Standards
- Events, subscriptions, shortcuts
- Chat Room
- Chat Room Rules
- Permissions
- Project Ideas List
- Treema
- Versioning
- Views