Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Just JavaScript] 01. Mental Models #1

Open
allenGKC opened this issue Jul 3, 2020 · 0 comments
Open

[Just JavaScript] 01. Mental Models #1

allenGKC opened this issue Jul 3, 2020 · 0 comments

Comments

@allenGKC
Copy link
Owner

allenGKC commented Jul 3, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant