By Joeri Timmermans
34 years old
Gaming, food, reading, education
Accountancy – Informatics
Interactive Multimedia Design
Developer
Project Manager
Learn & Development Manager
(1999 – 2007)
(2017 - 2021)
(2011 - 2017)
(2007 - 2011)
(2021 - now)
/in/joeri-timmermans
www.joeritimmermans.be
@joeri_timmer
hello@joeritimmermans.be
Joined Nov 2014
/company/iodigital-com
www.iodigital.com
@iodigital_com
/company/iodigital-com
www.iodigital.com
@iodigital_com
https://coderwall.com/pix-art
Year 2012
https://www.pix-art.be
Year 2014
https://www.joeritimmermans.be
Year 2020
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.
- John F. Woods, September 1991
The moment you're presented with a problem you have to solve, it's your duty to do this in the best possible way.
- Me, How to save a kitten by writing clean code
The ratio of time spent reading versus writing is well over 10 to 1. Making it easy to read code makes it easier to write.
- Robert C. Martin, Clean Code
Simple
Complex
VS
class EventService {
...
public function signUp(Invitation $invite, User $user)
{
// 1. Check if the invitation is valid
// 2. Check if the user already signed up for the event
// 3. Register the invitation
// 4. Sign the user up for the event
// 5. Return signup
}
...
}
class EventService {
...
public function signUp(Invitation $invite, User $user)
{
$this->guardThatInvitationIsValid($invite);
$event = $invite->getEvent();
$this->guardThatUserIsNotSignedUp($user, $event);
$this->invitationService->register($user, $invite);
$signUp = new SignUp($user, $event);
$event->addSignUp($signUp);
return $signUp;
}
...
}
class EventController {
...
public function signUpAction(Invitation $invite, User $user)
{
try {
$signup = $this->eventService->signUp($invite, $user);
} catch (GuardException $e) {
return View::create($e->getMessage(), 403);
} catch (ErrorException $e) {
return View::create($e->getMessage(), 400);
}
return View::create($signup, 200);
}
...
}
function writeFile($fullpath, $content)
{
if (is_writable($fullpath)) {
if ($fp = fopen($fullpath,'w')) {
if (fwrite($fp,$content)) {
// ...
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
function writeFile($fullpath, $content)
{
if (!is_writable($fullpath)) {
return false;
}
if (!$fp = fopen($fullpath,'w')) {
return false;
}
if (fwrite($fp,$content)) {
return true;
}
return false;
}
Bad example
Good example
Write a file
class ExampleService
{
public function doThis(Example $example)
{
if (!$example->isAwesome()) {
throw new NotAwesomeException("This is not an awesome example!");
}
//Do this
}
public function doThat(Example $example)
{
if (!$example->isAwesome()) {
throw new NotAwesomeException("This is not an awesome example!");
}
//Do that
}
}
Bad example
class ExampleService
{
public function doThis(Example $example)
{
$this->isAwesomeExample($example);
//Do this
}
public function doThat(Example $example)
{
$this->isAwesomeExample($example);
//Do that
}
private function isAwesomeExample(Example $example)
{
if (!$example->isAwesome()) {
throw new NotAwesomeException("This is not an awesome example!");
}
}
}
Good example
class EventService {
...
public function signUp(Invitation $invite, User $user)
{
if ($invite->hasBeenUsed() && $invite->hasCorrectDate() && $invite->isValid()) {
$event = $invite->getEvent();
if ($this->checkIfUserIsSignedup($user, $event)) {
$this->invitationService->register($user, $invite);
$signUp = new SignUp($user, $event);
$event->addSignUp($signUp);
} else {
throw new AlreadySignedupException();
}
} else {
throw new InvalidInvitationException();
}
return $signUp;
}
...
}
Bad example
class EventService {
...
public function signUp(Invitation $invite, User $user)
{
$this->guardThatInvitationIsValid($invite);
$event = $invite->getEvent();
$this->guardThatUserIsNotSignedUp($user, $event);
$this->invitationService->register($user, $invite);
$signUp = new SignUp($user, $event);
$event->addSignUp($signUp);
return $signUp;
}
...
}
Good example
“Always leave the campground cleaner than you found it.”
A tool that helps users control their code, from small to large projects and team collaboration
/git/git
A tool that looks for potential
problems in your code.
/phpmd/phpmd
A static analysis tool
to discover bugs in your code without running it.
/phpstan/phpstan
A tool that creates a git hook
and does a list of checks before you can commit.
/phpro/grumphp
A tool created by Sensiolabs to fix most violations against the PHP PSR-1 and PSR-2 standards.
/FriendsOfPHP/PHP-CS-Fixer
My Blog 🤓
$ vendor/bin/behat
Feature: pagevisit
This is a standard behat test to see if the
given page is visited and show the right content
Scenario: When i visit the school i should see things
# features/pagevisit.feature:4
Given I am on "school"
# FeatureContext::visit()
Then I should see "Good students"
# FeatureContext::assertPageContainsText()
Then I should see "Interesting topics"
# FeatureContext::assertPageContainsText()
1 scenario (1 passed)
3 steps (3 passed)
0m7.77s (13.74Mb)