Tom Clark
Jul 27, 2024

--

Great article which I’ll send to my team. The one area where I differ though is the named outputs, at least for this particular example.

If your function there were named ‘calculateShapeAttributes’ then sure, but a function called ‘calculateArea’ absolutely should return a single value, an area. By allowing people to vary outputs of functions that are quite atomic, you invite evolution toward very weirdly named functions.

In your example, if someone needed the perimeter they’d end up doing

const {perimeter} = calculateArea(…)

A good solution here is to rely on Typescript to have a unified input type, then a set of functions to operate on it

const area = calculateArea(Shape: myshape)
const perimeter = calculatePerimeter(Shape: myshape)

(Forgive slightly incorrect syntax, I’m on my phone, but you get the idea!)

--

--

Tom Clark
Tom Clark

Written by Tom Clark

Fluid Dynamicist at the core, lover of chaos theory. Experienced scientist, developer and team lead working in wind energy — from startups to heavy industry.

No responses yet