Claudiu
Claudiu

Reputation: 229361

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
    else:
        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?

Upvotes: 0

Views: 187

Answers (2)

Fred Hsu
Fred Hsu

Reputation: 98

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

Upvotes: 4

Bob Kaufman
Bob Kaufman

Reputation: 12815

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.

Upvotes: 3

Related Questions