gkz/LiveScript-style-guide
{ "createdAt": "2012-09-09T20:15:59Z", "defaultBranch": "master", "description": "Style guide for LiveScript - feel free to discuss or send pull requests", "fullName": "gkz/LiveScript-style-guide", "homepage": "http://livescript.net", "language": null, "name": "LiveScript-style-guide", "pushedAt": "2015-06-13T19:15:48Z", "stargazersCount": 73, "topics": [], "updatedAt": "2024-08-07T13:33:42Z", "url": "https://github.com/gkz/LiveScript-style-guide"}LiveScript Style Guide
Section titled “LiveScript Style Guide”This is a style guide for the LiveScript programming language.
Code Layout
Section titled “Code Layout”Indentation
Section titled “Indentation”Use 4 spaces per indentation level, not tabs, not another amount of spaces.
Blank Lines
Section titled “Blank Lines”Separate top level class and function definitions with a blank line. Add blank lines as needed for readability.
Trailing Whitespace
Section titled “Trailing Whitespace”Don’t leave any trailing whitespace.
Line End
Section titled “Line End”Don’t end your lines with semicolons. Just use a newline. Only use semicolons to separate multiple statements on a single line.
Statements per Line
Section titled “Statements per Line”Only have one statement per line.
x = 0x + yAlignment
Section titled “Alignment”Align short form switch statements:
switch| n <= 0 => []| empty list => []| otherwise => [x] ++ take n - 1, xsNaming
Section titled “Naming”Use dashes instead of camel case or underscores to name everything and access existing names.
to-upper-case = -> it.to-upper-case!Except for class names, use PascalCase for those:
class WidgetThing extends Base ...Literals
Section titled “Literals”Booleans
Section titled “Booleans”true and false are preferred over their aliases (yes, no, on, off), unless the code is much more readable using the alias.
Numbers
Section titled “Numbers”When you can, insert a number comment to specify the units, if it helps readability.
period = 7days * 52weeksStrings
Section titled “Strings”Use single quotes 'hello world', except if you need to use string interpolation or have a string with many single quotes in it, in which case use double quotes "hello #var".
List of Words
Section titled “List of Words”Use starting and ending whitespace before the first word and after the last, eg.
<[ list of words ]>not
<[list of words]>Spacing
Section titled “Spacing”Use a single space after the comma, do not use a space before the comma. Eg.
[x, y, z]Use @ for this except when it is stand alone.
Prototype
Section titled “Prototype”Use :: instead of .prototype.
Array::sliceOperators
Section titled “Operators”Spacing
Section titled “Spacing”Singly space operators, except in the case of their use in array access.
x = 1 + 2list[i-1]Aliases
Section titled “Aliases”English vs Symbols
Section titled “English vs Symbols”Use and, or, etc. over &&, || except when you need the special functionality of &&, || etc. (they do not close implicit calls, unlike and, or, etc.)
x = falsey = true(not) x or y #=> true [(not)(x) || y](not) x || y #=> false [(not)(x || y)]is not / isnt
Section titled “is not / isnt”Use isnt over is not.
Commas
Section titled “Commas”When you can, avoid commas. This means you can leave them out when the preceding item is a non-callable in a list (this includes arguments). However, keep it consistent within a call or a list. Either use commas between all items, or don’t use them at all.
[1 2 3]add-numbers 5 x[1, x, 3]Parentheses
Section titled “Parentheses”Avoid the use of parentheses whenever possible.
Do not use them when calling functions:
Math.pow 2 3You can use do instead of parentheses if you are calling against a block for instance:
some-func do prop: 3 other: 5Avoid them with chaining, access and logic closes implicit calls:
$ '#content .slider' .find 'a' .slide-up!You can avoid using them in lists by using a semicolon as a separator when a comma won’t work.
[add 2 3; times 2 3]Calling Functions
Section titled “Calling Functions”As mentioned earlier, if you can avoid using commas in the argument list, do so.
If you are calling with no arguments, use a bang call:
func!Unless you are negating or boolean casting the result, then use () as otherwise it looks funny.
!func()!!func()List Access
Section titled “List Access”Use list.0 instead of list[0]. Only use the brackets if you need to do some math, e.g. list[i+0].8
Switch
Section titled “Switch”As mentioned earlier, align your switch statements.
Short vs Long Form
Section titled “Short vs Long Form”If you can fit the body of each case on a single line, except for the otherwise case, use the short form. Otherwise, use the long form.
switch| even x => x| even y => y| otherwise => x + y
switchcase f x blah ...case g x asdf ...default ...Default
Section titled “Default”Use default with the long form.
Use | otherwise => with the short form, unless your test cases are very short, in which case you can use | _ =>
switch x| 2 => 7| 3 => 8| 4 => 9| _ => 10