Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elegant Objects and Cactoos

Elegant Objects and Cactoos

Overview of the EO paradigm and small overview of the Cactoos library for Porto Codes Meetup

Filipe Freire

December 21, 2017
Tweet

More Decks by Filipe Freire

Other Decks in Programming

Transcript

  1. Elegant objects & Cactoos
    Filipe Freire

    21 December 2017

    View Slide

  2. Quick intro
    Learner, tester, developer, husband

    OSS contributor
    2y work as a “coding” tester

    1y work as a developer Currently @
    filfreire.com
    2

    View Slide

  3. Elegant Objects
    3

    View Slide

  4. It’s an OOP paradigm
    4

    View Slide

  5. Book
    Yegor Bugayenko @yegor256
    www.yegor256.com
    5

    View Slide

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

    View Slide

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

    View Slide

  8. So, what are the recommendations?
    8

    View Slide

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

    View Slide

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

    View Slide

  11. Don’t treat objects as 

    data structures bags of data.

    Ever.
    11

    View Slide

  12. Maintainability > everything else
    12

    View Slide

  13. “Objects as living beings”
    Birth Working life Retirement
    13

    View Slide

  14. Some examples…
    14

    View Slide

  15. Birth
    15

    View Slide

  16. Code Free Constructors
    16

    View Slide

  17. 17

    View Slide

  18. “Object’s name != job title”
    18

    View Slide

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

    View Slide

  20. Helper, Handler, Writer, Reader,

    Converter, Observer, Listener,
    Sorter, Encoder, Decoder, …
    20

    View Slide

  21. Helper, Handler, Writer, Reader,

    Converter, Observer, Listener,
    Sorter, Encoder, Decoder, …
    21

    View Slide

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

    plus data
    22

    View Slide

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

    encapsulated data
    23

    View Slide

  24. A Finder of Prime Numbers
    vs
    A List of numbers that 

    returns only primes
    24

    View Slide

  25. Education & Work life
    25

    View Slide

  26. No Getters and Setters.

    Not even once.
    26

    View Slide

  27. No Getters and Setters

    ?! ?!
    27

    View Slide

  28. Again, don’t treat objects as 

    bags of data.

    Ever.
    28

    View Slide

  29. Computer-style:
    29

    View Slide

  30. Human-style:
    30

    View Slide

  31. “An object works by contracts”
    31

    View Slide

  32. “An object works by contracts”
    Always use interfaces
    32

    View Slide

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

    33

    View Slide

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

    ExcelTennisMatch
    TextTennisMatch
    JsonTennisMatch
    34

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  39. This can
    change
    State is
    the same
    39

    View Slide

  40. 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

    View Slide

  41. Retirement
    41

    View Slide

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

    View Slide

  43. - Hello, is it a software department?

    - Yes.

    - Let me talk to your employee "Jeffrey"
    please.

    - Hold the line please...

    - Hello.

    - …
    43

    View Slide

  44. - 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

    View Slide

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

    View Slide

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

    View Slide

  47. Useful building blocks from
    Guava, Apache Commons, JDK

    + EO paradigm
    47

    View Slide

  48. 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

    View Slide

  49. Example 1
    49

    View Slide

  50. Example 2
    50

    View Slide

  51. Example 3
    51

    View Slide

  52. Summing up
    52

    View Slide

  53. Maintainability
    53

    View Slide

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

    View Slide

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

    View Slide

  56. Thank you. Questions?
    filfreire filrfreire
    filfreire.com
    56

    View Slide