The program is written in JavaScript. To give you a rough idea what I am thinking of:
function State() {
return {
color: 'green',
size: 100,
// ... there are other properties here
}
}
function init() {
const state = State()
const otherObj = OtherObj()
// ...something like an event fires `foo`
foo(state, otherObj)
}
function foo(state, otherObj) {
state.size = calcNewSize(state) // state changes
state.color = pickNewColor(state, otherObj) // state changes
// ...
}
function pickNewColor(state, otherObj) {
const someInformation = getSomeInformation(state)
// ... do other stuff
return newColor
}
const a1 = init()
const a2 = init()
const a3 = init()
Right now, I have 3 objects that I should pass from function to function, but I am wondering, if this would scale well.
I am also wondering, if it is better to pass the entire object, instead of only the properties that are needed by these functions. Maybe it would be a good idea, to draw a line at 2-3 arguments, and if more is necessary, then pass the entire object.
Also, this could possibly be refactored into a single class
, with methods (like pickNewColor, getSomeInformation) having access to the properties (like size, color), in which case there wouldn't be any need to pass around objects. However, I was wondering, if the above approach of passing around objects could work well.
foo
inputting the state and mutating it in place, have it return a new state object without touching the original, making the function pure.