I use JavaScript with an automated Testing IDE. I have a function/method in a JavaScript class that populates a menu to create "a thing" in the software. Currently, I have the method take in an object parameter which will contain all the field values in the menu.
So for example, I might create a object that looks like this:
let object1 =
{
field1 = "1000",
field2 = "20",
field3 = "7",
field4 = "38",
field5 = "-69",
field6 = "1212"
}
The function looks like this:
this.initiateObject = function(object)
{
let field1 = [], field2 = [], field3 = [],
field4 = [], field5 = [], field6 = [];
if (object.field1)
{
field1 = [enums.RunFunctions.INPUTFUNCTION, object.field1,
this.WindowObjects.FIELD1_TEXTBOX];
}
if (object.field1)
{
field2 = [enums.RunFunctions.INPUTFUNCTION, object.field2,
this.WindowObjects.FIELD2_TEXTBOX];
}
if (object.field1)
{
field3 = [enums.RunFunctions.INPUTFUNCTION, object.field3,
this.WindowObjects.FIELD3_TEXTBOX];
}
if (object.field1)
{
field4 = [enums.RunFunctions.INPUTFUNCTION, object.field4,
this.WindowObjects.FIELD4_TEXTBOX];
}
if (object.field1)
{
field5 = [enums.RunFunctions.INPUTFUNCTION, object.field5,
this.WindowObjects.FIELD5_TEXTBOX];
}
if (object.field1)
{
field6 = [enums.RunFunctions.INPUTFUNCTION, object.field6,
this.WindowObjects.FIELD6_TEXTBOX];
}
input = [field1, field2, field3, field4, field5, field6];
this.runFunctions(input);
this.clickDone();
}
The whole function is basically constructing an array of arrays to put into the runFunctions which will execute a series of functions. The first element of each array is calling a value from an enum we use to determine the type of function that will be called. These all use the inputFunction which types into a field but we also have selectFunction to pull from a drop down or verifyTextBox to verify the text in the target textbox. The second element is the input value and the third element tells the function which field (using a defined object) to enter the value into.
All this to say/ask, if there's a way to simplify/optimize this sort of code. I feel like if there was a way to dynamically refer to the object properties then I could throw this into a loop of some kind. There is a clear pattern to the way the code is structured and in reality, we have more like 50 fields that we can fill and multiple windows this is used in which makes upkeep extremely tedious.