Slide 1

Slide 1 text

Why Data Literals Matters? Jérémie Grodziski - @jgrodziski

Slide 2

Slide 2 text

At the Beginning of every Project I look for Two Things

Slide 3

Slide 3 text

Some Examples of Concepts from the Domain How can I obtain Immediate Feedback?

Slide 4

Slide 4 text

Some Examples of Concepts from the Domain Concrete and Real Visible and Understandable With Data !

Slide 5

Slide 5 text

How can I obtain Immediate Feedback? From the Software I build? From my Assumptions?

Slide 6

Slide 6 text

Why the Success of JSON? Concrete and Real Visible and Understandable Data! WYSIWYG JS is ubiquituous with browser

Slide 7

Slide 7 text

Usable directly or with a simple JSON.parse()

Slide 8

Slide 8 text

Where do you usually see your Data? Stdout? Debugger? Databases? Text Files (CSV, XML, JSON)? When you write code? rarely...

Slide 9

Slide 9 text

Data Literal « A notation for representing a fixed value in source code » Available in some languages for « composite » types Array, associative structure (map, vector, object, etc.) Available in every languages for « scalar » types Integer, Float, Boolean, String, Character

Slide 10

Slide 10 text

Some « Scalars » String Boolean Numbers 1 2 3 4.5 Keyword :key1 :key2 Symbol this that λ

Slide 11

Slide 11 text

Some Data Structures List Vector Map Set

Slide 12

Slide 12 text

…and lot of functions Alan J. Perlis, developed Algol language, 1st recipient of the Turing Award For other « Perlism »: http://www.cs.yale.edu/quotes.html « It is better to have 100 functions operate on 1 data structure than 10 functions on 10 data structures »

Slide 13

Slide 13 text

Lot of functions Etc. Excerpt from clojure.org/cheatsheet

Slide 14

Slide 14 text

Tree Representation Two functions : Does this node has some child nodes? ⇒  returns a boolean How to retrieve the data structure of the child nodes? => Returns child nodes data structure

Slide 15

Slide 15 text

Tree Literal in JSON with a map and arrays Ex: JSON output of the tree command on my mac

Slide 16

Slide 16 text

Tree Literal in Clojure with map and arrays

Slide 17

Slide 17 text

Each node is an array [node-root child1 child2 …] More simple, a Tree Literal with an array of arrays tree-seq function in Clojure structure that tree and allows “walking” it with clojure.walk Viz done with github.com/ztellman/rhizome

Slide 18

Slide 18 text

Tree literal with more complex objects

Slide 19

Slide 19 text

Graph Representation Given a node Retrieve the list of identifiers of the adjacent nodes Two functions Retrieve the list of identifiers of the graph nodes

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

A graphical representation of the graph is often necessary to « touch » it Viz done with github.com/ztellman/rhizome

Slide 22

Slide 22 text

Use Cases Of Data Literals At Design-time

Slide 23

Slide 23 text

Visual Representation of your problem – Bowling Game

Slide 24

Slide 24 text

Visual Representation of your problem – Chess Game

Slide 25

Slide 25 text

Visual Representation of your problem – Sudoku Game

Slide 26

Slide 26 text

Data Literals on steroids: Clojure EDN (Extended Data Notation) The Tagged Elements of Clojure separate the literal representation from the memory representation: #tag "valueString"! Instant UUID

Slide 27

Slide 27 text

Object defined by a thread of continuity and a particular identity Ex : The bank account n° 30003 02367 000593287642 13, The credit card 8644 7023 0531 105 The EventBrite ticket for SoftShake Domain-Driven Design Immutable object that represents a concept whose equality is based on all the attributes and has no identity Ex : The 42 EUR amount The UUID f4fbe22a-9d53-4359- adbb-57f7d21ed549 The instant 21 october 2015 at 16:29 The distance 53 meters Value Object Entity

Slide 28

Slide 28 text

Tagged Data Literals are perfect for Value Object For each Value Object, add a literal representation as a « String » with valueOf(String)and toString() ! methods!

Slide 29

Slide 29 text

