# Vertical Formatting ###### tags: `軟體設計` ## Formatting 1. 大多數人的觀念"getting it working" 2. bad formatting會導致code可讀性下降 ## Vertical Formatting ### How big java source file ![](https://i.imgur.com/7WOt5ym.png) 下圖可以看到各種java 測試框架的source code行數 1. 方塊中間為平均行數 2. fitnesse最大file約400行,平均約65行 3. 常用junit fitnesse time&money都沒有file超過500行 4. 意味者大概200~500行的單個source code file就能構成出色的系統,雖然不是一定的但比較少code的file比比較多code的file好理解 ### Source file name 比喻成報紙的新聞頭條,名字要取的簡單又可以概括想要講的內容,detail再交給source code來執行,報紙由很多小文章所組成,每個文章短小精煉,如果直接用一長串的故事事實來寫,不會有人去讀 ### Vertical Openness Between Concepts 幾乎所有source code由上往下,由左往右讀 在每個函數和每個package,import之間用space隔開可以影響code的視覺外觀,讓讀者知道每個空格之後都是新的一個獨立的concept ### Vertical Density 相關的代碼應該要互相靠近不藥用註解相隔太遠 ### Vertical Distance 有時候想要trace一個function,裡面的variable卻是從其他function定義或甚至是其他class繼承過來的,但是明明只有這個function才用的到而已 1. **Variables should be declared as close to their usage as possible** 2. **local variable should appear at the top of each function** 3. **Control variables for loops should usually be declared within the loop statement** #### [scissors Rule](https://blog.eq8.eu/article/scissors-rule-in-coding.html) 把source file code切成兩半,public/private() > Instance variable should be declared at the top of class The rule is not about placing public methods on the very top / bottom of a file Nope it’s just about not mixing public and private methods. #### where to place the instance variable >java---top c++ ---buttom 只要知道哪裡可以看到所有的instance variable就好不要零零散散
{"metaMigratedAt":"2023-06-15T15:42:09.007Z","metaMigratedFrom":"Content","title":"Vertical Formatting","breaks":true,"contributors":"[{\"id\":\"e830bac7-b567-4f41-a8d2-90b8de26c0ec\",\"add\":1534,\"del\":67}]"}
    321 views
   Owned this note