Memorizing Code Patterns, PART II – A Glossary of Code Personified

In the previous blog, I mostly address “the why” of attempting to memorize code. In this blog, I follow up on the concept of a “memory palace”… an approach to organize the stuff you memorize. Rote memorization can work for a little while, but there’s no way you can do it for a deck of cards. In part because of the similarity between one thing you memorize and the next, but also because the mind simply can’t hold long form random (or seemingly random) information in tact. What you need to do is CHUNK it. Break the stuff you want to memorize down into component parts.

When it comes to memorizing a deck of cards, or any other type of “memory-palace-creation-activity”, there are a few things that help make things memorable: people, places, and absurdity.

To have a better understanding on “What” makes something memorable, you may want to consider purchasing the book: Impossible to Ignore by Carmen Simon. For a better understanding on how Memory Palaces and Memory Athletes compete: check out Joshua Foer’s Moonwalking with Einstein, or anything by Nelson Dellis.

The remainder of this blog is not focused on the actual memory palace (that will likely be in Part III), but personifications of “code concepts” that should make it easier to memorize.

If you’re a skeptic, ask yourself if you think the following would be difficult or easy to commit to memory?

function target_sum_exists(ts, array) {
  const yep = "yep", nope = "nope";
  let leftIdx = 0, rightIdx = array.length - 1;

  array.sort((a, b) => (a - b))
  while (leftIdx < rightIdx) {
   let currentSum = [array[leftIdx], array[rightIdx]].reduce((accumulateHere, currentVal) => accumulateHere + currentVal)
   if (currentSum === ts) {
     return yep
   } else if (currentSum < ts) {
     leftIdx++;
   } else if (currentSum > ts) {
     rightIdx--;
   }
  }
  return nope
}

console.log(target_sum_exists(5, [11,2,7,3,4]))

Note: the use of reduce in the above code is TOTAL overkill, but it’s there to prove a point.

Now, ask yourself if the following might be easier to memorize:

Lewis and Clark talking about some target practice while a Rabbi holding a cigar cutter chats with an accountant as Wile E. Coyote chases Karl Marx with a three pronged pitchfork.

While it may take a little time coming up with the absurd situation above, having “stock characters” for “stock code” should make it easier to encode in your mind and then recall. While you will not have the solution fully memorized, if you’re a semi-competent (or even less than semi-competent) coder, you should have enough information/enough clues to reconstruct a solution.

The above analogy translates into the code as follows:

Lewis and Clark talking about some target practice… for “some target” practice, think SUM TARGET, or target sum. As for Lewis and Clark, they become leftIdx and rightIdx. Quick aside: Lewis and Clark were American pioneers that helped chart the Western portion of the United States. I use them as substitutes for left and right indexes because left and right indexes are typically used for pointers and pointers make me think of charting maps. If you decide to implement this strategy yourself, you may end up using different personas to make memorization more intuitive. Another quick aside: all my examples will be included after the breakdown of this example.

Rabbi holding a cigar cutter makes me think of a briss. Aside, I know cigar cutters are not used for circumcision, but it definitely makes things a little bit more memorable. Rabbi holding a cigar cutter is to remind me of array.length - 1.

The accountant is to remind me that I need to sort. If the sorting used MUST take an argument as in someArray.sort((a, b) => a - b), use expensive accountant to denote a higher level of detail.

Wile E. Coyote is to remind me that there is a while loop as opposed to a for loop. Wile E. Coyote because it sorta sounds like “while”.

The presence of Karl Marx is a clue that the reduce function is used. Why Marx? Because he wanted to “reduce” capitalism to ashes. Sure, it’s a bit shocking… but I’ve found that myself, among others, often find the reduce function to be somewhat non-intuitive and difficult to know when to conjure it up. Associating it with a flamboyant figure from history ensures I’ll never forget where or when it ought to be used.

As for the three pronged pitchfork, it’s a clue that the control statement requires three conditionals.

To circle back and reflect for a moment: the vignette of “Lewis and Clark talking about some target practice while a Rabbi holding a cigar cutter chats with an accountant as Wile E. Coyote chases Karl Marx with a three pronged pitchfork” is not THEE SOLUTION, but it contains enough clues that I can reassemble the solution.

