Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Hidden Power of Humble Interfaces (GopherCon Singapore 2017)

The Hidden Power of Humble Interfaces (GopherCon Singapore 2017)

Learn how to design your own interfaces in Go that are simple yet powerful – just like io.Reader and error. Discover the techniques for correctly identifying the places where interfaces will improve our code, and the specific steps to ensuring that our interfaces are as powerful as they can be.

Aditya Mukerjee

May 26, 2017
Tweet

More Decks by Aditya Mukerjee

Other Decks in Technology

Transcript

  1. Why use interfaces? • Write less code • Write robust

    code • Write flexible code @chimeracoder
  2. What makes the io interfaces powerful? • Abstracting a lot

    of common functionality • Lots of granularity • Plethora of helper functions @chimeracoder
  3. What makes error powerful? • Abstracts no functionality • Provides

    no granularity • Provides (almost) no helper functions @chimeracoder
  4. Humility and Discipline • Keep interfaces humble • Writing interfaces

    forces you to define the minimum required contract for using your types • Keep interfaces disciplined • Writing interfaces allows the compiler to enforce this contract @chimeracoder
  5. Why Gophers avoid interfaces • Afraid of writing an interface

    that is too complicated • Afraid of specifying the wrong methods @chimeracoder
  6. How I Learned To Stop Worrying and Love the interface

    • Interfaces reveal the assumptions you’re already making • “If it’s there, you will use it” @chimeracoder
  7. Writing interfaces like io.* • Don’t write helper methods until

    you actually find you need them • The contracts provided by each should be minimal • Create composite interface types @chimeracoder