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
#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
#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
#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
#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
#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
#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
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
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