Presentation made at GTA meetup in 2012-02-07.
Object Calisthenics is a set of exercise rules to reach better code, maintainable, testable and readable.
5. Object Calisthenics
Erm... WTH is Object Calisthenics?
‣ Object Calisthenics
Term derived from greek,
“exercise”, under the
context of gymnastics.
6. Object Calisthenics
Erm... WTH is Object Calisthenics?
‣ Jeff Bay in The ThoughtWorks Anthology [1]
coined the term Object Calisthenics in computers,
as a group of exercises to Object Oriented
programming.
[1] The ThoughtWorks Anthology: Essays on Software Technology and Innovation
7. Object Calisthenics
Motivation
‣ Readable Code
‣ Comprehensible
‣ Testable
‣ Maintainable
Learning about good code practices
at Object CaIisthenics talk of
@guilhermeblanco on @gtaphp
25. Object Calisthenics
Rule 3: Wrap primitive types and strings
‣ This rule cannot be completely ported to PHP, because
the language does not perform well with an entirely
Object Oriented code with a huge amount of instances
26. Object Calisthenics
Rule 3: Wrap primitive types and strings
‣ But... if the variable of primitive type has a behavior, it
must be encapsulated
27. Object Calisthenics
Rule 3: Wrap primitive types and strings
class UIComponent
{
! // ...
!
public function repaint($animate = true)
{
// ...
}
}
// ...
$component->repaint(false);
28. Object Calisthenics
Rule 3: Wrap primitive types and strings
class UIComponent
{
! // ...
!
public function repaint($animate = true)
{
// ...
}
}
// ...
$component->repaint(false);
29. Object Calisthenics
Rule 3: Wrap primitive types and strings
class UIComponent
{
! // ...
!
public function repaint(Animate $animate)
{
// ...
}
}
class Animate
{
public $animate;
public function __construct($animate = true)
{
$this->animate = $animate;
}
}
// ...
$component->repaint(new Animate(false));
32. Object Calisthenics
Rule 4: Only one dot per line
‣ ...but multiple nested calls...
‣ tend to expose an encapsulation problem
‣ increase difficulty to debug and exception handling
‣ do not represent an atomic action
34. Object Calisthenics
Rule 4: Only one dot per line
‣ A chain of different objects, but only if the execution
only includes getters and setters
$user->getLocationPoint()->getCountry()->getName();
35. Object Calisthenics
Rule 4: Only one dot per line
‣ A chain of a unique object, through the usage of a
fluent interface
$filterChain->addFilter(new Zend_Filter_Alpha())
->addFilter(new Zend_Filter_StringToLower());
39. Object Calisthenics
Rule 5: Do not abbreviate
‣ Think about it... why do you want to abbreviate?
‣ Write the same name repeatedly?
40. Object Calisthenics
Rule 5: Do not abbreviate
‣ Think about it... why do you want to abbreviate?
‣ Write the same name repeatedly?
‣ Then your method is reused multiple times,
signalling a code duplication.
41. Object Calisthenics
Rule 5: Do not abbreviate
‣ Think about it... why do you want to abbreviate?
‣ Write the same name repeatedly?
‣ Then your method is reused multiple times,
signalling a code duplication.
‣ Method name too long?
42. Object Calisthenics
Rule 5: Do not abbreviate
‣ Think about it... why do you want to abbreviate?
‣ Write the same name repeatedly?
‣ Then your method is reused multiple times,
signalling a code duplication.
‣ Method name too long?
‣ Maybe your class has multiple responsibilities or it
is missing a helper class (bad architecture).
43. Object Calisthenics
Rule 5: Do not abbreviate
‣ Think about it... why do you want to abbreviate?
‣ Write the same name repeatedly?
‣ Then your method is reused multiple times,
signalling a code duplication.
‣ Method name too long?
‣ Maybe your class has multiple responsibilities or it
is missing a helper class (bad architecture).
46. Object Calisthenics
Rule 6: Keep your entities small
‣ Adapted to PHP: 100 lines per class and no more than
15 classes per package.
‣ The change is necessary because of the lack of rule for
documentation, which can easily occupy up to 50% of
the lines of a class.
47. Object Calisthenics
Rule 7: Do not create classes with more than 2 instance variables
‣ Do not create classes with more than 2 instance
variables
48. Object Calisthenics
Rule 7: Do not create classes with more than 2 instance variables
‣ Objective:
‣ Low cohesion
‣ Better encapsulation
49. Object Calisthenics
Rule 7: Do not create classes with more than 2 instance variables
‣ The original rule points to 2 instance variables
‣ To PHP, the suggestion is no more than 5 variables
51. Object Calisthenics
Rule 8: Use first class collections
‣ The rule is simple: Any class that contains a collection
(array to PHP), cannot contain any other properties
52. Object Calisthenics
Rule 8: Use first class collections
‣ Objectives:
‣ Specific behaviors have a good place to stay
‣ Filtering, combining, mapping, ...
53. Object Calisthenics
Rule 8: Use first class collections
‣ DoctrineCommonCollectionsArrayCollection
‣ Countable
‣ IteratorAggregate (inherits Traversable)
‣ ArrayAccess
54. Object Calisthenics
Rule 9: Do not create getter/setter methods to properties
‣ Do not create getter/setter methods to properties
55. Object Calisthenics
Rule 9: Do not create getter/setter methods to properties
‣ Non-applicable to PHP due to language’s nature
56. Object Calisthenics
Rule 9: Do not create getter/setter methods to properties
/**
* THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
*/
class ApplicationCoreDomainUserModelUserProxy
! extends ApplicationCoreDomainUserModelUser
! implements DoctrineORMProxyProxy
{
// ...
public function getId()
{
$this->__load();
return parent::getId();
}
public function setId($id)
{
$this->__load();
return parent::setId($id);
}
// ...
}