As a side note, the above code snippet I had originally memorized in rote fashion. In the long term, I trust that the absurd analogy above will be stickier for longer.

CAST OF CHARACTERS FOR MEMORIZATION

NOTE: the following “cast of characters” has been repurposed from commonly used code that I talked about in this blog here. The first few examples are more in depth to give you an idea of how to create these personifications on your own. The further I go down the list, the more brief the examples are.

ANOTHER NOTE: I will revisit this blog post as I add new characters and revise ones that are here. The reasons for the characters are sometimes more obvious than others.

Conan The Barbarian with a Ring of Keys (the for in loop). Barbarian technically means “foreigner” pronounced “for in”. Conan was also a bit of a “treasure hunter” so it’s not implausible for him to hold a Ring of Keys. The Ring of Keys is particularly relevant, because a For In loop is used to iterate through the keys of an object (or hash). If you’re iterating through an array instead of an object “in” gives you the “location” of the key. However, Conan The Barbarian and his Ring of Keys is a shorthand for remembering that I’ll be iterating through object keys.

Don Pietro Baking Pizzas in Ovens (the for of loop). “Don Pietro” technically “Angelo Pietro” is mascot for a chain of Italian restaurants in Japan. It’s also one of my favorite salad dressing; it’s like Italian dressing with a Japanese flair. ANYWAYS, if you google the mascot you’ll see the singing chef and it’s easy to imagine him in a kitchen watching over pizzas baking in ovens. Why “ovens” because of the implicit “for x of” … also, this loop returns values, hence “ov” in stead of “of”. It’s easy to imagine Don Pietro checking all the pizzas he’s cooking in an array of ovens.

Gordon Gekko as a Forex Trader (the extended for loop; for (i = 0; i < array.length; i++))… Forex as in “for extended”. If you haven’t seen the movie Wall Street with Michael Douglas and Charlie Sheen from 1987, it’s worth a watch; also a Forex trader is someone that trades Foreign Exchanges. Not really relevant, but that’s where the word Forex comes from.

Wile E. Coyote (the while loop) explained in the above example.

Arthur, Ariadne, Cobb, Mal, Eames, Fischer, Saito as the recursive “if else” loop. If you aren’t familiar with those character names, you could theoretically just say “THE MOVIE INCEPTION“, or think of any random character from it. The recursive “if else” loop is code you call on itself that will continue until a final break statement (usually checked at the top of the loop) is met. While it’s probably easier to choose your favorite character, recursive if else statements often seem to have lots of conditional logic. Remembering the seven characters listed above can help you quickly identify how many conditionals you might need. If you memorize an algorithm with Saito, it might help you remember that you need 7 things to check; likewise if Cobb, perhaps only 3. You could also memorize accessories or props for each character if you needed more clues.

Lewis and Clark, for left and right pointers (or indexes). Lewis is “Left”, ClaRk is “Right”. Further reasoning for them from the vignette above.

Window Washer or Window Washing for windowStart and windowEnd, featured in the sliding window paradigm, though can be used for any windowStart or windowEnd (as with arrays in general).

Two Face” from Batman as a reminder that you will be working with “head” and “tail” nodes as opposed to “Left” and “Right”. In exploring algorithms, the difference seems somewhat semantic, but I suspect the differentiation is something to embrace if you memorize lots of them.

“Classroom” for a class.

Wart Nose” to represent a “node”. As in: the polite way of saying “There’s a giant wart on that nose!!!” might be “Thee nose has a node.”

“Construction Worker” to represent the constructor method on a class. Depending on what the construction worker is doing, you might more easily conjure up ideas of what arguments must be passed.

Rabbi with a Cigar Cutter as a reminder that you are deducting 1 from the length of something. You could alternately remember the Rabbi with Super Glue if you’re adding +1. And if the length of the deduction or addition is something other than 1, you might imagine the Rabbi with a box of “Special K” cereal. “K” being symbolic of some indeterminate value. If the box is face up perhaps that’s addition, if face down, subtraction. Aside: when dealing with values other than 1, you may likely be dealing with summing three values.

A Ninja Doing Magic Tricks for the slice method, because slice will leave your original array intact. It’s more for selecting some portion of the array.

Doctor Moreau from the story: The Island of Doctor Moreau, for splice An evil scientist that would splice DNA to make hybrid monsters. It goes without saying that altering DNA is undoable.

