views:

78

answers:

2

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?

+3  A: 

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.

Bob Kaufman
agreed. The first example is simpler to read.
Geoff
hm i agree i think.. will let some others post, but looking at the two side-by-side on my screen i just deleted the shorter version
Claudiu
+3  A: 

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

Fred Hsu