The Strategy of One
Tags: complexity, antipatterns, programming, song
Published: 2022-06-01 16:05:32

Accidental complexity is quite an oddity
It's making things hard
Just for fun, without a plan
To make me feel smart

## The Strategy of One

The Strategy of One always gets me bummed
Why even bother?
Just call the thing, do that and win
Don't feed that monster

```ts
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 🧟');
  }
};
```

## The Sequence Implied

The Sequence Implied should be simplified
Call `a` then call `b`
The order preserved but hardly observed
Much less guaranteed

```ts
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?
```

## The Eager Effect

The Eager Effect is a source of unrest
Why not just describe?
Let others worry, take all the glory
By having _them_ apply

```ts
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).
  //  ___/      /__\ \ \
  // (___.'\_______)\_|_|
}
```

## The Triangle of Gloom

The Triangle of Gloom means impending doom
Maybe not right now
And not for you, you are testing with two
But in prod it will blow!

```ts
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).
  },
  [],
);
```
## The Not-a-Duck

The Not-a-Duck lives in the muck
It wants to be clever
Yet it walks like a duck, and it talks like a duck
So we name it whatever

```ts
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?
```