Slide 1

Slide 1 text

Data Abstraction in Go Michael DiBernardo @mdibernardo 19-Sep-2013 @ GolangTO

Slide 2

Slide 2 text

How did we get here?

Slide 3

Slide 3 text

Programming “in” “Programmers who program ‘in’ a language limit their thoughts to constructs that the language directly supports. If the language tools are primitive, the programmer's thoughts will also be primitive.”

Slide 4

Slide 4 text

Programming “into” “Programmers who program ‘into’ a language first decide what thoughts they want to express, and then they determine how to express those thoughts using the tools provided by their specific language.” - Code Complete 2nd ed (Steve McConnell)

Slide 5

Slide 5 text

This works until...

Slide 6

Slide 6 text

signature TREE= sig datatype 'a tree = Lf | Node of 'a * 'a tree * 'a tree val size: 'a tree -> int end structure T:> TREE = struct datatype 'a tree = Lf | Node of 'a * 'a tree * 'a tree fun size Lf = 0 | size(Node(_,t1, t2)) = 1 + size t1 + size t2 end

Slide 7

Slide 7 text

Programming models • Procedural • Functional • Object-oriented • Stack-based • Stream-oriented • ...

Slide 8

Slide 8 text

Programming models • Easier to models that are idiomatic to the language • Many concepts are independent of model • But what about implementation of these concepts?

Slide 9

Slide 9 text

Data Abstraction

Slide 10

Slide 10 text

ADTs • Who wants to define ADT?

Slide 11

Slide 11 text

ADTs "The general technique of isolating the parts of a program that deal with how data objects are represented from the parts of a program that deal with how data objects are used is a powerful design methodology called data abstraction." - Structure and Interpretation of Computer Programs pp.80

Slide 12

Slide 12 text

ADTs • Abstract type (name) • Operations on that type • Preconditions • Postconditions • Invariants

Slide 13

Slide 13 text

ADTs - Why? • Often quoted are: • Ease of use • Ease of change

Slide 14

Slide 14 text

ADTs - Why? • Note that reuse was not a strong motivator for these, historically • Re-use was a later movement (esp. OO) • The focus of this talk is not on reuse!

Slide 15

Slide 15 text

Example: Rational Numbers

Slide 16

Slide 16 text

Rational ADT • Create • Convert to human-readable repr • Add • Invariant: Should always be in lowest terms

Slide 17

Slide 17 text

“Implicit” Interfaces "Every type has an interface, which is defined to be the set of methods which operate on that type." - Ian Lance Taylor in “Learning Go”

Slide 18

Slide 18 text

Where do implicit interfaces fail?

Slide 19

Slide 19 text

Summary • Using type + methods = implicit interface • Hiding struct members can add robustness, but doesn’t work for non-structs

Slide 20

Slide 20 text

Summary • Interfaces can abstract different implementations of common behaviour • Can also be used shield against direct access to non-struct types (arrays, slices, etc)

Slide 21

Slide 21 text

Summary • Some mechanisms exist for re-using ADT method implementation (e.g. anonymous fields) • But for now, good old "procedural abstraction" is not a bad idea.

Slide 22

Slide 22 text

// Header file "api.h" struct Entity; extern struct Entity * open_entity(int id); extern int process_entity(struct Entity *info); extern void close_entity(struct Entity *info);

Slide 23

Slide 23 text

http://github.com/MichaelDiBernardo/golang-adts