Static helper function to set a property on another object A situation has come up in which we have a block of code that will be repeated in multiple files, and this block of code has to:


Create some object based on the values of other controls/properties.
Set a property on another object to be the object from #1.


Here is an example of what this looks like:



Repeated code before refactoring (TypeScript)

public class PageViewModel {
  private _name: TextBox;
  private _type: TextBox;
  private _timestampFormat: RadioButton;

  ..

  // This should generate a data model out of the current state of its controls.
  public generateDataModel(): IDataModel {
    // Here will be some logic to generate a property value based on
    // the radio button value and some other conditions.
    let timestampProperty;
    if (this._radioControl.value() === 1) {
      timestampProperty = { format: "foo" };
    } else if(this._radioControl.value() === 2) {
      timestampProperty = { format: "bar" };
    }

    var dataModelObject = &lt;IDataModel&gt;{
      name: this._name.value(),
      type: this._type.value(),
      timestamp: timestampProperty
    };
    return dataModelObject;
  }

}




Problem

The generateDataModel function will be present on more than one view model, all of which will generate the timestamp field from the radio control with that logic, so code will be repeated.



How it was solved

A helper method was extracted to do both #1 and #2 from above. This method is being called with references to both the control and the IDataModel object being created.

Helpers.ts:

public static setTimestampRadioButton(radioControl: RadioButton, dataModelObject: IDataModel): void {
    let timestampProperty;
    if (radioControl.value() === 1) {
      timestampProperty = { format: "foo" };
    } else if(radioControl.value() === 2) {
      timestampProperty = { format: "bar" };
    }

    dataModelObject.timestamp = timestampProperty;
}


PageViewModel.ts:

public generateDataModel(): IDataModel {
  var dataModelObject = &lt;IDataModel&gt;{
    name: this._name.value(),
    type: this._type.value()
  };

  Helpers.setTimestampRadioButton(this._radioControl, dataModelObject);

  return dataModelObject;
}




I have some concerns with this approach from the point of view of readability, and also because I think Helpers.ts knows too much about IDataModel and how it is used.

My questions:


Is this a good way to solve the problem of repetition here? Why / why not?


If not, is there a better way you'd suggest?

What do you think of the pattern static setSomethingOnSomethingElse(thing1, thing2)?
I would argue that setTimestampRadioButton can be improved in three ways.


Side effects. By mutating the passed dataModelObject, the function immediately makes any function that uses setTimestampRadioButton harder to understand as it magically sets a property name that the calling function did not specify. 
Typing. Mutating the argument the function forces you to create a Partial&lt;IDataModel&gt; (or cast an object which is not an IDataModel to IDataModel). This is not ideal. 
Alternative property names. Currently only IDataModel uses the format from a RadioButton, what happens when ITimeModel needs the timestamp format stored in a format property? We are now back to the original problem.


I would instead recommend creating a method to get the expected format. 

public static getTimestampFormat(radioControl: RadioButton): IFormat {
    if (radioControl.value() === 1) {
      return { format: "foo" };
    } else if(radioControl.value() === 2) {
      return { format: "bar" };
    }
    return { format: 
}


This simplifies your generateDataModel method even more. 

public generateDataModel(): IDataModel {
  return {
    name: this._name.value(),
    type: this._type.value(),
    timestamp: Helpers.getTimestampFormat(this._radioControl)
  };
}


Instead of a static helper method, you might also consider adding the method as an instance method of the RadioButton class, or creating a child of the RadioButton class with a TimestampRadioButton class. 

A couple other general things: 


Avoid casting like the plague. It is almost as bad as using the any type as it hides potential problems in a very similar way. 
I can't think of any instance in which adding Object or data to a variable name makes it more descriptive. dataModelObject doesn't really tell you any more than model.