Случайная сложность — довольно странное явление
Это усложнение
Просто так, без плана.
Чтобы я чувствовал себя умным
Стратегия одного
Стратегия одного всегда выбивает меня из колеи.
Зачем вообще напрягаться?
Просто назови вещь, сделай это и выиграй
Не кормите этого монстра
const monsterFactory = (type: MonsterType): Monster => {
switch (type) {
case 'gizmo':
return gremlin;
default:
// |---/| I bet `type MonsterType = 'gizmo'`, what
// | o_o | sort of crash-loop inducing, exploding
// _^_/ kittens game are we playing here?
throw new Error('Invalid monster, sorry 🧟');
}
};
Подразумеваемая последовательность
Подразумеваемая последовательность должна быть упрощена
Вызвать a
затем вызвать b
Порядок сохраняется, но почти не соблюдается
Гораздо менее гарантирован
export interface TableBuilder {
buildTable: (materials: WoodAndNails) => Table;
getMaterials: (style: Style) => WoodAndNails;
getStyle: (mood: Mood) => Style;
}
// There's probably just one valid way of calling these 👆.
// ________
// / /| Do we really need that inversion of control?
// /_______/ |
// |_______|/| What about `buildTable: Mood => Table`?
// || || ||||
// || || Can't we compose functions instead of defining
// single-use methods?
Эффект нетерпения
Эффект нетерпения — источник волнений
Почему бы просто не описать?
Пусть другие беспокоятся, берут всю славу на себя
Заставив их применить
const nightFalls = async (subject: Werewolf, moon: Moon) => {
const weCouldReturnThis = moon === 'full'
? transform(subject)
: humanize(subject);
// ...but instead we apply save to it...
await save(weCouldReturnThis);
// n,
// _/ | _
// /' `'/ ...and now testing this function
// <~ .' is much harder.
// .' |
// _/ | (because we need to intercept
// _/ `.`. that `save` call and steal its
// / ' __ | | first argument).
// ___/ /__
// (___.'_______)_|_|
}
Треугольник мрака
Треугольник мрака означает надвигающуюся гибель.
Возможно, не прямо сейчас.
И не для вас, вы тестируете с двумя.
Но в prod это будет взрыв!
const result = lots.reduce(
(acc, v) => {
const next = ...
return [...acc, next];
// How many times are we iterating `acc`?
//
// / This is a triangular number
// / (e.g. 9 + 8 + ... + 1) and
// /____ it has quadratic complexity: O(n^2).
//
// Just use `acc.push`, much to your functional soul's
// discomfort, to bring this back to linear, O(n).
},
[],
);
Не утка
Утка живет в грязи.
Он хочет быть умным.
Но ходит, как утка, и говорит, как утка.
Поэтому мы называем его как угодно.
type Duck = (say: Word) => Cuack;
const duck: Duck = () => 'cuack!';
const politeDuck: Duck = say => `respectfully, ${say}`;
const duckBroker = (say: Word): Cuack =>
say === 'hi'
? duck(say)
: politeDuck(say);
// Why is this "duck broker" not a duck?
//
// <(.)__ It certainly looks, talks and walks like
// (___/ a duck.
//
// Why are we introducing new names for plain
// composition? Isn't this just more of the same?