2. Before we start...
As developers we all are aware of the core concepts that make up good OO code…
cohesion, loose coupling, no redundancy, encapsulation,
testability, readability, maintainability, etc.
But the question is, are we really implementing these concepts properly in code?
3. Why does my
CODE suck?
Is it MAINTAINABLE? Is it READABLE?
Is it TESTABLE?Is it REUSABLE?
Let’s ask ourselves of the following...
4. And what does it have to do with good OO coding?
What is Object Calisthenics?
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
5. Calisthenics ( cal·is·then·ics )
Definition :
Calisthenics are a form of dynamic exercise consisting of a variety of
simple, often rhythmical, movements, generally using minimal equipment or
apparatus.
Object Calisthenics
Definition :
A variety of simple, often rhythmical, exercises to achieve better OO
(Object Oriented) design and code quality.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
6. 1. One level of indentation per method
2. Don’t use the ELSE keyword
3. Wrap all primitives and Strings
4. First class collections
5. One -> per line
6. Don’t abbreviate
7. Keep all entities small
8. Limit the number of instance variables
in a class (max: 2~5)
9. Do use accessors (getters/setters)
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
7. OC #1
“Only one level of indentation per
method”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
8. function validateProducts ($products) {
foreach ($products as $rawProduct) {
$fields = array_keys($rawProduct);
foreach ($requiredFields as $requiredField) {
if (!in_array($requiredField, $fields)) {
$valid = false;
}
}
}
}
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
9. OC #1: Only one level of indentation per method
• A giant method lacks cohesiveness.
• Try to ensure that each method does exactly one thing – one
control structure, or one block of statements, per method.
• Nested control structures in a method, works at multiple levels of
abstraction which means it does more than one thing.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
10. Basically…
• Do one thing per method
• one control structure
• one block of statements
• Avoid nested control structures
• it means you are doing more than one thing
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
11. Object Calisthenics: 9 steps to better software design today, by Jeff Bay
function validateProductList ( $products) {
$invalidProducts = array_filter($products, ‘isInvalidProduct’ );
return (count($invalidProducts ) === 0);
}
function isInvalidProduct ( $rawProduct) {
$requiredFields = array(‘price’, ‘name’, ‘description’, ‘type’);
$fields = array_keys($rawProduct);
$missingFields = array_diff($requiredFields , $fields);
return (count($missingFields ) > 0);
}
reusable method
12. Benefits…
• Cohesiveness
• Methods do only one thing
• Increases re-use
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
13. OC #2
“Do not use the ‘else‘ keyword”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
14. if ($form->isValid) {
$repository = $this->getRepository( ‘MyBundle:Post’ );
if (!$repository->exists($entity)) {
$repository->save($entity);
return $this->redirect(‘create_ok’);
} else {
$error = “Post Title already exists” ;
return array(‘form’ => $form, ‘error’ => $error);
}
} else {
$error = “Invalid fields” ;
return array(‘form’ => $form, ‘error’ => $error);
}
ELSE
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
15. OC #2: Do not use the ‘else’ keyword
• Conditionals are also a frequent source of duplication.
if (status == ‘DONE’) {
doSomething();
} else {
…
}
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
16. Basically…
• Nested conditionals & long case statements
• hard to follow
• Frequent source of code duplication
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
17. $repository = $this->getRepository( ‘MyBundle:Post’ );
if (!$form->isValid) {
return array(‘form’ => $form, ‘error’ => ‘Invalid fields’ );
}
if ($repository->exists($entity)) {
return array(‘form’ => $form, ‘error’ => ‘Post Title already exists’ );
}
$repository->save($entity);
return $this->redirect(‘create_ok’);
early return
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
18. Benefits…
• Helps avoid code duplication
• Makes code clearer, easier to read
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
19. OC #3
“Wrap all primitive types and strings, if
it has behavior”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
20. class UIComponent
{
//...
public function repaint($animate = true) {
//...
}
}
//...
$component->repaint(false);
unclear operation
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
21. OC #3: Wrap all primitive types and strings, if it has behavior
• To give the programmer additional information about what the
value is and why it is being used
• This rule tries to encapsulate all the primitives within objects for
these to work in all our code in an OO-way.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
22. class UIComponent
{
//...
public function repaint(Animate $animate) {
//...
}
}
class Animate
{
public $animate
public function __construct( $animate = true) {
$this->animate = $animate;
}
}
//...
$component->repaint(new Animate(false));
This can now
encapsulate all
animation related
operations
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
23. <?php
class IntegerNumber
{
private $number;
public function __construct($number)
{
$this->number = $number;
}
//...
}
An example of how we
can wrap the int
primitive.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
24. <?php
class Item
{
final public static function find(Id $id)
{
// do find...
}
final public static function create(Id $id, array $data)
{
// do create...
}
}
$id is now
guaranteed
to be valid.
$id is now
guaranteed
to be valid.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
25. Benefits…
• More maintainable code
• Helps identify what should be an Object
• Type hinting
• Encapsulation of operations
• Context of value is more obvious
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
26. OC #4
“Use first class collections”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
28. OC #4: Use first class collections
• Any class that contains a collection should contain no other
member variables.
• Each collection gets wrapped in its own class. So now, behaviors
related to the collection have a home.
• Class will contain related behaviors.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
29. class Items
{
private $item = [];
public function add(Item $item, array $data)
{
// do add...
}
public function filter(array $filters)
{
// do filter...
}
}
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
30. Benefits…
• Implements collection operations
• Uses SPL interfaces
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
31. OC #5
“Only one -> per line”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
34. OC #5: Only one -> per line
• An object should not deal with two other objects at once
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
35. Basically…
• Multiple ->
• indicate misplaced responsibilities
• violate encapsulation
• Law of Demeter
• “Talk only to your friends”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
36. Benefits…
• Readability
• Easier Mocking
• Easier to Debug
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
37. OC #6
“Do NOT abbreviate”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
38. if ($sx >= $sy) {
if ($sx > $strSysMatImgW ) {
//…
}
if ($ny > $strSysMatImgH ) {
//…
}
} else {
if ($sy > $strSysMatImgH ) {
//…
}
if ($nx > $strSysMatImgW ) {
//…
}
}
?
?
?
?
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
39. OC #6: Do NOT abbreviate
• Abbreviations can be confusing, and they tend to hide larger
problems.
• Keep class and method names to 1-2 words, and avoid names that
duplicate the context.
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
40. Basically…
• Keep names to one-two words
• class names
• method names
• entities
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
41. function getPage($data) { ... }
function startProcess() { ... }
$tr->process(“site.login”);
Use a thesaurus: fork,
create, begin, open
Easy understanding,
complete scope:
$translatorService
Use clearer names:
fetchPage()
downloadPage()
get from where?
table row?
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
43. OC #7
“Keep all entities small”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
44. OC #7: Keep classes small
• 200 lines per class (including docblocks)
• 15 - 20 lines per method
• 10 methods per class
• 15 classes per namespace (package or folder)
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
45. Benefits…
• Easier to grasp quickly
• Single responsibility
• Objective method
• Slimmer namespaces
• Less clunky folders
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
46. OC #8
“Limit the number of instance variables
in a class (max: 2 5)”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
48. Benefits…
• Shorter dependency list
• Better understanding of commonality
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
49. OC #9
“Do not use accessors (getter/setter)”
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
50. class Tally
{
public $total = 0;
public function add($amount)
{
$this->total += $amount;
}
}
$tally = new Tally();
$tally->add(1);
// some other code...
$tally->total = -1;
// some other code...
$tally->add(1);
echo $tally->total . PHP_EOL
$total can be changed without
this instance knowing
causes unexpected results
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
51. class Tally
{
private $total = 0;
public function add($amount)
{
$this->total += $amount;
}
public function getTotal()
{
return $this->total;
}
}
$tally = new Tally();
$tally->add(1);
$tally->add(1);
echo $tally->getTotal() . PHP_EOL
total cannot be “reset”
no unexpected results
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
52. Benefits…
• Injector operations
• Encapsulation of transformations
Object Calisthenics: 9 steps to better software design today, by Jeff Bay
53. 1. “Only one level of indentation per method”
2. “Do not use the ‘else‘ keyword”
3. “Wrap all primitive types and strings, if it has behavior”
4. “Use first class collections”
5. “Only one -> per line”
6. “Do NOT abbreviate”
7. “Keep all entities small”
8. “Limit the number of instance variables in a class (max: 2 5)”
9. “Do not use accessors (getter/setter)”
The Recap...
54. • To visualize and implement the holy grail of object oriented
programming – encapsulation of data
• To use polymorphism appropriately
• To encourage concise and straightforward naming standards
• To craft code that has no duplication in code or idea
The Aim...