Re: [討論] Python 3.10將加入Switch-Case語句

看板Soft_Job作者 (cocorosie)時間3年前 (2021/03/27 22:20), 編輯推噓1(106)
留言7則, 3人參與, 3年前最新討論串3/6 (看更多)
我個人是很討厭很多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

03/27 22:22, 3年前 , 1F
想著根本還沒發生的"幾百個if",然後過度優化成這樣
03/27 22:22, 1F

03/27 22:22, 3年前 , 2F
可讀性直接差了一個等級
03/27 22:22, 2F

03/27 23:07, 3年前 , 3F
如果插了一個區間是10-1000 做A, 1000-100m 做B 你
03/27 23:07, 3F

03/27 23:08, 3年前 , 4F
這段 code 要改多少地方? 再看看下面那段 code 可讀
03/27 23:08, 4F

03/27 23:08, 3年前 , 5F
性 這樣真的有比較好嗎
03/27 23:08, 5F

03/27 23:24, 3年前 , 6F
無視我第一句推文好了 跟 if-else 改的量差不多
03/27 23:24, 6F

03/27 23:46, 3年前 , 7F
毫無緣由的refactor十之八九是無意義的
03/27 23:46, 7F
文章代碼(AID): #1WNpyNBp (Soft_Job)
討論串 (同標題文章)
文章代碼(AID): #1WNpyNBp (Soft_Job)