Git Product home page Git Product logo

livescript-style-guide's Introduction

LiveScript 語法指南

翻譯自: https://github.com/gkz/LiveScript-style-guide

本語法指南是針對 LiveScript(http://livescript.net) 所撰寫。

程式碼佈局

縮排

每一階層不多不少地使用 4 個空白 代表縮排,不使用 tab。

空白行

最上層類別定義與函數宣告之前至少需要一列空白,並且可以視閱讀上的需要再增加。

尾隨空白

不要留下任何尾隨空白。包含程式碼、註解文字之後,以及空白行中都不要留下空白。

Tip
譯者註

因為版本管理系統不懂得忽略這種空白,使得這些空白會讓版本差異的比對結果難以閱讀。

行尾

不要在行尾加上分號 (;) ,只要使用換行字元即可。分號只用來分隔合併於同一行的多個陳述式。

每行陳述式

一行只包含一個陳述式。

x = 0
x + y

對齊

將較短版本的 switch 條件式排列整齊:

switch
| n <= 0     => []
| empty list => []
| otherwise  => [x] ++ take n - 1, xs

命名

使用連字號 (-) 而不採用駝𡶶式或下底線 (_) 來進行命名、取用變數。

to-upper-case = -> it.to-upper-case!

其中的例外是類別,使用帕斯卡命名法(大駝峰式)。

class WidgetThing extends Base
  ...

語義

布林值

truefalse 與它的別名 (yes, no, on, off) 相比更能表現語意,除非別名更有助於程式閱讀,否則優先使用它們。

數值

如果可以的話,只要能夠讓程式更容易閱讀,就在數值加上單位的描述。

period = 7days * 52weeks

字串

使用單引號 (') 像 'hello world' 來包覆字串,除非字串本身有許多單引號,或是裡面帶有要組合進去的變數,像是 "hello #var"

清單

文字清單

在開頭與結尾處都加上一個空白,像下面的例子⋯

<[ list of words ]>

而不是⋯

<[list of words]>

空白

在逗號後頭加上一個空白,前面不要。如下面的例子⋯

[x, y, z]

This

使用 @ 來代表 this,除非它是在單獨,後面沒接其它東西的狀態。

Prototype

使用 :: 取代 .prototype.

Array::slice

運算子

空白

除非在存取陣列的時候,使用單一空白來分隔運算子與運算元。

x = 1 + 2
list[i-1]

別名

文字 vs 運算符號

使用 and, or, 之類的文字取代 &&, || ,除非你需要 &&, || 來達到特殊的編譯結果(它們並不像 and, or 之類有隱含的函式呼叫)

x = false
y = true
(not) x or y #=> true  [(not)(x) || y]
(not) x || y #=> false [(not)(x || y)]

is not / isnt

使用 isnt 取代 is not

逗號

可以的話儘量少用逗號。這表示只要前面不是可呼叫的元素(例如函數)就可以省略逗號(也適用於呼叫引數)。然而,保持整體的一致性,要就全加逗號,不然就全部都不加。

[1 2 3]
add-numbers 5 x
[1, x, 3]

括號

省略所有能夠被省略的括號。

不要用在函數的呼叫:

Math.pow 2 3

如果你想傳入一個資料結構,可以搭配 do 語法取代括號的使用:

some-func do
  prop:  3
  other: 5

在鏈接技術 (chaning) 中,一樣可以省略括號的使用:

$ '#content .slider' .find 'a' .slide-up!
Tip
譯者註

編譯的結果是一個 jQuery 呼叫

$('#content .slider').find('a').slideUp();

在清單中也可以使用分號來分隔函數呼叫來避免使用括號,此處使用逗號會解析錯誤。

[add 2 3; times 2 3]
Tip
譯者註

上述的程式編譯後會是

[add(2, 3), times(2, 3)];

但如果是改用逗號取代分號:

[add 2 3, times 2 3]

則會被編譯成:

[add(2, 3, times(2, 3))];

函數呼叫

如同之前提到的,如果引數中的逗號是可以被省略的,那就省略它。

如果你呼叫的是一個不需要引數的函數,使用簡捷方式呼叫:

func!

除非你打算對回傳結果作 NOT 運算或強制轉型成布林值,這時候才使用 (),否則程式看起來會怪怪的。

!func()
!!func()
Tip
譯者註

原作者認為怪怪的程式應該是像下面的兩行:

!func!
!!func!

清單的存取

使用 list.0 取代 list[0] ,只有在索引需要進行數學運算,像是 list[i+0].8 的時候。

Switch

如之前所提到的,將 switch 的內容排整齊。

較短版本 vs 較長版本

如果你能夠讓每個 case 語句精簡在一行內,那就使用下面例子上半的較短版本及 otherwise 語法。否則還是使用使用下半部使用 default 的較長版本。

switch
| even x    => x
| even y    => y
| otherwise => x + y
switch
case f x
    blah
    ...
case g x
    asdf
    ...
default
    ...

Default

在較長版本的 switch 條件式中使用 default

使用 | otherwise ⇒ 的語法在較短的版本。除非條件式行數真的很少,才使用 | _ ⇒ 的語法。

switch x
| 2 => 7
| 3 => 8
| 4 => 9
| _ => 10

livescript-style-guide's People

Contributors

ddtet avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.