Legacy code: changing software you don’t understand

const x = readInput();
if (isMultipleOfThree(x)) {
return true;
} else {
return false;
}
const x = readInput();
if (isMultipleOfThree(x) && endsWithDigitFive(x)) {
return true;
} else {
return false;
}
const x = sync({ waitFor: 'input' })
if (isMultipleOfThree(x)) {
sync({ request: 'good', waitFor: 'bad' })
} else {
sync({ request: 'bad', block: 'good' })
}
input(6) // input event
good
input(7)
bad
const x = sync({ waitFor: 'input' })
if (endsWithDigitFive(x)) {
sync({ request: 'good', waitFor: 'bad' })
} else {
sync({ request: 'bad', block: 'good' })
}
if (isMultipleOfThree(x)) {
sync({ request: 'good', waitFor: 'bad' })
...
if (endsWithDigitFive(x)) {
sync({ request: 'good', waitFor: 'bad' })
if (isMultipleOfThree(x)) {
sync({ request: 'good', waitFor: 'bad' })
} else {
sync({ request: 'bad', block: 'good' })
}
...
if (endsWithDigitFive(x)) {
sync({ request: 'good', waitFor: 'bad' })
} else {
sync({ request: 'bad', block: 'good' })
}

Integrating changes is where complexity lies

You might be asking: what’s the point of programming this way using these sync calls, and these request/waitFor/block events?

But my pure functions are also oblivious of each-other

Pure functions are just input→output and in this context they are also written in a way that they are unaware of each-other.

pipe(
getName,
uppercase,
get6Characters,
reverse
)({ name: 'Buckethead' })
// 'TEKCUB'

Changing software you don’t understand

But how does all this help with the infamous question of changing legacy code?

Given the user doesn't have a promo code
When the user adds an item to the shopping cart
And is the first Monday of the month
Then they should not be able to add more than 3 items to the cart
promoCode = sync({ waitFor: 'promoCode' })
if (promoCode) return;
sync({ waitFor: 'itemAddedToCart' })
if (isFirstMondayOfMonth()) {
sync({ waitFor: 'itemAddedToCart' })
sync({ waitFor: 'itemAddedToCart' })
// only 3 items max!
sync({ block: 'itemsAddedToCart' })
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store