看板 Soft_Job 關於我們 聯絡資訊
我個人是很討厭很多if-else, 或是switch case. 並不是說不好, 只是很容易出現有些section是code, 有些是function. 案子急一點, 重覆的code就會很多. 幾百個if-else/switch-case就有機會變成上萬行的code. 這個就很阿雜了. 就之前數字區間的code, 我是會往這個方向走 import operator def over_100m(s): print('over 100m') def ten_to_100m(s): print('10 - 100m') def zero_to_ten(s): print('0 - 10') def is_zero(s): print('0') def is_negative(s): print('< 0') map = {(operator.ge, 100000000): over_100m, (operator.ge, 10): ten_to_100m, (operator.gt, 0): zero_to_ten, (operator.eq, 0): is_zero, (operator.lt, 0): is_negative} eigen_spectrum = [120000000, 1000, 10, 8, 0, -10] for s in eigen_spectrum: for c in map: if c[0](s, c[1]): map[c](s) break 這樣至少可以強迫分工的時候, 有一個列表可以作維護. 不過coding style這個真的見人見智, 我是不覺得有絕對好壞之分. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.160.144.241 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1616854807.A.2F3.html
alihue: 想著根本還沒發生的"幾百個if",然後過度優化成這樣 03/27 22:22
alihue: 可讀性直接差了一個等級 03/27 22:22
IcecreamHsu: 如果插了一個區間是10-1000 做A, 1000-100m 做B 你 03/27 23:07
IcecreamHsu: 這段 code 要改多少地方? 再看看下面那段 code 可讀 03/27 23:08
IcecreamHsu: 性 這樣真的有比較好嗎 03/27 23:08
IcecreamHsu: 無視我第一句推文好了 跟 if-else 改的量差不多 03/27 23:24
aalexx: 毫無緣由的refactor十之八九是無意義的 03/27 23:46
agogoman: 原來第一時間想到的算是最佳(笑), 還refactoring... 04/04 06:02