A Gymnast for .split.

Conjoined Twins from the Circus for .join

A circus strongman smushing stuff together for concat.

The presence of “popcorn” for the .pop method.

A Pushy Car Salesman to remind you of push.

Christopher Columbus for map.

Mr. Coffee for filter.

Karl Marx for reduce.

An Accountant or perhaps Ben Affleck as THE ACCOUNTANT for sort.

A shifty eyed shoplifter boldly snatching something from the front of the checkout line for shift.

A “SHIfT head cutting you on the grocery line” for unshift.

A gas station attendant filling up the tank for fill.

Jason from Friday the 13th for JSON.parse.

Jason from Jason and The Argonauts fighting Medusa with her nasty stringy snake hair for JSON.stringify.

A pawn broker with a keyring for Object.keys, with a ledger for Object.entries (keys and values), with a vault for Object.values. Pawn broker because they’re associated with lots and lots of objects. This analogy may become easily extendable with additional props.

A geeky math teacher in a tweed jacket for anything related to the Math.someFunction. A fat math teacher for Math.max, a skinny one for Math.min, a math teacher on the floor for Math.floor, a body building math teacher doing situps for Math.abs

Sherlock Holmes for find.

A “Librarian” for findIndex.

A Stock Room Manager at a shoe store for indexOf or lastIndexOf. Could be a clothing store, I just “once upon a time” worked in a shoe store so it felt more memorable for me.

An awesome hippie for includes. As in includes everybody.

A Deli if you’re using the delete option. Most likely deleting a key from a hash.

Street Magician for some kind of swap or “switcheroo”.

“Fat Bastard” from Austin Powers Counting His Hash Browns at McDonalds: frequency counter using a hash table. For example:

const hashbrowns = ["burnt hashbrown", "burnt hashbrown", "soggy hashbrown", "normal hashbrown", "soggy hashbrown", "bacon"]

hbFrequency = {};

  for (let hb of hashbrowns) {
    if (!(hb in hbFrequency)) {
      hbFrequency[hb] = 0;
    }
    hbFrequency[hb] += 1;
  };

console.log(hbFrequency)

Turtle Bouncer: ternary expression, if a bouncer, then used for control flow to exit a loop. If a security guard, then a ternary expression somewhere/anywhere inside of the code. Example from above rewritten as a ternary expression… Below would be a Turtle Security Guard. IF a retired turtle (of any type) then assume a single line IF statement with NO else.

const hashbrowns = ["burnt hashbrown", "burnt hashbrown", "soggy hashbrown", "normal hashbrown", "soggy hashbrown", "bacon"]

hbFrequency = {};

  for (let hb of hashbrowns) {
    (!(hb in hbFrequency)) ? hbFrequency[hb] = 1 : hbFrequency[hb] += 1;
  };


console.log(hbFrequency)

Supporting Cast & Crew: the number of supporting cast and crew members is related to how many functions might be needed to complete some pattern. If supporting cast and crew just two functions… if supporting cast, crew, and extras then three functions. If supporting cast, crew, extras, and catering then four etc…

Gutenberg: the dude that invented the printing press. He represents use of some custom print method as an alternative to console.log. Where console.log sorta just prints everything out… creating a custom print method on an object takes the format below:

customPrintMethod() {
  process.stdout.write(`[${this.someValueOnAClass}]`);
}

class Thing {
  constructor(someAttribute) {
    this.thingAttribute = someAttribute
  }

  printMe() {
    process.stdout.write(`${this.thingAttribute}`)
    console.log()
  }
}

// see it in action below

const aBunchOfThings = [new Thing("huh"), new Thing("what"), new Thing("okay")]

for (x in aBunchOfThings) {
  aBunchOfThings[x].printMe()
}


Comments

7 responses to “Memorizing Code Patterns, PART II – A Glossary of Code Personified”

  1. […] Glossary of Code Personified […]

  2. […] Glossary of Code Personified […]

  3. […] Glossary of Code Personified […]

  4. […] Glossary of Code Personified […]

  5. […] Glossary of Code Personified […]

  6. […] Glossary of Code Personified […]

  7. […] Glossary of Code Personified […]

Discover more from Comedy Tragedy Epic

Subscribe now to keep reading and get access to the full archive.

Continue reading