1. Evaluate forms frequently
每次如果修改了某個 forms ,尤其是它是 def 的 form ,就可以考慮把它重新求值一下。 求值的作用可以想象成是一種存檔。
2. Start stateful things in REPL before you do anythings else
使用 mount 或是 component
3. Use comment forms to document your experiments
在 REPL 要做的求值實驗,可以考慮用 (comment ... ) 記錄在程式裡,方便日後理解。
4. Use def for debugging
REPL 可以對定義好的函數輕易地求值。然而,函數內部的 form 要如何 debug 呢? 函數內部的 form 往往會依賴於函數的 arguments 。很簡單,就是用 def 把函數的 arguments 指定一個固定值,這樣子就可以 debug 了。記得 git commit 之前要把這種 debugging 的 def 移除。範例
5. Unmap namespace during experimentation
ns-unalias 與 ns-unmap 可以視為 def 和 require 的反運算。
6. Re-open libraries for exploration
在 require 某個 library 之後,使用 in-ns 可以進入該 library 的 namespace 裡,並且在這邊重新定義變數,以達成除錯或是取得執行資訊的目的。(monkey-patch)
7. Pretty Print
使用 clojure.pprint 這個函式庫來做輸出
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.pprint :as pp] | |
'[clojure.main :as main]) | |
(clojure.main/repl :print pp/pprint) |