Learning Object-Oriented Programming, Design and TDD with Pharo
Total Page:16
File Type:pdf, Size:1020Kb
Learning Object-Oriented Programming, Design and TDD with Pharo Stéphane Ducasse with Damien Pollet September 29, 2017 master @ 86ac50a © 2017 by Stéphane Ducasse with Damien Pollet. This work is licensed under a Creative Commons “Attribution- NonCommercial-NoDerivatives 4.0 International” license. This is a human-readable summary of (and not a substitute for) the license: https://creativecommons.org/licenses/by-nc-nd/4.0/deed.en You are free to: s Share Copy and redistribute the material in any medium or format. The licensor cannot revoke these freedoms as long as you follow the license terms. Under the following terms: b Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. n NonCommercial You may not use the material for commercial purposes. d NoDerivatives If you remix, transform, or build upon the material, you may not distribute the modified material. No additional restrictions You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Layout and typography based on the sbabook LATEX class by Damien Pollet. Contents Illustrations viii 1 About this book 1 1.1 A word of presentation ............................ 1 1.2 Structure of the book ............................. 1 1.3 What you will learn .............................. 3 1.4 Typographic conventions ........................... 4 1.5 Videos ..................................... 5 1.6 Thanks ..................................... 5 I Getting in touch with Pharo 2 Pharo syntax in a nutshell 9 2.1 Simplicity and elegance of messages ..................... 9 2.2 Which message is executed first? ....................... 12 2.3 Sending messages to classes ......................... 13 2.4 Local variables and statement sequences ................... 14 2.5 About literal objects .............................. 14 2.6 Sending multiple messages to the same object ................ 15 2.7 Blocks ..................................... 16 2.8 Control structures ............................... 16 2.9 Methods .................................... 17 2.10 Resources ................................... 18 2.11 Conclusion .................................. 19 2.12 Syntax summary ................................ 20 3 Challenge yourself 25 3.1 Challenge: Message identification ....................... 25 3.2 Challenge: Literal objects ........................... 27 3.3 Challenge: Kind of messages ......................... 27 3.4 Challenge: Results ............................... 28 3.5 Challenge: unneeded parentheses ...................... 29 i Contents 4 Developing a simple counter 31 4.1 Our use case .................................. 31 4.2 Create your own class ............................. 32 4.3 Define protocols and methods ........................ 33 4.4 Define a Test Class ............................... 36 4.5 Saving your work ............................... 36 4.6 Adding more messages ............................ 39 4.7 Better object description ........................... 40 4.8 Instance initialization method ......................... 41 4.9 Conclusion ................................... 42 5 Tests, tests and tests 43 5.1 What is a test? ................................. 43 5.2 Test Driven Design ............................... 44 5.3 Why testing is important ........................... 45 5.4 What makes a good test? ........................... 46 5.5 right-Bicep ................................... 47 5.6 SUnit by example ............................... 47 5.7 The SUnit cookbook .............................. 51 5.8 Defining a fixture ............................... 52 5.9 Chapter summary ............................... 53 6 Some collection katas with words 55 6.1 Isogram .................................... 55 6.2 About strings ................................. 56 6.3 A solution using sets .............................. 56 6.4 Defining a test ................................. 58 6.5 Some fun: Obtaining french isograms ..................... 60 6.6 Pangrams ................................... 61 6.7 Handling alphabet ............................... 63 6.8 Identifying missing letters ........................... 64 6.9 Palindrome as exercise ............................ 67 6.10 Conclusion ................................... 67 II About objects and classes 7 Objects and classes 73 7.1 Objects: Entities reacting to messages .................... 73 7.2 Messages and Methods ............................ 75 7.3 An object is a protective entity ........................ 76 7.4 An object protects its data ........................... 77 7.5 With counters ................................. 79 7.6 A class: blueprint or factory of objects .................... 79 7.7 Class and instances are really different .................... 82 7.8 Conclusion ................................... 82 ii Contents 8 Revisiting objects and classes 83 8.1 A simple and naive file system ......................... 83 8.2 Studying a first scenario ............................ 84 8.3 Defining a class ................................ 85 8.4 Printing a directory .............................. 87 8.5 Adding files .................................. 88 8.6 One message and multiple methods ..................... 90 8.7 Objects: stepping back ............................ 91 8.8 Examples of distribution of responsibilities .................. 91 8.9 Important points ............................... 93 8.10 Distribution of responsibilities ........................ 94 8.11 So far so good? Not fully! ........................... 95 8.12 Conclusion ................................... 95 9 Converter 97 9.1 First a test ................................... 97 9.2 Define a test method (and more) ....................... 98 9.3 The class TemperaturConverter ........................ 99 9.4 Converting from Farhenheit to Celsius .................... 100 9.5 About floats .................................. 100 9.6 Printing rounded results ............................ 101 9.7 Building a map of degrees ........................... 102 9.8 Spelling Fahrenheit correctly! ......................... 103 9.9 Adding logging behavior ............................ 104 9.10 Discussion ................................... 106 9.11 Conclusion ................................... 107 10 An electronic wallet 109 10.1 A first test ................................... 109 10.2 Adding coins .................................. 110 10.3 Looking at Bag ................................. 111 10.4 Using a bag for a wallet ............................ 112 10.5 More tests ................................... 113 10.6 Testing money ................................. 113 10.7 Checking to pay an amount .......................... 114 10.8 Biggest coin .................................. 114 10.9 Biggest below a value ............................. 115 10.10 Improving the API ............................... 115 10.11 Coins for paying: First version ......................... 117 10.12 Better heuristics ................................ 118 10.13 Conclusion ................................... 119 11 Crafting a simple embedded DSL with Pharo 121 11.1 Getting started ................................. 121 11.2 Rolling a die .................................. 123 11.3 Creating another test ............................. 123 iii Contents 11.4 Instance creation interface .......................... 124 11.5 First specification of a die handle ....................... 126 11.6 Defining the DieHandle class ......................... 127 11.7 Improving programmer experience ...................... 128 11.8 Rolling a die handle .............................. 128 11.9 About Dice and DieHandle API ........................ 130 11.10 Role playing syntax .............................. 131 11.11 Handle’s addition ............................... 133 11.12 Conclusion ................................... 133 III Sending messages 12 Sending a message is making a choice 137 12.1 Negation: the not message .......................... 138 12.2 Implementing not ............................... 138 12.3 Implementing disjunction ........................... 140 12.4 About ifTrue:ifFalse: implementation ..................... 142 12.5 What is the point? ............................... 143 12.6 Conclusion ................................... 146 IV Looking at inheritance 13 Inheritance: Incremental definition and behavior reuse 149 13.1 Inheritance .................................. 149 13.2 Improving files/directories example design .................. 151 13.3 Transformation strategies ........................... 151 13.4 Factoring out state ............................... 152 13.5 Factoring similar methods ........................... 155 13.6 Sending a message and method lookup ................... 156 13.7 Basic method overrides ............................ 157 13.8 self-send messages and lookup create hooks ................. 158 13.9 Hook/Template explanations ......................... 159 13.10 Essence of self and dispatch .......................... 160 13.11 Instance variables vs. messages ........................ 162 13.12 Conclusion ................................... 162 14 Extending superclass behavior 163 14.1 Revisiting printOn: ............................... 163 14.2 Improving the situation ............................ 164 14.3 Extending superclass behavior using super .................. 166 14.4 Another