Forum Discussion

Vec84's avatar
Vec84
Contributor
7 years ago
Solved

Using Properties instead of Params

Our AUT currently runs through a flow of forms and i have a Unit for each form which i consider to be a class and each form has multiple outcomes and each form has multiple functions. I have added params for input on the functions and i use these functions on many other unit so if i was to change the params or remove,  it means updating them in all places the functions are called, which i have found to be bad design and not good for maintainability.

 

I have some experience with OOP as i done it in college however i would like to create properties for each of my forms(Classes) so that i can add and remove them in one place. I will also create a separate function for the properties so that it covers backwards compatibility if new development changes the input and i wont need to go over the calls to the method to update the properties.

 

I have read up on the ODT however this has now been removed from the product and i then read over the alternative however i found this difficult to understand and it also didn't work when i attempted it for my function.

 

Instead of the example below i would prefer to have the params as properties and that a new object couldnt be used without them being defined.

 

example of current code to be changed

 

function Main()
{
      inputData("1", "2" );
}


function inputData(testInput1, testInput2)
{
      Textbox1.SetText(testInput1);
      Textbox2.SetText(testInput2);
}
  • Yes.  Basically, whatever function you'd determine to be your constructor would assign all your properties.  It would look like:

     

    function constructorA() {
        var localObject = {prop1: '', prop2: 0} //Sets up the object with properties and default values
    
        localObject.Method1 = function () {
            Log.Message('Valjean 1: ' + localObject.prop1);
        }
        localObject.Method2 = function () {
            Log.Message('Marius: ' + localObject.prop2);
        }
        return localObject;
    }
    
    function Main() {
        var myObject = new constructorA();
        myObject.prop1 = 'Who am I?"
        myObject.prop2 = 24601;
        myObject.Method1();
        myObject.Method2();
        
    }

6 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    So, something like this?

     

    inputData then becomes the constructor for a class with the two properties.  I built it out that you assign the properties on construction but you could always keep the parameters out, set the properties to empty strings and then just set their values in code.

     

    function inputData (testInput1, testInput2) {
         this.testInput1 = testInput1;
         this.testInput2 = testInput2;     
         this.setText = function () {
            TextBox1.SetText(this.testInput1);
            TextBox2.SetText(this.testInput2);
         }
    }
    
    function Main() {
        var myObject = new inputData('1', '2');
        myObject.setText();
        myObject.testInput1 = '3';
        myObject.testInput2 = '4';
        myObject.setText();
    }

     

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      If your class is in a different code unit than where you're going to be instantiating it, you'll need to do something SLIGHTLY different in your constructor...

       

      function inputData (testInput1, testInput2) {
      var localObject = {} localObject.testInput1 = testInput1; localObject.testInput2 = testInput2; localObject.setText = function () { TextBox1.SetText(localObject.testInput1); TextBox2.SetText(localObject.testInput2); }
      return localObject; }

      Other than that, the rest is the same.

  • Vec84's avatar
    Vec84
    Contributor
    Hi tristaanogre, thanks for your reply.
    I wanted to do away with the params for maintainability. If I was to remove the params, would an object have to define properties when calling the function.

    PS - Really enjoyed the recent 301 training, Very useful and something we are going to implement with our projects. I have also downloaded your previous 301 on frameworks.
    Thanks Again for your help
    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Yes.  Basically, whatever function you'd determine to be your constructor would assign all your properties.  It would look like:

       

      function constructorA() {
          var localObject = {prop1: '', prop2: 0} //Sets up the object with properties and default values
      
          localObject.Method1 = function () {
              Log.Message('Valjean 1: ' + localObject.prop1);
          }
          localObject.Method2 = function () {
              Log.Message('Marius: ' + localObject.prop2);
          }
          return localObject;
      }
      
      function Main() {
          var myObject = new constructorA();
          myObject.prop1 = 'Who am I?"
          myObject.prop2 = 24601;
          myObject.Method1();
          myObject.Method2();
          
      }
      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        BTW, if you like the idea of Script Extensions and like the framework, you COULD combine the two... build out a whole bunch of runtime objects that would be your different objects that you could then just assign values to properties and call methods without having to instantiate them in code.  It could work but it would take some back end elbow grease to get it up and running.  Theoretically feasible.

  • Vec84's avatar
    Vec84
    Contributor
    Brilliant, thanks very much for your help Rob.
    I'm just part of the way through the framework video now.