DEV Community 👩‍💻👨‍💻

Cover image for The Strategy of One
Pato Z
Pato Z

Posted on

The Strategy of One

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

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 🧟');
  }
};
Enter fullscreen mode Exit fullscreen mode

The Sequence Implied

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

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?
Enter fullscreen mode Exit fullscreen mode

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

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).
  //  ___/      /__\ \ \
  // (___.'\_______)\_|_|
}
Enter fullscreen mode Exit fullscreen mode

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!

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).
  },
  [],
);
Enter fullscreen mode Exit fullscreen mode

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

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?
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Forem

🏗 We built a 100% open source community software called Forem.

 
You can contribute to the codebase or host your own.