Resolving Duplication In Object Maps

Last edited on

Script-based object maps allow resolving duplication between object names by defining functions which act as parameterized object names.

A Cause For Object Name Duplication

To improve usability of graphical user interfaces, most applications typically make use of a standard set of controls such as buttons, input fields and check boxes. The controls are then distinguished by a caption which indicates the functionality exposed by that control. This similarity between user controls is reflected by a certain degree of duplication among object names stored in a script object map. For instance, here's a typical snippet taken from the object map used for testing the 'addressbook' Qt application shipped with Squish for Qt :

address_Book_MainWindow = {"type": "MainWindow", "unnamed": 1, "visible": 1, "windowTitle": "Address Book"}
address_Book_New_QToolButton = {"text": "New", "type": "QToolButton", "unnamed": 1, "visible": 1, "window": address_Book_MainWindow}
address_Book_Add_QToolButton = {"text": "Add", "type": "QToolButton", "unnamed": 1, "visible": 1, "window": address_Book_MainWindow}
address_Book_Remove_QToolButton = {"text": "Remove", "type": "QToolButton", "unnamed": 1, "visible": 1, "window": address_Book_MainWindow}
address_Book_Save_QToolButton = {"text": "Save", "type": "QToolButton", "unnamed": 1, "visible": 1, "window": address_Book_MainWindow}

Here, a single name address_Book_MainWindow is used for the main window, and four more names are then used for individual tool buttons.

The duplication between the object names for the QToolButton controls is easily visible. The code is very noisy, and hard to read.

Don't Repeat Yourself

In the spirit of the DRY Principle , we can resolve this repetition by defining a function which provides object names given some caption:

def toolButton(text):
    return {"text": text, "type": "QToolButton", "unnamed": 1, "visible": 1, "window": address_Book_MainWindow}

address_Book_MainWindow = {"type": "MainWindow", "unnamed": 1, "visible": 1, "windowTitle": "Address Book"}
address_Book_New_QToolButton = toolButton("New")
address_Book_Add_QToolButton = toolButton("Add")
address_Book_Remove_QToolButton = toolButton("Remove")
address_Book_Save_QToolButton = toolButton("Save")

This introduces a toolButton function which is effectively a parameterized symbolic name, expecting the text of a tool button and producing an object name matching that tool button. Reducing the code duplication using this function not only greatly reduced noise (and thus improved readability) of the code, it also hardens the object map against changes in the future: any changes to how tool buttons are identified (except for the text of the tool button, of course) now only cause a change in a single place.