What’s a state? State is « summarized » with a past participle that has a static and stable meaning (order paid, user connected, etc.) State influence the object behavior State implies that some Data Transformations occured during a state transition, triggered by an event State is associated with an identity, then together they form an entity, that represents the different states and transitions along the time A state is the relation between an entity and the values that she owns at the t instant; That state can change

Slide 30

Slide 30 text

State, Identity and Function State Identity State 1 State 2 State 3 Function Function Event triggers Event triggers •  value 1a •  value 1b •  value 1c •  value 1a •  value 1b •  value 1c •  value 2a •  value 2b •  value 1a •  value 1b •  value 1c •  value 2a •  value 2b •  value 3a Publish Internal Event Publish Internal Event ... Time

Slide 31

Slide 31 text

Exanple with the Order entity Identity : Order N° 123456 Checkout Paid Shipped pay ship Payment details from Payment Gateway received Carrier data q  Customer Data q  Product Lines (product + qty) q  Shipping Data States checkout q  Customer Data q  Product Lines (product + qty) q  Shipping Data q  Payment Data q  Customer Data q  Product Lines (product + qty) q  Shipping Data q  Payment Data q  Carrier Data Front website form submitted Order submitted event Order paid event Order shipped event Time

Slide 32

Slide 32 text

Les Data Literals sont également parfait pour visualiser les transitions d’états d’une Entity Identity State 1 State 2 State 3 Function Function •  value 1a •  value 1b •  value 1c •  value 1a •  value 1b •  value 1c •  value 2a •  value 2b •  value 1a •  value 1b •  value 1c •  value 2a •  value 2b •  value 3a ... Time States

Slide 33

Slide 33 text

State transitions seen as functions Each « Snapshot » of the entity state at a particular instant can be displayed with a data literal A diff allows to visualize the differences between the prevState and the nextState provided by the function function(input): output! function(prevState, event): nextState!

Slide 34

Slide 34 text

setShippingMethod! increaseItemQty! Easy Diff

Slide 35

Slide 35 text

OOP done right with Clojure Identity State as values Function Method = function applied to an entity’s state, producing a new state (eventually) Entity = Identity + State Protocol = Interface = set of methods

Slide 36

Slide 36 text

During Testing and Debugging Use Cases Of Data Literals

Slide 37

Slide 37 text

TDD

Slide 38

Slide 38 text

Testing BDD In BDD scenarios (Behavior-Driven Development) the data can be expressed in a tabular form following the Gherkin grammar Some framework allows to input a Data Literal directly: cf framework BDD Spexec See speakerdeck.com/jgrodziski/anatomy-of-a-bdd-execution-library-in-clojure

Slide 39

Slide 39 text

Example of a BDD scenario with tabular data

Slide 40

Slide 40 text

Debugging Tools.trace is a clojure library that instrumentalize functions or a whole namespace and log functions input and output Combined with aprint (Awesome Print) it does miracles…almost no need of a debugger

Slide 41

Slide 41 text

Data Integration Use Cases Of Data Literals

Slide 42

Slide 42 text

Data Flow

Slide 43

Slide 43 text

Principles for Functions that fit Data Flows Pures Functions Immutable Data Structures seen as Data Literals Composables Steps (aka. Functions) Isolated Side Effects

Slide 44

Slide 44 text

Pure Function Always return the same result given the same arguments whatever the number of invocation (idempotence) Lead to no observable side effect (safe) Referential Transparency: Time doesn’t affect the result of a function, hence the function is transparent regarding time Allows « memoization », composition increase the testability

Slide 45

Slide 45 text

Immutable Data Structures The functional approach foster functions consuming values and returning new values The initial data structure is never modified Clojure provides an efficient implementation with Structural Sharing of Persistent Data Structures (values are shared between the old and new version of the structure)

Slide 46

Slide 46 text

Composable Functions Function 1 Function 2 Function 3 state t0 State t2 State t1 State t3

Slide 47

Slide 47 text

Isolated Side Effects Separate the data processing logic from the side effect (persistence, messaging, etc.) Place the non-pure functions at the beginning or end of the chain

