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

Object Calesthenics

Object Calesthenics

Thunder talk hold at SilverStripe Europe Conference in Malta 2017.

wernerkrauss

October 14, 2017
Tweet

More Decks by wernerkrauss

Other Decks in Technology

Transcript

  1. Object Calesthenics 14.10.2017 www.silverstrip.es 1

  2. Object… WHAT? • Cal • is • then • ics

    - /ˌkaləsˈTHeniks/ • Gymnastics • 9 +2 simple, formalised exercises • Invented by Jeff Bay in his book “The ThoughtWorks Anthology” • Adapted for PHP by Raphael Dohms 14.10.2017 www.silverstrip.es 2
  3. Focus on • Maintainability • Readability • Testability • Comprehensibility

    • Reusability 14.10.2017 www.silverstrip.es 3
  4. How to use it • Guidelines, not rules! • Pick

    one and start using it • Add the next items next 14.10.2017 www.silverstrip.es 4
  5. #1: Only One Level Of Intendation Per Method • Better

    readability and understanding of the code • Extract code to reusable methods, that do one thing 14.10.2017 www.silverstrip.es 5
  6. OMFG! • Method does too much • Hard to understand

    • Repeated code – Who copy/pasted that? 14.10.2017 www.silverstrip.es 6
  7. Clean it up • Duplicated code extracted and now reusable

    • Easier to understand 14.10.2017 www.silverstrip.es 7
  8. #2: No Else Keyword No! 14.10.2017 www.silverstrip.es 8

  9. NEVER EVER 14.10.2017 www.silverstrip.es 9

  10. 14.10.2017 www.silverstrip.es 12 You never come here when you have

    a $fieldName
  11. Possible Solution 14.10.2017 www.silverstrip.es 13 Return early Single path, no

    logal tree.
  12. #3: Wrap Primitives and Strings • If it has behaviour

    • Encapsulate primitives within Objects • Better Type Hinting 14.10.2017 www.silverstrip.es 14
  13. Do you remember? 14.10.2017 www.silverstrip.es 15

  14. Much cleaner nowadays 14.10.2017 www.silverstrip.es 16

  15. #4: Use First Class Collections • SilverStripe has ArrayList 14.10.2017

    www.silverstrip.es 17
  16. #5: One Arrow Per Line • If not a getter

    or fluent • Otherwise hard to debug, test, read and understand • See Demeter‘s Law 14.10.2017 www.silverstrip.es 18
  17. #6: Don‘t Abbrevate • Use clear and easy to understand

    names • Don‘t call your methods like your company or your cat‘s name (if it has nothing to do with it) • Your co-worker will thank you 14.10.2017 www.silverstrip.es 19
  18. #7: Keep Your Classes Small • 100 lines per class

    (incl. Docblocks) • 15 classes per package (aka. namespace or folder) • Single Responsibility • Slimmer namespaces and folders 14.10.2017 www.silverstrip.es 20
  19. #8: Max. 5 Instance Variables Per Class • SilverStripe specific

    config ($db etc…) does not count • Easier Mocking for tests • Less depencies 14.10.2017 www.silverstrip.es 21
  20. #9: No Getters / Setters • Tell, don‘t ask •

    It‘s OK to use accessors to get a state • Any decision on the state should be made inside the object 14.10.2017 www.silverstrip.es 22
  21. No Getters/Setters • Procedural code gets information then makes decisions.

    Object-oriented code tells objects to do things (Alec Sharp) • https://pragprog.com/articles/ tell-dont-ask 14.10.2017 www.silverstrip.es 23
  22. #10: Document Your Code • Don‘t explain bad code, fix

    it! • Mark @todo items • Automatic API docs possible 14.10.2017 www.silverstrip.es 24
  23. #11: Test your code • „Stupid“ mistakes are found faster

    • Easier refactoring 14.10.2017 www.silverstrip.es 25
  24. Sources 14.10.2017 www.silverstrip.es 26

  25. About me • Werner M. Krauß • Located in Hallstatt,

    Austria • wmk on IRC slack / github / stackoverflow • PHP since 1998 • Freelancer since 2006 (netwerkstatt) • SilverStripe since 2009 14.10.2017 www.silverstrip.es 27
  26. Thank you! www.silverstrip.es 14.10.2017 www.silverstrip.es 28