Artwork

Το περιεχόμενο παρέχεται από το iteration podcast, John Jacob, and JP Sio - Web Developers. Όλο το περιεχόμενο podcast, συμπεριλαμβανομένων των επεισοδίων, των γραφικών και των περιγραφών podcast, μεταφορτώνεται και παρέχεται απευθείας από τον iteration podcast, John Jacob, and JP Sio - Web Developers ή τον συνεργάτη της πλατφόρμας podcast. Εάν πιστεύετε ότι κάποιος χρησιμοποιεί το έργο σας που προστατεύεται από πνευματικά δικαιώματα χωρίς την άδειά σας, μπορείτε να ακολουθήσετε τη διαδικασία που περιγράφεται εδώ https://el.player.fm/legal.
Player FM - Εφαρμογή podcast
Πηγαίνετε εκτός σύνδεσης με την εφαρμογή Player FM !

Encapsulation

39:54
 
Μοίρασέ το
 

Manage episode 236757437 series 1900125
Το περιεχόμενο παρέχεται από το iteration podcast, John Jacob, and JP Sio - Web Developers. Όλο το περιεχόμενο podcast, συμπεριλαμβανομένων των επεισοδίων, των γραφικών και των περιγραφών podcast, μεταφορτώνεται και παρέχεται απευθείας από τον iteration podcast, John Jacob, and JP Sio - Web Developers ή τον συνεργάτη της πλατφόρμας podcast. Εάν πιστεύετε ότι κάποιος χρησιμοποιεί το έργο σας που προστατεύεται από πνευματικά δικαιώματα χωρίς την άδειά σας, μπορείτε να ακολουθήσετε τη διαδικασία που περιγράφεται εδώ https://el.player.fm/legal.

Episode 6 - More Code Examples

  • Drawing from Chapter 7 - Encapsulation

A weekly podcast about programming, development, and design through the lens of amazing books, chapter-by-chapter.

Encapsulate Record (162)

var organization = { name: "JP Sio", country: "USA" }; 

becomes ⬇️

class Organization { constructor(data) { this._name = data.name; this._country = data.country; } get name() { return this._name; } set name(arg) { this._name = arg; } get country() { return this._country; } set country(arg) { this._country = arg; } } 
  • you can hide what is stored and provide methods
  • consumer of class Organization doesn't need to know / care which is stored and which is calculated
  • nice getter and setter methods
  • makes it easier to refactor -> can hide implementation of internals and update the internals while keeping the same external interface

Encapsulate Collection (170)

class Person { get courses() { return this._courses; } set courses(aList) { this._courses = aList; } } 

becomes ⬇️

class Person { get courses() { return this._courses.slice(); } addCourse(aCourse) { /*...*/ } } 

Replace Primative with Object (174)

orders.filter(0 => "high" === o.priority || "rush" === o.priority) 

becomes ⬇️

orders.filter(o => o.priority.higherThan(new Priority("normal"))); 
  • this goes back to "Primitive Obsession"
  • programmers are often hesitant to create their own types and rely only on primitives. i.e. representing a phone number as a string instead of as it's own type

A telephone number may be represented as a string for a while, but later it will need special behavior for formatting, extracting the area code, and the like

  • create a new class for that bit of data
  • at first, the class does very little. in fact it probably only wraps a primitive
  • but now you have a place to put behavior specific to its needs

Inline Function (115) 🥴

Sometimes it's better to not try to split things apart, sometimes it just complicates things.

// before refactor: function getItemPrice(item) { if (itemOnSale(item) == true) { return item.price - 5 } else { return item.price } }; function itemOnSale(product) { if (product.onSale == true) { return true; } else { return false; } }; let original = getItemPrice(sweatshirt); // after refactor: function newGetItemPrice(item) { if (item.onSale == true) { return item.price - 5 } else { return item.price } }; 

Extract Class (182) 🥴

  • Talk through HUGE applicant model (in Ruby)
  • Broke this into child objects
    • Applicant Health History
    • Applicant Habits
    • Applicant Lifestyle
    • Applicant Method
    • Applicant Legal Release

