Tag coding

程設風格 | 早期終止執行

剛開始寫程式的時候,以為所謂邏輯判斷就是很多個若 A 則 B 包起來的複雜地圖。這樣的程式碼,很容易因為邏輯判斷太過複雜,很難一眼就看到到底目前程式會跑到哪一個 if 分支裡面執行。 taget = Dog.new if target.is_a?(Animal) if target.has_four_legs? if target.is_a?(Dog) puts "wolf!" else puts "four leg animal can say yeah" end else puts "don't know what that is" end else puts "don't know what that is" end => "wolf!" 如果透過層層過濾只是要剔除一些情況讓程式不處理,例如有很多個分支其實是「不處理」或是「同樣的簡單處理」,例如上面的 puts "don't know what that is" ,可以改用「早期終止」的模式來改寫。 def what_it_says(target) # 如果不符合條件的參數就會在此提早回傳值 return "don't know what that is" unless target.is_a?(Animal) && target.has_four_legs? # 符合資格的參數才會進入真正的必要判斷 if target.

終端機工具

轉型後端工程師的路上,看了很多教學,裝了很多套件,用過很多軟體。目前用得還算不手殘的工具只有終端機(terminal)跟文本編輯器(text editor)。 因為要經常性切換不同的 Git Branch,MacOX 本來附掛的 terminal 顯得有點陽春,需要極大注意力才能清楚知道自己究竟目前身在何處又在哪支 branch 裡。 先烈做了很多很棒的工具改善工作體驗與視覺效果,iTerm 2 很是不錯,搭配上一些快捷熱鍵可以有效增加效率。 Shortcut of iTerms2 windows and tabs command + T to open new tab shift + command + [ / ] to switch between tabs command + W to close the tab command + D to have vertical divided windows command + [ / ] to switch between windows cursor ctrl + a to go to the beginning of line