0
考慮下面的代碼:重構好還是不好?
status = row[COL_STATUS]
if status == "idle":
row[COL_EDITABLE] = True
row[COL_FONTSTYLE] = pango.STYLE_NORMAL
row[COL_WEIGHT] = pango.WEIGHT_NORMAL
elif status == "DCed":
row[COL_EDITABLE] = True
row[COL_FONTSTYLE] = pango.STYLE_ITALIC
row[COL_WEIGHT] = pango.WEIGHT_NORMAL
else:
row[COL_EDITABLE] = False
row[COL_FONTSTYLE] = pango.STYLE_NORMAL
row[COL_WEIGHT] = pango.WEIGHT_BOLD
請問以下是淨有益的重構,在您看來?
d = {"idle": (True, pango.STYLE_NORMAL, pango.WEIGHT_NORMAL),
"DCed": (True, pango.STYLE_ITALIC, pango.WEIGHT_NORMAL),
None: (False, pango.STYLE_NORMAL, pango.WEIGHT_BOLD)}
e,f,w = d.get(status, d[None])
row[COL_EDITABLE] = e
row[COL_FONTSTYLE] = f
row[COL_WEIGHT] = w
如果有更多的情況下,以上行組件編輯?
同意。第一個例子更容易閱讀。 – Geoff 2010-10-28 19:37:24
嗯我同意我認爲..會讓其他人發帖,但看着我的屏幕上的兩個並排我剛剛刪除了較短的版本 – Claudiu 2010-10-28 19:42:22