$30 off During Our Annual Pro Sale. View Details »

Healthy OOP in Javascript

Healthy OOP in Javascript

Marco Troisi

August 31, 2016
Tweet

More Decks by Marco Troisi

Other Decks in Programming

Transcript

  1. HEALTHY OBJECT ORIENTED PROGRAMMING IN JAVASCRIPT @MarcoTroisi

  2. @MarcoTroisi WHY OOP HEALTHY OOP IN JAVASCRIPT

  3. @MarcoTroisi OBJECTS AS A WAY TO UNDERSTAND THE WORLD HEALTHY

    OOP IN JAVASCRIPT David West quote
  4. @MarcoTroisi WHERE DOES THE CRITICISM COME FROM HEALTHY OOP IN

    JAVASCRIPT
  5. @MarcoTroisi HEALTHY OOP IN JAVASCRIPT HEALTHY OOP IN JAVASCRIPT

  6. @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT

  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
  8. @MarcoTroisi ...all classes should be immutable in a perfect object-oriented

    world (Yegor Bugayenko, author of “Elegant Objects”) HEALTHY OOP IN JAVASCRIPT
  9. @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT PREDICTABILITY (NO SIDE EFFECTS)

  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"); }
  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”
  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"); }
  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”
  14. @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT PERFORMANCE

  15. @MarcoTroisi IMMUTABILITY HEALTHY OOP IN JAVASCRIPT THREAD SAFETY

  16. @MarcoTroisi HEALTHY OOP IN JAVASCRIPT IMMUTABILITY IN JAVASCRIPT

  17. @MarcoTroisi HEALTHY OOP IN JAVASCRIPT ACCESSORS AND MUTATORS vs USEFUL

    METHODS
  18. @MarcoTroisi USEFUL METHODS HEALTHY OOP IN JAVASCRIPT OBJECT CLOSER TO

    REAL WORLD ENTITY
  19. @MarcoTroisi USEFUL METHODS HEALTHY OOP IN JAVASCRIPT NO EXPOSITION OF

    INTERNAL VARIABLES
  20. @MarcoTroisi USEFUL METHODS HEALTHY OOP IN JAVASCRIPT EXPOSE A SMALL

    NUMBER OF METHODS
  21. @MarcoTroisi HEALTHY OOP IN JAVASCRIPT REAL WORLD ENTITIES ARE BETTER

    THAN “SERVICES”
  22. @MarcoTroisi REAL WORLD ENTITIES HEALTHY OOP IN JAVASCRIPT PasswordEncrypter() PasswordEncryptionService()

    ?
  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 ""; }
  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; } }
  25. @MarcoTroisi HEALTHY OOP IN JAVASCRIPT USE INTERFACES

  26. @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation
  27. @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation - Easy to write tests
  28. @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT - Object is defined

    before implementation - Easy to write tests - Force objects to adhere to a contract
  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
  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
  31. @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT INTERFACE = ABSTRACT CLASS

    with empty methods
  32. @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT <?php interface PhoneInterface {

    public function makeAndModel() : string; } <?php abstract class PhoneInterface { public function makeAndModel() : string { return ""; } }
  33. @MarcoTroisi INTERFACES HEALTHY OOP IN JAVASCRIPT var PhoneInterface = {}

    PhoneInterface.makeAndModel = function() { return ""; }
  34. @MarcoTroisi USE INTERFACES HEALTHY OOP IN JAVASCRIPT GIVE AN ABSTRACT

    SIGNATURE TO YOUR OBJECTS
  35. None
  36. @MarcoTroisi THANK YOU VERY MUCH! marcotroisi.com @MarcoTroisi github.com/marcotroisi bluefin.com HEALTHY

    OOP IN JAVASCRIPT