Skip to content

[Just JavaScript] 01. Mental Models #1

Open
@allenGKC

Description

@allenGKC

01. Mental Models

What’s a Mental Model?

let a = 10;
let b = a;
a = 0;

The answer is a being 0 and b being 10.

Dan shows these codes and wishes everyone to read them in his own way.

Experienced programmers may have their own monologue, and no matter what it is, Dan wishes those who read this article will establish the correct mental model which will help them understand codes better and well.

Coding, Fast and Slow

We all have two systems —— fast and slow.

This “fast” system is good at pattern matching (necessary for survival!) and “gut reactions”. But it’s not good at planning.

This “slow” system is responsible for complex step-by-step reasoning. It lets us plan future events, engage in arguments, or follow mathematical proofs.

function duplicateSpreadsheet(original) {
  if (original.hasPendingChanges) {
    throw new Error('You need to save the file before you can duplicate it.');
  }
  let copy = {
    created: Date.now(),
    author: original.author,
    cells: original.cells,
    metadata: original.metadata,
  };
  copy.metadata.title = 'Copy of ' + original.metadata.title;
  return copy;
}

You’ve probably noticed that:

  • This function duplicates a spreadsheet.
  • It throws an error if the original spreadsheet isn’t saved.
  • It prepends “Copy of” to the new spreadsheet’s title.

What you might not have noticed (great job if you did though!) is that this function also accidentally changes the title of the original spreadsheet.

Missing the bug is not a big deal but rebuild the correct mental model is important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions