lundi 25 février 2019

Functional Programming: How to handle complex data without bloated functions?

Lets say in your program you have defined a complex car object. That object holds a very long list of predefined key value pairs (wheels,engine,color, lights, amountDoors etc.), each being either a part number or a list of part number, or a specific value.

//** PSEUDO CODE:
var inputCar = { 
  "engine": "engine-123", 
  "lights": ["light-type-a", "light-type-b"], 
  "amountDoors": 6,
  etc ... lets assume a lot more properties
}

Lets also assume, this object is already as simple as possible and can not be further reduced.

Additionally we have a list of settings, that tells us more information about the part numbers and is different for each kind of part. For the engine it could look like this:

var settingsEngine = [
  { "id": "engine-123", weight: 400, price: 11000, numberScrews: 120, etc ... },
  { "id": "engine-124" etc ... }
]

With all the settings being bundled in a main settings object

settings = { settingsEngine, settingsWheel, settingsLight ... }

Now we have different functions that are supposed to take a Car and return certain values about it, like weight, price or number of screws.

To calculate those values its necessary to match the IDs from the input car, with those from the settings, and also apply some logic to get the exact data of complex parts (to figure out what the autobody looks like, we need to see how many doors there are, how big the wheels are etc.).

Getting the price would also be different and arbitrarily complex for each part of the car. Each part of the pricing could need to access different parts and information about the car, so just mapping over a parts list wouldn't suffice. (For the price of the paint job we would need the total surface area of all parts with the same color etc.)

One idea would be to create an inbetween object, that has resolved all the details about the car that are shared between the price and weight calculations and can then be used to calculate the weight, price etc.

One implementation could look like that:

var detailedCar = getDetailedCar(inputCar, settings);

var priceCar = getPriceCar(detailedCar);
var weightCar = getWeightCar(detailedCar);

This way part of the work has only to be done once. But in this example detailedCar would be an even more complex object than the initial input object, and therefor so would be the parameter of getPriceCar - making it also really hard to test, because we would always need a full car object for each test case. So I am not sure if that is a good approach.

Question

What is a good design pattern for a program that handles complex input data that can't be further simplified in a functional programming style/with pure functions/composition?

How can the the result be easily unit-testable given a complex, interdependent input?

Aucun commentaire:

Enregistrer un commentaire