# CH9 Organizing Data p239-247 ## Split variables 拆分變數 #### Example 1 ```javascript let temp = 2 * (height + width); console.log(temp); temp = height * width; console.log(temp); ``` refactor兩個地方 1. temp 名稱 2. 避免 temp 名稱重複賦值 ```javascript const perimeter = 2 * (height + width); console.log(perimeter); const area = height * width; console.log(area); ``` ### 重構要點 1. 一個變數只能有一個職責。(例外1) 2. 如果可以的話,宣告一個immutable的變數 3. 更改其他有用到相同變數的地方 4. 測試 5. 往下重構下一個變數 ```javascript= //例外1 // collecting variable 可以不必須做多餘的命名 function add(num1, num2) { let result result += num1 result += num2 return result } ``` #### Example 1 ```javascript // Before重構 function discount (inputValue, quantity) { if (inputValue > 50) inputValue = inputValue - 2; if (quantity > 100) inputValue = inputValue - 1; return inputValue; } ``` ```javascript! // 重構 Step1: input immutable處理 function discount (originalInputValue, quantity) { let inputValue = originalInputValue; if (inputValue > 50) inputValue = inputValue - 2; if (quantity > 100) inputValue = inputValue - 1; return inputValue; } ``` ```javascript // 重構 Step2: 在變數賦質一次之後,命名新的變數。一個變數只能有一個職責 function discount (inputValue, quantity) { let result = inputValue; if (inputValue > 50) result = result - 2; if (quantity > 100) result = result - 1; return result; } ``` #### 討論 1. 大家處理immutable input(by value, by reference)的方式? 2. 大家過往的經驗中,變數也是維持單一職責嗎,或者是會重複使用來避免產生過多的變數名稱? ## Rename field 字段改名 ### 重構方法 1. 如果record只有使用在小範圍的話,不需要做以下的動作,可以直接rename該變數並且測試。 2. 封裝該record(Encapsulate Record, ch7) 3. 為private field重新命名, 4. 測試 ```javascript= // 把name key改成title const organization = {name: "Acme Gooseberries", country: "GB"}; // const organization = {title: "Acme Gooseberries", country: "GB"}; ```     ```javascript= // step2 封裝該record(Encapsulate Record) class Organization { constructor(data) { this._name = data.name; this._country = data.country; } get name() {return this._name;} set name(aString) {this._name = aString;} get country() {return this._country;} set country(aCountryCode) {this._country = aCountryCode;} } const organization = new Organization({name: "Acme Gooseberries", country: "GB"}); ```     ```javascript= // step3 為private field重新命名,更改成想要重構的名稱 class Organization { constructor(data) { this._title = data.name; //為private field重新命名 this._country = data.country; } get name() {return this._title;} set name(aString) {this._title = aString;} //為private field重新命名 get country() {return this._country;} set country(aCountryCode) {this._country = aCountryCode;} } ```     ```javascript! // 在title的部分,做判斷來避免報錯 class Organization { constructor(data) { this._title = (data.title !== undefined) ? data.title : data.name; this._country = data.country; } get name() {return this._title;} set name(aString) {this._title = aString;} get country() {return this._country;} set country(aCountryCode) {this._country = aCountryCode;} } ```     ```javascript! // step4 重構field 做 test const organization = new Organization({title: "Acme Gooseberries", country: "GB"}); ```     ```javascript! // 確定沒有報錯之後,就可以把判斷的地方拿掉 class Organization { constructor(data) { this._title = data.title; //這裡把上一個部分做的判斷給拿掉 this._country = data.country; } get name() {return this._title;} set name(aString) {this._title = aString;} get country() {return this._country;} set country(aCountryCode) {this._country = aCountryCode;} } ```     ```javascript class Organization { constructor(data) { this._title = data.title; this._country = data.country; } get title() {return this._title;} //把整個封裝的record都更改重構後的變數名稱 set title(aString) {this._title = aString;} //把整個封裝的record都更改重構後的變數名稱 get country() {return this._country;} set country(aCountryCode) {this._country = aCountryCode;} } ```     ```javascript! const organization = {title: "Acme Gooseberries", country: "GB"}; ``` #### 討論 1. 什麼樣得情境下會需要使用到完整得rename field方法,什麼時機只需要直接去更改名稱? 2. rename field 的方法有什麼優缺點嗎?或是哪裡可以做改善? 3. chatGpt Naming 工具 1. https://www.thesaurus.com/browse/book 查詢同義詞或起反義詞 2. vs code extensions => code spell checker