---
# System prepended metadata

title: Topic 19 - 21
tags: [The Pragmatic Programmer]

---

# Topic 19 - 21
###### tags: `The Pragmatic Programmer`
## Topic 19 Version Control
>version control system (VCS) is a giant undo key

a project-wide time machine that can return you to those halcyon days of last week, when the code actually compiled and ran.

other functions:
- collaboration
- deployment pipelines
- issue tracking
- general team interaction

> Always Use Version Control

- Make sure that everything is under version control.
- Even if we’re not working on a project, our day-to-day work is secured in a repository.
:::info
Use version control for nonproject things, too.
:::

> Shared Directories Are NOT Version Control
- 👎 the occasional team who share their project source files across a network (ex: cloud storage) 
- 👎👎 Some folks do use version control, and keep their main repository on a network or cloud drive. 
- The version control software uses a set of interacting files and directories. If two instances simultaneously make changes, the overall state can become corrupted, and there’s no telling how much damage will be done.

> Branches are one of their most powerful and useful features.

benefit of branches:
-  the isolation
-  branches are often at the heart of a team’s project workflow 

:computer: the power of Version Control's thought experimet :
Spill an entire cup of tea onto your laptop keyboard --> Buy a new computer -->
How long would it take to get that machine back to the same state it was in? -->
(with all the SSH keys, editor configuration, shell setup, installed applications, and so on)


 version control store: 
- All the user preferences and dotfiles 所有使用者設定和dotfile隱藏設定檔
- The editor configuration 編輯器設定
- The list of software installed using Homebrew 以Homebrew安裝的軟體列表: Mac OS平臺下的軟件包管理工具
- The Ansible script used to configure apps 用於設定應用程式的Ansible腳本: 自動化部署工具
- All current projects 目前在開發的所有專案


Q. Share gitflow workflow of your company?

## Topic 20 Debugging
history of the first computer bug: a moth caught in a relay in an early computer system 在電腦的繼電器中發現一隻蛾


> debugging is just problem solving

No one writes perfect software, so it’s a given that debugging will take up a major portion of your day. 

> Fix the Problem, Not the Blame

Before you start debugging, it’s important to adopt the right mindset

It doesn’t really matter whether the bug is your fault or someone else’s. It is still your problem.

 > the first rule of debugging: Don’t Panic

If your first reaction on witnessing a bug or seeing a bug report is “that’s impossible,” you are plainly wrong. 

#### WHERE TO START
- You may need to interview the user who reported the bug in order to gather more data than you were initially given.
-  Artificial tests isn't enough. You must brutally **test both boundary conditions** (邊界測試) and realistic **end-user usage patterns** (最終使用者模擬測試).
#### DEBUGGING STRATEGIES
> the most important rule of debugging: Failing Test Before Fixing Code
- The best way to start fixing a bug is to make it **reproducible** (可重現的).
- we want a bug that can be reproduced with a single command.
- We find it often helps to keep pen and paper nearby so we can jot down notes.


> Read the Damn Error Message

First, look at the problem. Is it a **crash**?
What if it’s not a crash? What if it’s just a **bad result**?

### Binary Chop/Binary search -->  Average	 Complexity: 	O(logn)
Choose a value in the middle of the array. If it’s the one you’re looking for, stop.  --> **Best Complexity : O(1)**
Otherwise you can chop the array in two. If the value you find is greater than the target then you know it must be in the first half of the array, otherwise it’s in the second half. Repeat the procedure in the appropriate subarray, and in no time you’ll have a result. --> **Worst Complexity: 	O(logn)**

    - Sensitivity to Input Values
    - Regressions Across Releases

### **Logging and/or Tracing**

[**console.trace(message[, ...args])**](https://www.geeksforgeeks.org/node-js-console-trace-method/)
- **Tracing statements** are those **little diagnostic messages you print to the screen or to a file** that say things such as “got here” and “value of x = 2.”
- Trace messages should be in a regular, consistent format as you
may want to parse them automatically.
- 通常會放些讓 Developer 在追蹤 bug 時，可以補足 ERROR 或 WARN 的 context 資訊。
-  console.log 只能得知執行當下程式碼的位置，console.trace 會印出 Call stack 並直接展開
![](https://i.imgur.com/8K6Rqz9.png)

```javascript=
function one(){
 two();
}
function two() {
 three();
}
function three() {
 console.trace("Call Stack");
}
one();
```

- 建議的使用時機: 如果出問題的部分和其他套件有關係，尤其是一個 Function 會在多處被使用的時候
```javascript=
video.addEventListener('pause', console.trace);
```

- console API補充:
**console.table()** 
```javascript=
const rows = [
  {
    "name": "Frozen yoghurt",
    "calories": 159,
    "fat": 6,
    "carbs": 24,
    "protein": 4
  },
  {
    "name": "Ice cream sandwich",
    "calories": 237,
    "fat": 9,
    "carbs": 37,
    "protein": 4.3
  },
  {
    "name": "Eclair",
    "calories": 262,
    "fat": 16,
    "carbs": 24,
    "protein": 6
  }
];
```
### Rubber Ducking 塑膠黃色小鴨
- A very simple but particularly useful technique for finding the cause of a problem is simply to **explain it to someone else.**
- The other person should look over your shoulder at the screen, and nod his or her head constantly (like a rubber duck bobbing up and down in a bathtub).
- And if you don’t have a person, a rubber duck, or teddy bear, or potted plant will do.

### Process of Elimination 排除法流程
- Remember, if you see hoof prints, think horses—not zebras.
- If you “changed only one thing’’ and the system stopped working, that one thing was likely to be responsible, directly orindirectly, no matter how farfetched it seems.
- new versions of the OS, compiler, database, or other third-party software can wreak havoc with previously correct code. New bugs might show up.
- keep a close eye on the schedule when considering an upgrade.
### Don’t Assume It—Prove It
- Don’t gloss over a routine or piece of code involved in the bug because you “know” it works. Prove it.
- When you come across a surprise bug, beyond merely fixing it, you need to determine why this failure wasn’t caught earlier.
- If it took a long time to fix this bug, ask yourself why.
- if the bug is the result of someone’s wrong assumption, discuss the problem with the whole team: if one person misunderstands, then it’s possible many people do.
### CHALLENGES QUESTION --- NO
`Debugging is challenge enough.`
## Topic 21 Text Manipulation
In previous sections we discussed some **specific tools** — shells, editors, debuggers.
we need to perform some transformation not readily handled by the basic tool set: a general-purpose **text manipulation tool** (一個通用的文字操作工具)

a number of great text manipulation languages:
-  command shells (augmented with tools such as awk and sed)
-   Python 
-   Ruby 
-   Perl (Perl is a family of script programming languages that is similar in syntax to the C language)

> Learn a Text Manipulation Language
- the Pragmatic Bookshelf is built around text manipulation (Ruby + Python). 
- if you follow our advice to keep things in plain text, then using these languages to manipulate that text will bring a whole host of benefits.