4
\$\begingroup\$

I have a function that needs to evaluate some parameters to determine if they're valid or not. They all need to be evaluated individually, so I have some code like below.

#Configure widgets held in a layout
def layoutConfigurer(name, widgetType, minW, minH, maxH, maxW, flat, focus, image, position=False):
 widgetType.setObjectName(_fromUtf8(name))
 if minW or minH is not None:
 widgetType.setMinimumSize(QtCore.QSize(minW, minH))
 if maxH or maxW is not None:
 widgetType.setMaximumSize(QtCore.QSize(maxH, maxW))
 if flat:
 widgetType.setFlat(True)
 if focus:
 widgetType.setFocusPolicy(QtCore.Qt.NoFocus)
 if image:
 widgetType.setPixmap(QtGui.QPixmap(_fromUtf8(image)))
 if position:
 widgetType.setAlignment(QtCore.Qt.AlignCenter)

This code works, but is kind of ugly. I'm using the multiple if statements, because I need them all to be evaluated and not just the first true statements.

I'm not sure if there is a better way of evaluating command line arguments or not, but I would like to make the above code seem prettier. At the moment, it seems a little inefficient

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 4, 2014 at 23:39
\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

I don't see any simple way to generalize your if statements, since they result in very different actions. You might be able to push that complexity into the widgetType instead, if that code is under your control, but it won't reduce the total amount of code.

What concerns me, though, is the length of the parameter list: 10 parameters, of which 9 are mandatory. The caller is certainly going to look ugly. A quick improvement might be to accept keyword arguments. A deeper analysis might reveal that those parameters should belong in an object instead.

answered Feb 5, 2014 at 0:21
\$\endgroup\$
2
  • \$\begingroup\$ I have no control over the widgetType code since it gets set to a object from the QT librariers. I will admit that it's a little odd about the ammount of mandatory arguments, and I'll look into fixing that. The function itself takes parameters that are configurations of QT widgets, and then configures it, reducing what would be 6-7 lines of code to just one. \$\endgroup\$ Commented Feb 5, 2014 at 0:31
  • 1
    \$\begingroup\$ I'd definitely switch to kwargs and maybe provide defaults instead of requiring params - that way you can reduce conditionality and cut down on if tests \$\endgroup\$ Commented Feb 5, 2014 at 0:56
1
\$\begingroup\$

Fundamentally I agree that there is little to be done here; all you can really do is shove the complexity somewhere else. However there are some small things you can do try to reduce the branching that a reader of your code has to think through. I may not have translated these all correctly, but here's a couple ways to minimize the conditional code in the outer function:

# If there's a reasonable default (perhaps 0,0), a helper can return it
widgetType.setMinimumSize(calcMinimumSize(minW, minH))
# If there's no reasonable default, a helper can hide the conditional
# (You should probably use only one of these first two approaches at a time.)
applyMaximumSize(widgetType, maxH, maxW)
# bool will constrain `flat` to True or False
widgetType.setFlat(bool(flat))
# (x if C else y) lets you specify the default (I'm guessing `TabFocus`)
widgetType.setFocusPolicy(QtCore.Qt.NoFocus if focus else QtCore.Qt.TabFocus)
answered Feb 5, 2014 at 14:10
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.