The document provides advice and best practices for writing code that lasts over time. It discusses improving code through practices like making it more comprehensible, flexible, tested, and refactorable. Specific techniques mentioned include object calisthenics exercises like limiting indentation levels and instance variables, using first class collections, and avoiding getters/setters. The document emphasizes continuously improving code through practices like reading and sharing code with others.
23. NIH
| nɒt ɪnˈventɪd hɪə |
Not Invented Here —The German art of humiliating any technology,
agricultural product, or (medical) scientific work that has not been
invented or produced in Germany, Switzerland, or Austria.
source: Urban dictionary
photo by: John 'K'
42. Calisthenics
/ˌ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.
photo by: boston_public_library
46. 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);
}
}
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);
}
}
actual goal of function
“Create Post"
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);
}
}
this is all
error
handling
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();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
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');
}
51. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
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');
}
exit condition 1: invalid form
52. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
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');
}
exit condition 2: duplicated post
53. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
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');
}
this is what the
method actually
does
55. public function createPost($request)
{
$repository = $this->getRepository('MyBundle:Post');
$entity = new Post();
$repository->save($entity);
return $this->redirect('create_ok');
}
FormValidationMiddleware
UniqueEntityMiddleware
56. public function createPost($request)
{
$repository = $this->getRepository('MyBundle:Post');
$entity = new Post();
$repository->save($entity);
return $this->redirect('create_ok');
}
The power of PSR-7
and middleware
FormValidationMiddleware
UniqueEntityMiddleware
78. Improve your naming,
secure your API, grow
awesome teams, handle
errors, flip out, check out
MySQL 8, Drupal 8 and
HTML 5, go PSR-7, git you
guts and finally, survive.
#phpyorkshire
79. Improve your naming,
secure your API, grow
awesome teams, handle
errors, flip out, check out
MySQL 8, Drupal 8 and
HTML 5, go PSR-7, git you
guts and finally, survive.
#phpyorkshire