Slide 1

Slide 1 text

Elegant objects & Cactoos Filipe Freire
 21 December 2017

Slide 2

Slide 2 text

Quick intro Learner, tester, developer, husband
 OSS contributor 2y work as a “coding” tester
 1y work as a developer Currently @ filfreire.com 2

Slide 3

Slide 3 text

Elegant Objects 3

Slide 4

Slide 4 text

It’s an OOP paradigm 4

Slide 5

Slide 5 text

Book Yegor Bugayenko @yegor256 www.yegor256.com 5

Slide 6

Slide 6 text

Book Yegor Bugayenko @yegor256 www.yegor256.com Set of recommendations: - Cleaner code - Better classes - Visible architecture 6

Slide 7

Slide 7 text

Disclaimer: You can find out more at yegor256.com Youtube @yegor256 and on the EO books. 7

Slide 8

Slide 8 text

So, what are the recommendations? 8

Slide 9

Slide 9 text

Getters Setters Mutable objects Static methods Annotations Data Objects Type Casting Etc. 9

Slide 10

Slide 10 text

Getters Setters Mutable objects Static methods Annotations Data Objects Type Casting Etc. 10

Slide 11

Slide 11 text

Don’t treat objects as 
 data structures bags of data.
 Ever. 11

Slide 12

Slide 12 text

Maintainability > everything else 12

Slide 13

Slide 13 text

“Objects as living beings” Birth Working life Retirement 13

Slide 14

Slide 14 text

Some examples… 14

Slide 15

Slide 15 text

Birth 15

Slide 16

Slide 16 text

Code Free Constructors 16

Slide 17

Slide 17 text

17

Slide 18

Slide 18 text

“Object’s name != job title” 18

Slide 19

Slide 19 text

Object’s name != job title Meaning: Avoid the use of “-ER” 19

Slide 20

Slide 20 text

Helper, Handler, Writer, Reader,
 Converter, Observer, Listener, Sorter, Encoder, Decoder, … 20

Slide 21

Slide 21 text

Helper, Handler, Writer, Reader,
 Converter, Observer, Listener, Sorter, Encoder, Decoder, … 21

Slide 22

Slide 22 text

An object isn’t: 1) A link between worlds 2) A collection of procedures 
 plus data 22

Slide 23

Slide 23 text

An object is: 1) Self-sufficient 2) Representative of 
 encapsulated data 23

Slide 24

Slide 24 text

A Finder of Prime Numbers vs A List of numbers that 
 returns only primes 24

Slide 25

Slide 25 text

Education & Work life 25

Slide 26

Slide 26 text

No Getters and Setters.
 Not even once. 26

Slide 27

Slide 27 text

No Getters and Setters
 ?! ?! 27

Slide 28

Slide 28 text

Again, don’t treat objects as 
 bags of data.
 Ever. 28

Slide 29

Slide 29 text

Computer-style: 29

Slide 30

Slide 30 text

Human-style: 30

Slide 31

Slide 31 text

“An object works by contracts” 31

Slide 32

Slide 32 text

“An object works by contracts” Always use interfaces 32

Slide 33

Slide 33 text

Example “I want results for a Tennis match” Tennis_31Feb.xlsx TENNIS.txt tennis_res.json Sources: … 33

Slide 34

Slide 34 text

Example “I want results for a Tennis match” Tennis_31Feb.xlsx TENNIS.txt tennis_res.json Sources: … ExcelTennisMatch TextTennisMatch JsonTennisMatch 34

Slide 35

Slide 35 text

Example “I want results for a Tennis match” ExcelTennisMatch TextTennisMatch JsonTennisMatch TennisMatch GameMatch implement extends 35

Slide 36

Slide 36 text

Excel, Text, Json, etc. Contract stays the same More Decoupling and more Maintainability 36

Slide 37

Slide 37 text

“A good object should never change his encapsulated state.” 37

Slide 38

Slide 38 text

“A good object should never change his encapsulated state.” 38 Be immutable

Slide 39

Slide 39 text

This can change State is the same 39

Slide 40

Slide 40 text

Some benefits Thread Safety Avoiding Temporal Coupling Avoiding side effects Avoiding identity mutability (more at http://yegor256.com/2014/06/09/objects-should-be-immutable.html) 40

Slide 41

Slide 41 text

Retirement 41

Slide 42

Slide 42 text

Don’t accept null arguments Don’t return null 42

Slide 43

Slide 43 text

- Hello, is it a software department?
 - Yes.
 - Let me talk to your employee "Jeffrey" please.
 - Hold the line please...
 - Hello.
 - … 43

Slide 44

Slide 44 text

- Hello, is it a software department?
 - Yes.
 - Let me talk to your employee "Jeffrey" please.
 - Hold the line please...
 - Hello.
 - Are you NULL? (more at http://www.yegor256.com/2014/05/13/why-null-is-bad.html) 44

Slide 45

Slide 45 text

There’s more to it, Let’s save it for another talk 45

Slide 46

Slide 46 text

https://github.com/yegor256/cactoos 46

Slide 47

Slide 47 text

Useful building blocks from Guava, Apache Commons, JDK
 + EO paradigm 47

Slide 48

Slide 48 text

Meaning… No null No code in constructors No getters and setters No mutable objects No static methods, not even private ones (among other stuff) 48

Slide 49

Slide 49 text

Example 1 49

Slide 50

Slide 50 text

Example 2 50

Slide 51

Slide 51 text

Example 3 51

Slide 52

Slide 52 text

Summing up 52

Slide 53

Slide 53 text

Maintainability 53

Slide 54

Slide 54 text

–W. Edwards Deming "Build quality into the product rather than trying to test it in later." 54

Slide 55

Slide 55 text

Quality from the start Force strict control of code quality. Ex: Static Analysis -> mandatory 55

Slide 56

Slide 56 text

Thank you. Questions? filfreire filrfreire filfreire.com 56