Performance and testing are just one aspect of code, to really be successful your code needs to be readable, maintainable and generally easier to comprehend and work with. This talk draws from my own experience in applying the techniques of object calisthenics and code readability, within an existing team. It will help you identify trouble areas, learn how to refactor them and train you to write better code in future projects avoiding common pitfalls.
8. Is it Maintainable? Is it Readable?
Why does my
code suck?
Is it Reusable? Is it Testable?
9. <?php Does it look like this?
$list=mysql_connect("******","*******","*****");
if(!$list)echo 'Cannot login.';
else{
mysql_select_db("******", $list);
$best=array("0","0","0","0","0","0");
$id=mysql_num_rows(mysql_query("SELECT * FROM allnews"));
$count=0;
for($i=0;$i<6;$i++){
while(mysql_query("SELECT language FROM allnews WHERE id=$id-$count")!="he")$count+
+;
$best[$i]=mysql_query("SELECT id FROM allnews WHERE id=$id-$count");}
$id=$id-$count;
$maxdate=mktime(0,0,0,date('m'),date('d')-7,date('Y'));
while(mysql_query("SELECT date FROM allnews WHERE id=$id-$count")>=$maxdate){
if(mysql_query("SELECT language FROM allnews WHERE id=$id-$count")=="he"){
$small=$best[0];
while($i=0;$i<6;$i++){
if(mysql_query("SELECT score FROM allnews WHERE id=
$small)"<mysql_query("SELECT score FROM allnews WHERE id=$best[i+1]"))
$small=$best[i+1];}
if(mysql_query("SELECT score FROM allnews WHERE id=
$small")<mysql_query("SELECT score FROM allnews WHERE id=$id-$count")){
while($i=0;$i<6;$i++){
if($small==$best[i])$best[i]=mysql_query("SELECT id FROM
allnews WHERE id=$id-$count");}}}}
while($i=0;$i<6;$i++)
echo '<a href="news-page.php?id='.$best[i].'"><div class="box '.mysql_query("SELECT
type FROM allnews WHERE id=$best[i]").'">'.mysql_query("SELECT title FROM allnews WHERE id=
$best[i]").'<div class="img" style="background-image:url(images/'.mysql_query("SELECT
image1 FROM allnews WHERE id=$best[i]").');"></div></div></a>';
mysql_close($list);
}
?>
10. <?php Does it look like this?
$list=mysql_connect("******","*******","*****");
if(!$list)echo 'Cannot login.';
else{
mysql_select_db("******", $list);
$best=array("0","0","0","0","0","0");
$id=mysql_num_rows(mysql_query("SELECT * FROM allnews"));
$count=0;
for($i=0;$i<6;$i++){
while(mysql_query("SELECT language FROM allnews WHERE id=$id-$count")!="he")$count+
+;
$best[$i]=mysql_query("SELECT id FROM allnews WHERE id=$id-$count");}
$id=$id-$count;
$maxdate=mktime(0,0,0,date('m'),date('d')-7,date('Y'));
while(mysql_query("SELECT date FROM allnews WHERE id=$id-$count")>=$maxdate){
if(mysql_query("SELECT language FROM allnews WHERE id=$id-$count")=="he"){
$small=$best[0];
while($i=0;$i<6;$i++){
if(mysql_query("SELECT score FROM allnews WHERE id=
$small)"<mysql_query("SELECT score FROM allnews WHERE id=$best[i+1]"))
$small=$best[i+1];}
if(mysql_query("SELECT score FROM allnews WHERE id=
$small")<mysql_query("SELECT score FROM Rebecca WHERE id=$id-$count")){
If allnews Black was a developer
while($i=0;$i<6;$i++){
if($small==$best[i])$best[i]=mysql_query("SELECT id FROM
allnews WHERE id=$id-$count");}}}}
while($i=0;$i<6;$i++)
echo '<a href="news-page.php?id='.$best[i].'"><div class="box '.mysql_query("SELECT
type FROM allnews WHERE id=$best[i]").'">'.mysql_query("SELECT title FROM allnews WHERE id=
$best[i]").'<div class="img" style="background-image:url(images/'.mysql_query("SELECT
image1 FROM allnews WHERE id=$best[i]").');"></div></div></a>';
mysql_close($list);
}
?>
13. cal·is·then·ics - noun - /ˌkaləsˈTHeniks/
Calisthenics are a form of dynamic
exercise consisting of a variety of
simple, often rhythmical, movements,
generally using minimal equipment or
apparatus.
Object Calisthenics
14. cal·is·then·ics - noun - /ˌkaləsˈTHeniks/
Calisthenics are a form of dynamic
exercise consisting of a variety of
simple, often rhythmical, movements,
generally using minimal equipment or
apparatus.
Object Calisthenics
A variety of simple, often
rhythmical, exercises to achieve
better OO and code quality
15. “So here’s an exercise that can help you to internalize
principles of good object-oriented design and actually
use them in real life.”
-- Jeff Bay
Object Calisthenics
16. “So here’s an exercise that can help you to internalize
principles of good object-oriented design and actually
use them in real life.”
-- Jeff Bay
Object Calisthenics
Important:
Written for JAVA
Adaptations are needed for PHP, Ruby,
JS, Python
18. “You need to write code that minimizes the time it would
Object Calisthenics
take someone else to understand it—even if that
someone else is you.”
+
-- Dustin Boswell and Trevor Foucher
Readability Tips
19. “You need to write code that minimizes the time it would
Object Calisthenics
take someone else to understand it—even if that
someone else is you.”
+
-- Dustin Boswell and Trevor Foucher
Readability Tips
I’m a tip
47. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
48. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
49. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
50. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
51. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
52. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
53. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
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);
}
}
54. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid()){
$repository = $this->getRepository('MyBundle:Post');
if (!$repository->exists($entity) ) {
$repository->save($entity);
intermediate variable
return $this->redirect('create_ok');
} else {
$error = "Post Title already exists";
return array('form' => $form, 'error' => $error);
}
intermediate variable
} else {
$error = "Invalid fields";
return array('form' => $form, 'error' => $error);
}
}
55. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ( ! $form->isValid()){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
56. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ( ! $form->isValid()){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
57. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
removed intermediates
if ( ! $form->isValid()){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
58. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
removed intermediates
if ( ! $form->isValid()){
return array('form' => $form, 'error' => 'Invalid fields');
}
early return
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
59. Separate code
into blocks.
public function createPost($request)
{ Its like using
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post'); Paragraphs.
$form = new MyForm($entity);
$form->bind($request);
removed intermediates
if ( ! $form->isValid()){
return array('form' => $form, 'error' => 'Invalid fields');
}
early return
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
66. class UIComponent
{
//...
public function repaint( Animate $animate ){
//...
}
}
class Animate
{
public $animate;
public function __construct( $animate = true ) {
$this->animate = $animate;}
}
//...
$component->repaint( new Animate(false) );
67. class UIComponent
{
//...
public function repaint( Animate $animate ){
//...
}
}
class Animate
This can now encapsulate all
{
animation related operations
public $animate;
public function __construct( $animate = true ) {
$this->animate = $animate;}
}
//...
$component->repaint( new Animate(false) );
68. Key Benefits
• Helps identify what should be an Object
• Type Hinting
• Encapsulation of operations
69. Ad
ap
te
d
OC #4
“Only one -> per line”
or dot
* getter chain or a fluent interface
71. Source: CodeIgniter
properties are harder to mock
$this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this-
>CI->uri->slash_segment(2, 'both');
$this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
72. Source: CodeIgniter
properties are harder to mock
$this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this-
>CI->uri->slash_segment(2, 'both');
no whitespace
$this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
73. Source: CodeIgniter
properties are harder to mock
$this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this-
>CI->uri->slash_segment(2, 'both');
no whitespace
$this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
- Underlying encapsulation problem
- Hard to debug and test
- Hard to read and understand
74. Source: CodeIgniter
properties are harder to mock
$this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this-
>CI->uri->slash_segment(2, 'both');
no whitespace
$this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
move everything to uri object
$this->getCI()->getUriBuilder()->getBaseUri(‘leading’);
- Underlying encapsulation problem
- Hard to debug and test
- Hard to read and understand
88. Why do you abbreviate?
Its repeated many times,
and i’m lazy.
89. Why do you abbreviate?
Its repeated many times,
and i’m lazy.
Underlying Problem!
You need to transfer those operations into a separate class.
90. Why do you abbreviate?
function processResponseHeadersAndDefineOutput($response) { ... }
91. Why do you abbreviate?
function processResponseHeadersAndDefineOutput($response) { ... }
This method name is too long to type,
and i’m lazy.
92. Why do you abbreviate?
more than one
responsibility?
function processResponseHeadersAndDefineOutput($response) { ... }
This method name is too long to type,
and i’m lazy.
94. get from where?
function getPage($data) { ... }
function startProcess() { ... }
$tr->process(“site.login”);
95. get from where?
Use clearer names:
function getPage($data) { ... } fetchPage()
downloadPage()
function startProcess() { ... }
$tr->process(“site.login”);
96. get from where?
Use clearer names:
function getPage($data) { ... } fetchPage()
downloadPage()
Use a thesaurus:
function startProcess() { ... }
fork, create, begin, open
$tr->process(“site.login”);
97. get from where?
Use clearer names:
function getPage($data) { ... } fetchPage()
downloadPage()
Use a thesaurus:
function startProcess() { ... }
fork, create, begin, open
Table row?
$tr->process(“site.login”);
98. get from where?
Use clearer names:
function getPage($data) { ... } fetchPage()
downloadPage()
Use a thesaurus:
function startProcess() { ... }
fork, create, begin, open
Table row?
Easy understanding, complete scope:
$tr->process(“site.login”);
$translatorService
99. Key Benefits
• Clearer communication and maintainability
• Indicates underlying problems
101. 200 lines per class
10 methods per class
15 classes per package
102. Increased to include
docblocks
200 lines per class
10 methods per class
15 classes per package
103. Increased to include
docblocks 15-20 lines per method
200 lines per class
10 methods per class
15 classes per package
104. Increased to include
docblocks 15-20 lines per method
200 lines per class
10 methods per class
15 classes per package
read this as
namespace or folder
105. Key Benefits
• Single Responsibility
• Objective and clear methods
• Slimmer namespaces
• Avoids clunky folders
106. Ad
ap
OC #7
te
d
“Limit the number of
instance variables in a
class (2 to 5)”
109. class MyRegistrationService
{
protected $userService;
protected $passwordService;
protected $logger;
All DB interaction protected $translator;
should be in protected $entityManager;
userService protected $imageCropper; Use an event based
system and move this
// ... to listener
}
Limit: 5
113. Key Benefits
• Implements collection operations
• Uses SPL interfaces
• Easier to merge collections and not worry
about member behaviour in them
114. Dr
op
pe
d
OC #9
“Do not use accessors
(getter/setter)”
* Use them if you code PHP
115. /**
* THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
*/
class DoctrineTestsModelsCMSCmsUserProxy
extends DoctrineTestsModelsCMSCmsUser
implements DoctrineORMProxyProxy
{
public function getId()
{
$this->__load();
return parent::getId();
}
public function getStatus()
{
$this->__load();
return parent::getStatus();
}
Property get/set syntax RFC may change the game.
116. /**
* THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
*/
class DoctrineTestsModelsCMSCmsUserProxy
extends DoctrineTestsModelsCMSCmsUser
implements DoctrineORMProxyProxy
{
public function getId()
{
$this->__load(); Example: Doctrine uses getters to
return parent::getId(); inject lazy loading operations
}
public function getStatus()
{
$this->__load();
return parent::getStatus();
}
Property get/set syntax RFC may change the game.
118. Cr
ea
te
d!
OC #10 (bonus!)
“Document your code!”
119. //check to see if the section above set the $overall_pref variable to void
if ($overall_pref == 'void')
// implode the revised array of selections in group three into a string
// variable so that it can be transferred to the database at the end of the
// page
$groupthree = implode($groupthree_array, "nr");
120. really?
//check to see if the section above set the $overall_pref variable to void
if ($overall_pref == 'void')
// implode the revised array of selections in group three into a string
// variable so that it can be transferred to the database at the end of the
// page
$groupthree = implode($groupthree_array, "nr");
121. really?
//check to see if the section above set the $overall_pref variable to void
if ($overall_pref == 'void')
// implode the revised array of selections in group three into a string
// variable so that it can be transferred to the database at the end of the
// page
$groupthree = implode($groupthree_array, "nr");
Documenting because i’m doing it wrong in an unusual way
123. $priority = isset($event['priority']) ? $event['priority'] : 0;
if (!isset($event['event'])) {
throw new InvalidArgumentException(...));
}
if (!isset($event['method'])) {
$event['method'] = 'on'.preg_replace(array(
'/(?<=b)[a-z]/ie',
'/[^a-z0-9]/i'
), array('strtoupper("0")', ''), $event['event']);
}
$definition->addMethodCall(
'addListenerService',
array($event['event'],
array($listenerId, What does this do?
$event['method']),
$priority
));
Source: Symfony2
124. $priority = isset($event['priority']) ? $event['priority'] : 0;
Add a simple comment:
if (!isset($event['event'])) {
throw new //Strips special chars and camel cases to onXxx
InvalidArgumentException(...));
}
if (!isset($event['method'])) {
$event['method'] = 'on'.preg_replace(array(
'/(?<=b)[a-z]/ie',
'/[^a-z0-9]/i'
), array('strtoupper("0")', ''), $event['event']);
}
$definition->addMethodCall(
'addListenerService',
array($event['event'],
array($listenerId, What does this do?
$event['method']),
$priority
));
Source: Symfony2
125. $priority = isset($event['priority']) ? $event['priority'] : 0;
Add a simple comment:
if (!isset($event['event'])) {
throw new //Strips special chars and camel cases to onXxx
InvalidArgumentException(...));
}
if (!isset($event['method'])) {
$event['method'] = 'on'.preg_replace(array(
Don’t explain bad
'/(?<=b)[a-z]/ie', code, fix it!
'/[^a-z0-9]/i'
), array('strtoupper("0")', ''), $event['event']);
}
$definition->addMethodCall(
'addListenerService',
array($event['event'],
array($listenerId, What does this do?
$event['method']),
$priority
));
Source: Symfony2
126. /**
* Checks whether an element is contained in the collection.
* This is an O(n) operation, where n is the size of the collection.
*
* @todo implement caching for better performance
* @param mixed $element The element to search for.
* @return boolean TRUE if the collection contains the element, or FALSE.
*/
function contains($element);
127. /**
* Checks whether an element is contained in the collection.
* This is an O(n) operation, where n is the size of the collection.
*
* @todo implement caching for better performance
* @param mixed $element The element to search for.
* @return boolean TRUE if the collection contains the element, or FALSE.
*/
function contains($element);
mark todo items so the
changes don’t get lost
128. A note on cost of
running function
/**
* Checks whether an element is contained in the collection.
* This is an O(n) operation, where n is the size of the collection.
*
* @todo implement caching for better performance
* @param mixed $element The element to search for.
* @return boolean TRUE if the collection contains the element, or FALSE.
*/
function contains($element);
mark todo items so the
changes don’t get lost
129. Do a mind dump,
then clean it up.
A note on cost of
running function
/**
* Checks whether an element is contained in the collection.
* This is an O(n) operation, where n is the size of the collection.
*
* @todo implement caching for better performance
* @param mixed $element The element to search for.
* @return boolean TRUE if the collection contains the element, or FALSE.
*/
function contains($element);
mark todo items so the
changes don’t get lost
130. Do a mind dump,
then clean it up.
A note on cost of
running function
/**
* Checks whether an element is contained in the collection.
* This is an O(n) operation, where n is the size of the collection.
*
* @todo implement caching for better performance
* @param mixed $element The element to search for.
* @return boolean TRUE if the collection contains the element, or FALSE.
*/
function contains($element);
Generate API docs
with phpDocumentor
mark todo items so the
changes don’t get lost
131. Key Benefits
• Automatic API documentation
• Transmission of “line of thought”
• Avoids confusion
132. Recap
• #1 - Only one indentation level per method.
• #2 - Do not use the ‘else’ keyword.
• #3 - Wrap primitive types and strings.
• #4 - Only one -> per line.
• #5 - Do not Abbreviate.
• #6 - Keep your classes small.
• #7 - Limit the number of instance variables in a class (max: 5)
• #8 - Use first class collections
• #9 - Use accessors (getter/setter) *
• #10 - Document your code!
134. Recommended Links:
The ThoughtWorks Anthology
http://goo.gl/OcSNx
The Art of Readable Code
http://goo.gl/unrij
DISCLAIMER: This talk re-uses some of the examples used by Guilherme Blanco in his
original Object Calisthenic talk. These principles were studied and applied by us while we
worked together in previous jobs. The result taught us all a lesson we really want to spread
to other developers.