object calisthenics
DESCRIPTION
TRANSCRIPT
![Page 1: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/1.jpg)
Object Calisthenics@krolow
![Page 2: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/2.jpg)
● code is hard to understand, test and maintain
● OO will save us!● it's hard the transaction of paradigms
Procedural --- to --- OOP● Qualities matters:
○ cohesion○ loose coupling○ no redundancy○ encapsulation○ testability○ readability○ testability
WTF?
![Page 3: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/3.jpg)
so what is it about?
It's an exercise, a practice to help you to write good oriented-object code!
![Page 4: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/4.jpg)
The RULES
#1 One level of indentation per method#2 Don't use ELSE keyword#3 Wrap all primitives and String#4 First class collections#5 One dot per file#6 Don't abbreviate#7 Keep all entities small#8 No classes with more than two instances variable#9 No getters/setters/properties
![Page 5: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/5.jpg)
#RULE1You shall not ident more than once!
![Page 6: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/6.jpg)
#RULE2You shall not have "else"
![Page 7: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/7.jpg)
#RULE3You shall encapsulate the primitives types
not totally applicable in PHP because of performance issues
but... we can do this with other types...
![Page 8: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/8.jpg)
#RULE4Your collection shall not have other members variable
![Page 9: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/9.jpg)
#RULE 5You shall use one dot per file, so you know each object has the responsible
not totally applicable in PHP...... but nested calls
● show some problems of encapsulationmake harder to debug...
● we can use... in chain of get and setters in chain of objects with a fluent interface
![Page 10: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/10.jpg)
#RULE 6You must not abbreviate
why I should abbreviate?● because I use the name several times...● .... maybe your method is to heavily and you should remove
duplication● or because the names are too long...● ... maybe you are misplacing responsibilities or there is a missing
class...
Avoid also duplicate of words...
![Page 11: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/11.jpg)
#RULE 7Keep your entity classes smaller!
50 rows by class10 classes by package...
maybe 100 rows by class and 15 by package it's also OK!
![Page 12: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/12.jpg)
#RULE 8Class shall have less than 2 instance variables
Yep quite hard this... but let's try...
with this we can decomposition, we can separate the concerns...
maybe 5 variables it's also okay don't you think?
![Page 13: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/13.jpg)
#RULE 9You shall not have getter and setters
WTF?
Yeah, PHP it's not possible apply that :|
BUT...
If we apply the #RULE 8 we may get this!
![Page 14: Object Calisthenics](https://reader035.vdocuments.net/reader035/viewer/2022081821/553887df4a79598e758b47da/html5/thumbnails/14.jpg)