Slide 48

Slide 48 text

Pure Functions or not? Pure Functions without side effects (Safe and Idempotent) •  Data Transformations •  Data structure shape (ex : map -> list) •  Values (transcoding) •  Creation of new data (ex: from computation) •  Decision : authorisation, result Non-pure Functions Impures with side effects (Non Safe or Non Idempotent) •  Safe, Non Idempotent : Data enrichment from a « mutable » external data source (ex : read from a database) •  Non Safe, Non Idempotent : Persistence (insert, update), Messaging

Slide 49

Slide 49 text

Data Pipeline Outside World Outside World Convert from External Repr. To Internal Enrich from external DataSource Validate Transform Transform Transform Operate (Persistence, Messaging) Pures Functions Get Data Non-pures Functions Non-pures Functions The non-pures functions are pushed to both ends of the pipeline

Slide 50

Slide 50 text

Domain Specific Language (DSL) Use Cases Of Data Literals

Slide 51

Slide 51 text

Domain-Specific Language A DSL with a data structure benefits from the powerful data manipulation functions of Clojure SQL HTML

Slide 52

Slide 52 text

Logs Use Cases Of Data Literals

Slide 53

Slide 53 text

Logs with Data Literals Store the contextual data of logs in a form that can be processed easily is a recommendation from the visualization tools (Splunk, Logstash)

Slide 54

Slide 54 text

Logs HTTP Requests with Data Literals Ring is the idiomatic library of Clojure for the web: each request and response are represented by a map processed by a handler

Slide 55

Slide 55 text

It’s all well and good that logs but… Can I replay a user interaction?

Slide 56

Slide 56 text

Logs HTTP Requests with Data Literals and the method invocation A simple eval allows to replay the whole set of HTTP requests

Slide 57

Slide 57 text

Clojure is an Homoiconic language Every Clojure clojure is a data structure of type List of the form: (verb a b)! Clojure is itself a Data Literal ! Allows very powerful meta-programming « Code is Data, Data is Code » from homo - the same - and icon - representation -

Slide 58

Slide 58 text

Code is Data Vector List Symbols List

Slide 59

Slide 59 text

Food for Thoughts about Event Sourcing Event Sourcing means storing all the state transitions of an application like a series of events Replay is almost built-in … but is strongly dependant on the source code version that process the event at a particular momentnt t Can we store the source code which process an event besides the even itself?

Slide 60

Slide 60 text

Food for Thoughts about Event Sourcing Storing data in a textual or literal form is the most lasting one It’s easily readable both by a human AND a machine

Slide 61

Slide 61 text

And Feedback? A REPL or a notebook enables direct interaction with Data Literals And moreover enter « in the Zone », Be in the « Flow » where productivity is at its high cf Mihály Csíkszentmihályi

Slide 62

Slide 62 text

Takeaways Make your data VISIBLE! Touch it! Data express your DOMAIN Your Data Literals are your clay, your functions are your hands that shape it Make your functions composables Isolate Side-effects

Slide 63

Slide 63 text

« An algorithm must be seen to be believed » Donal Knuth « Source Code is static, Behavior is dynamic: Good programs make it easy to reason about their Behavior » Barbara Liskov

Slide 64

Slide 64 text

« Give me exemples » « Give me something concrete it’s my job to abstract it! » « Premature Abstraction is the Root of all Evil » « Give me a REPL! »

Slide 65

Slide 65 text

Thank you! Questions? Comments? @jgrodziski www.grodziski.com Get the slides: speakerdeck.com/jgrodziski/why-data-literals-matters-en

Slide 66

Slide 66 text

Bowling Game

Slide 67

Slide 67 text

TDD Bowling Game Domain : Bowling The game consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares. A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll. ‘/’ denote a spare in the score sheet. A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled. ‘X’ denote a striker in the score sheet. In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame (so 3 balls can be rolled in tenth frame).

Slide 68

Slide 68 text

Code Kata : Number theory 2, the return Now, let’s get back to the « number theory » stories for which we will rewrite but in a TDD manner. Then we’ll compare the two solution.