Overview of the EO paradigm and small overview of the Cactoos library for Porto Codes Meetup
Elegant objects & CactoosFilipe Freire 21 December 2017
View Slide
Quick introLearner, tester, developer, husband OSS contributor2y work as a “coding” tester 1y work as a developer Currently @filfreire.com2
Elegant Objects3
It’s an OOP paradigm4
BookYegor Bugayenko @yegor256www.yegor256.com5
BookYegor Bugayenko @yegor256www.yegor256.comSet of recommendations:- Cleaner code- Better classes- Visible architecture6
Disclaimer:You can find out moreat yegor256.comYoutube @yegor256and on the EO books.7
So, what are the recommendations?8
GettersSettersMutable objectsStatic methodsAnnotationsData ObjectsType CastingEtc.9
GettersSettersMutable objectsStatic methodsAnnotationsData ObjectsType CastingEtc.10
Don’t treat objects as data structures bags of data. Ever.11
Maintainability > everything else12
“Objects as living beings”Birth Working life Retirement13
Some examples…14
Birth15
Code Free Constructors16
17
“Object’s name != job title”18
Object’s name != job titleMeaning:Avoid the use of “-ER”19
Helper, Handler, Writer, Reader, Converter, Observer, Listener,Sorter, Encoder, Decoder, …20
Helper, Handler, Writer, Reader, Converter, Observer, Listener,Sorter, Encoder, Decoder, …21
An object isn’t:1) A link between worlds2) A collection of procedures plus data22
An object is:1) Self-sufficient2) Representative of encapsulated data23
A Finder of Prime NumbersvsA List of numbers that returns only primes24
Education & Work life25
No Getters and Setters. Not even once.26
No Getters and Setters ?! ?!27
Again, don’t treat objects as bags of data. Ever.28
Computer-style:29
Human-style:30
“An object works by contracts”31
“An object works by contracts”Always use interfaces32
Example“I want results for a Tennis match”Tennis_31Feb.xlsxTENNIS.txttennis_res.jsonSources:…33
Example“I want results for a Tennis match”Tennis_31Feb.xlsxTENNIS.txttennis_res.jsonSources:…ExcelTennisMatchTextTennisMatchJsonTennisMatch34
Example“I want results for a Tennis match”ExcelTennisMatchTextTennisMatchJsonTennisMatchTennisMatchGameMatchimplementextends35
Excel, Text, Json, etc.Contract stays the sameMore Decoupling and more Maintainability36
“A good object should neverchange his encapsulated state.”37
“A good object should neverchange his encapsulated state.”38Be immutable
This canchangeState isthe same39
Some benefitsThread SafetyAvoiding Temporal CouplingAvoiding side effectsAvoiding identity mutability(more at http://yegor256.com/2014/06/09/objects-should-be-immutable.html)40
Retirement41
Don’t accept null argumentsDon’t return null42
- Hello, is it a software department? - Yes. - Let me talk to your employee "Jeffrey"please. - Hold the line please... - Hello. - …43
- 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
There’s more to it,Let’s save it for another talk45
https://github.com/yegor256/cactoos46
Useful building blocks fromGuava, Apache Commons, JDK + EO paradigm47
Meaning…No nullNo code in constructorsNo getters and settersNo mutable objectsNo static methods, not even private ones(among other stuff)48
Example 149
Example 250
Example 351
Summing up52
Maintainability53
–W. Edwards Deming"Build quality into the product rather thantrying to test it in later."54
Quality from the startForce strict control of code quality.Ex: Static Analysis -> mandatory55
Thank you. Questions?filfreire filrfreirefilfreire.com56