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 :
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:
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.