Picks

  • JP: None :(
  • John: Quad Lock phone mount - bikes
  continue reading

78 επεισόδια

Artwork

Encapsulation

iteration

113 subscribers

published

iconΜοίρασέ το
 
Manage episode 236757437 series 1900125
Το περιεχόμενο παρέχεται από το iteration podcast, John Jacob, and JP Sio - Web Developers. Όλο το περιεχόμενο podcast, συμπεριλαμβανομένων των επεισοδίων, των γραφικών και των περιγραφών podcast, μεταφορτώνεται και παρέχεται απευθείας από τον iteration podcast, John Jacob, and JP Sio - Web Developers ή τον συνεργάτη της πλατφόρμας podcast. Εάν πιστεύετε ότι κάποιος χρησιμοποιεί το έργο σας που προστατεύεται από πνευματικά δικαιώματα χωρίς την άδειά σας, μπορείτε να ακολουθήσετε τη διαδικασία που περιγράφεται εδώ https://el.player.fm/legal.

Episode 6 - More Code Examples

  • Drawing from Chapter 7 - Encapsulation

A weekly podcast about programming, development, and design through the lens of amazing books, chapter-by-chapter.

Encapsulate Record (162)

var organization = { name: "JP Sio", country: "USA" }; 

becomes ⬇️

class Organization { constructor(data) { this._name = data.name; this._country = data.country; } get name() { return this._name; } set name(arg) { this._name = arg; } get country() { return this._country; } set country(arg) { this._country = arg; } } 
  • you can hide what is stored and provide methods
  • consumer of class Organization doesn't need to know / care which is stored and which is calculated
  • nice getter and setter methods
  • makes it easier to refactor -> can hide implementation of internals and update the internals while keeping the same external interface

Encapsulate Collection (170)

class Person { get courses() { return this._courses; } set courses(aList) { this._courses = aList; } } 

becomes ⬇️

class Person { get courses() { return this._courses.slice(); } addCourse(aCourse) { /*...*/ } } 

Replace Primative with Object (174)

orders.filter(0 => "high" === o.priority || "rush" === o.priority) 

becomes ⬇️

orders.filter(o => o.priority.higherThan(new Priority("normal"))); 
  • this goes back to "Primitive Obsession"
  • programmers are often hesitant to create their own types and rely only on primitives. i.e. representing a phone number as a string instead of as it's own type

A telephone number may be represented as a string for a while, but later it will need special behavior for formatting, extracting the area code, and the like

  • create a new class for that bit of data
  • at first, the class does very little. in fact it probably only wraps a primitive
  • but now you have a place to put behavior specific to its needs

Inline Function (115) 🥴

Sometimes it's better to not try to split things apart, sometimes it just complicates things.

// before refactor: function getItemPrice(item) { if (itemOnSale(item) == true) { return item.price - 5 } else { return item.price } }; function itemOnSale(product) { if (product.onSale == true) { return true; } else { return false; } }; let original = getItemPrice(sweatshirt); // after refactor: function newGetItemPrice(item) { if (item.onSale == true) { return item.price - 5 } else { return item.price } }; 

Extract Class (182) 🥴

  • Talk through HUGE applicant model (in Ruby)
  • Broke this into child objects
    • Applicant Health History
    • Applicant Habits
    • Applicant Lifestyle
    • Applicant Method
    • Applicant Legal Release

Picks

  • JP: None :(
  • John: Quad Lock phone mount - bikes
  continue reading

78 επεισόδια

Wszystkie odcinki

×
 
Loading …

Καλώς ήλθατε στο Player FM!

Το FM Player σαρώνει τον ιστό για podcasts υψηλής ποιότητας για να απολαύσετε αυτή τη στιγμή. Είναι η καλύτερη εφαρμογή podcast και λειτουργεί σε Android, iPhone και στον ιστό. Εγγραφή για συγχρονισμό συνδρομών σε όλες τις συσκευές.

 

Οδηγός γρήγορης αναφοράς