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

Object Calesthenics

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Object Calesthenics

Thunder talk hold at SilverStripe Europe Conference in Malta 2017.

wernerkrauss

October 14, 2017

Transcript

26 slides
  1. 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
  2. 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
  3. #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
  4. OMFG! • Method does too much • Hard to understand

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

    • Easier to understand 14.10.2017 www.silverstrip.es 7
  6. #3: Wrap Primitives and Strings • If it has behaviour

    • Encapsulate primitives within Objects • Better Type Hinting 14.10.2017 www.silverstrip.es 14
  7. #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
  8. #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
  9. #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
  10. #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
  11. #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
  12. 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
  13. #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
  14. #11: Test your code • „Stupid“ mistakes are found faster

    • Easier refactoring 14.10.2017 www.silverstrip.es 25
  15. 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

More Decks by wernerkrauss

Other Decks in Technology