Inspired by biology background I combine some ideas from functional and OO programming into a talk about software design.
Compose softwarelike nature would@this_ahmed
View Slide
Feature Request
Big Ball of Mud
software
change
software as a living organism
Earth: 4.5 billion YA
Monads: 3.5 billion YA
stromatolites
The Machinery of LifeDavid S. Goodsell
small1 μm
cell membraneboundaries
easily replaced
messages = colony
Bacterial colony
–Alan Kay“I thought of objects being like biological cells and/or individual computers on a network, only able tocommunicate with messages…”
Components: modules,objects, functions, etc.Messages: functions,properties, events, etc.Components with boundariesCompose with messages
• easier to reason about (fits in your head)• easier to change (can be re-written)• easier to test (confidence when changing)
import myModule from ‘file.js';// file.jsexport default function() {return something;}Boundaries
DataFetchDataFilterLineGraphdata-fetch.jsdata-filter.jsline-graph.js
DataFetchDataFilterBarGraphchangedata-fetch.jsdata-filter.jsbar-graph.js
DataFetchDataFilterLineGraph/project|-- data-fetch.js|-- data-filter.js|-- line-graph.js… 1000 other filesWhat does system do?
Multicellular: 1.5 billion YA
stove-pipe sponge
MeshDataFetchDataFilterLineGraph
project└── dashboard└── sales-widget├── data-fetch.js├── data-filter.js├── index.js└── line-graph.js
sales-widget/index.js// import or inject components// compose our componentsconst sales = dataFetch(“/sales");const planSales = dataFilter(sales, plan);lineGraph(planSales);
side-effects
Nervous System: ~600million YA
Nerve Net
push side-effects to the edges of ourprogram
Data(e.g. fetch api)“Nerve Net”draw(e.g. chart.js)
Functional Core
pure functionsconst increment = (n) => { n + 1; };
mutable dataconst array = [3,1,2];array[0] // 3array.sort(); // somewhere elsearray[0] // 1
immutable dataconst array = Object.freeze([3,1,2]);array[0] // 3array.sort(); // TypeError: Cannot assign toread only property '1' ...array[0] // 3
let newArr = [ ...prevArr ];let newObj = { ...prevObj };copy data
DataFilterDataShaperLineGraphOptionsData(e.g. fetch api)FunctionalCore“Nerve Net”draw(e.g. chart.js)
DataFilterDataShaperLineGraphOptionsData(e.g. fetch api)FunctionalCore“Nerve Net”draw(e.g. chart.js)DateFilterTrendLineGary Bernhardt, Boundaries
Cambrian Explosion: ~541million YA
Run and Tumblemit.edu
Run and Tumble to Better Designsmallcomponentscompose withmessagespush side-effectsto boundariesfunctional core name things
Resources• Gary Bernhardt - Boundaries: https://www.destroyallsoftware.com/talks/boundaries• Dan North - Software that Fits in Your Head: https://www.youtube.com/watch?v=4Y0tOi7QWqM• Rich Hickey - Simple Made Easy: https://www.infoq.com/presentations/Simple-Made-Easy• Jessica Kerr - Functional Principles for OO Development:https://www.youtube.com/watch?v=tq5SQ4W3gRI