开发者

good refactoring or bad?

Given the following code:

    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
    el开发者_开发百科se:
        row[COL_EDITABLE] = False
        row[COL_FONTSTYLE] = pango.STYLE_NORMAL
        row[COL_WEIGHT] = pango.WEIGHT_BOLD

Would the following be a net beneficial refactoring, in your opinion?

    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

What if there were more cases or more row components to edit?


What about using objects and doing something akin to "Replace Type Code With Subclasses"? http://www.refactoring.com/catalog/replaceTypeCodeWithSubclasses.html


What you gain in conciseness you lose in readability. In the current example, I can easily tell what goes where. In the new code, I have to think a little harder.

Multiply that by the next thousand edits and you're going to have some serious maintainability problems on your hands.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