Healthy OOP in Javascript

Healthy OOP in Javascript

Bdcd782e200eaaa4d277af868cf3a3dd?s=128

Marco Troisi

August 31, 2016
Tweet

Transcript

  1. 7.

    @MarcoTroisi Classes should be immutable unless there's a very good

    reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible. (Joshua Bloch, “Effective Java”) HEALTHY OOP IN JAVASCRIPT
  2. 8.

    @MarcoTroisi ...all classes should be immutable in a perfect object-oriented

    world (Yegor Bugayenko, author of “Elegant Objects”) HEALTHY OOP IN JAVASCRIPT
  3. 10.

    @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT PREDICTABILITY (NO SIDE EFFECTS)

    var Phone = function(phoneMake, phoneModel) { this.make = function() { return phoneMake; } this.model = function() { return phoneModel; } this.equals = function(Phone) { return ( this.make() === Phone.make() && this.model() === Phone.model() ) } } var MotoG = new Phone("Motorola", "Moto G"); var IPhone6 = new Phone("Apple", "iPhone 6"); if (MotoG.equals(IPhone6)) { console.log("equals"); } else { console.log("NOT equals"); }
  4. 11.

    @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT PREDICTABILITY (NO SIDE EFFECTS)

    var Phone = function(phoneMake, phoneModel) { this.make = function() { return phoneMake; } this.model = function() { return phoneModel; } this.equals = function(Phone) { return ( this.make() === Phone.make() && this.model() === Phone.model() ) } } var MotoG = new Phone("Motorola", "Moto G"); var IPhone6 = new Phone("Apple", "iPhone 6"); if (MotoG.equals(IPhone6)) { console.log("equals"); } else { console.log("NOT equals"); } // WILL PRINT “NOT equals”
  5. 12.

    @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT PREDICTABILITY (NO SIDE EFFECTS)

    var Phone = function(phoneMake, phoneModel) { this.make = phoneMake; this.model = phoneModel; this.equals = function(Phone) { return (this.make === Phone.make && this.model === Phone.model) } } var MotoG = new Phone("Motorola", "Moto G"); var IPhone6 = new Phone("Apple", "iPhone 6"); // Uh-oh MotoG.make = "Apple"; MotoG.model = "iPhone 6"; if (MotoG.equals(IPhone6)) { console.log("equals"); } else { console.log("NOT equals"); }
  6. 13.

    @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT PREDICTABILITY (NO SIDE EFFECTS)

    var Phone = function(phoneMake, phoneModel) { this.make = phoneMake; this.model = phoneModel; this.equals = function(Phone) { return (this.make === Phone.make && this.model === Phone.model) } } var MotoG = new Phone("Motorola", "Moto G"); var IPhone6 = new Phone("Apple", "iPhone 6"); // Uh-oh MotoG.make = "Apple"; MotoG.model = "iPhone 6"; if (MotoG.equals(IPhone6)) { console.log("equals"); } else { console.log("NOT equals"); } // WILL PRINT “Equals”
  7. 23.

    @MarcoTroisi REAL WORLD ENTITIES HEALTHY OOP IN JAVASCRIPT /** *

    Interface for an Encrypted Password Object */ var EncryptedPasswordInterface = {} /** * Returns the encrypted value * @return string */ EncryptedPasswordInterface.encryptedValue = function() { return ""; }
  8. 24.

    @MarcoTroisi REAL WORLD ENTITIES HEALTHY OOP IN JAVASCRIPT var Md5EncryptedPassword

    = Object.create(EncryptedPasswordInterface); Md5EncryptedPassword = function(passwordToEncrypt) { this.encryptedValue = function() { // do encryption here… return encryptedPassword; } }
  9. 27.

    @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation - Easy to write tests
  10. 28.

    @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation - Easy to write tests - Force objects to adhere to a contract
  11. 29.

    @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation - Easy to write tests - Force objects to adhere to a contract - Easier to avoid violating the interface segregation principle
  12. 30.

    @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation - Easy to write tests - Force objects to adhere to a contract - Easier to avoid violating the interface segregation principle - Easy to swap an object with another
  13. 32.

    @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT <?php interface PhoneInterface {

    public function makeAndModel() : string; } <?php abstract class PhoneInterface { public function makeAndModel() : string { return ""; } }
  14. 33.

    @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT var PhoneInterface = {}

    PhoneInterface.makeAndModel = function() { return ""; }
  15. 35.