Clean Code
By Joeri Timmermans
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Hi, I’m Joeri!
34 years old
Gaming, food, reading, education
Accountancy – Informatics
Interactive Multimedia Design
Developer
Project Manager
Learn & Development Manager
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682604/Picture1.png)
(1999 – 2007)
(2017 - 2021)
(2011 - 2017)
(2007 - 2011)
(2021 - now)
/in/joeri-timmermans
www.joeritimmermans.be
@joeri_timmer
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058033/instagram-square-brands.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058034/house-user-solid.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058035/linkedin-brands.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058039/envelope-solid.png)
hello@joeritimmermans.be
Joined Nov 2014
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9297283/iO-logo-300x300-purple.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039226/pasted-from-clipboard.png)
Campuses
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682677/font-awesome_4-7-0_home_256_0_5576e6_none.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682678/font-awesome_4-7-0_instagram_256_0_5576e6_none.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682679/font-awesome_4-7-0_linkedin_256_0_5576e6_none.png)
/company/iodigital-com
www.iodigital.com
@iodigital_com
Here to help you grow
Stimulating
talent growth
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039308/pasted-from-clipboard.png)
Shaping campuses
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039306/pasted-from-clipboard.png)
In-depth expertise
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039307/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682677/font-awesome_4-7-0_home_256_0_5576e6_none.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682678/font-awesome_4-7-0_instagram_256_0_5576e6_none.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682679/font-awesome_4-7-0_linkedin_256_0_5576e6_none.png)
/company/iodigital-com
www.iodigital.com
@iodigital_com
How did I end up here?
Blogging
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058262/pasted-from-clipboard.png)
https://coderwall.com/pix-art
Year 2012
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058273/pasted-from-clipboard.png)
https://www.pix-art.be
Year 2014
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9058287/pasted-from-clipboard.png)
https://www.joeritimmermans.be
Year 2020
Public speaking
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6512499/Joeri-Clean_Code-6461.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/7201799/Joeri_studenten-02997.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6512501/Joeri-Clean_Code-6455.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6512502/Joeri-Clean_Code-6505.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6512503/Joeri-Clean_Code-6522.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/7201805/Joeri_studenten-02996.jpg)
Interaction loading!
Interaction loading!
Interaction loading!
Interaction loading!
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
Interaction loading!
Real life example 😢
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5683915/FotorCreated.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039319/iO-logo_black.png)
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
Interaction loading!
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
6 steps
to clean coding
Step 1
Understanding
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Interaction loading!
Documentation
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6523948/Screenshot_2019-09-10_11.34.00.png)
Pen & Paper
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6523967/IMG_0637.jpg)
ERD Schema
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5724132/Screenshot_2019-01-29_12.20.06.png)
Flowchart
Step 2
Writing
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Organising
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5724190/Screenshot_2016-04-11_15.17.52.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/6642285/pasted-from-clipboard.png)
Simple
Complex
VS
Our own festival!
- Valid invitation
- User already signed up
- Destroy invitation
- Sign up user
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/7822915/pasted-from-clipboard.png)
Commenting/Pseudocode
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
}
...
}
Naming
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;
}
...
}
Indenting
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);
}
...
}
Think positive negative
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
- Check if writing is allowed
- Open the file
- Write content
Step 3
Principles
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Interaction loading!
Don't repeat yourself (DRY)
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
Don't repeat yourself (DRY)
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
Keep it simple stupid (KISS)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5906248/source.gif)
Keep it simple stupid (KISS)
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
Boy Scout Rule
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5683915/FotorCreated.jpg)
“Always leave the campground cleaner than you found it.”
Step 4
Tools
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Interaction loading!
Git
A tool that helps users control their code, from small to large projects and team collaboration
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682673/font-awesome_4-7-0_github-square_256_0_5576e6_none.png)
/git/git
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5786592/git-clean-history.gif)
PHP MD
A tool that looks for potential
problems in your code.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682673/font-awesome_4-7-0_github-square_256_0_5576e6_none.png)
/phpmd/phpmd
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5786620/pasted-from-clipboard.png)
PHPSTAN
A static analysis tool
to discover bugs in your code without running it.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682673/font-awesome_4-7-0_github-square_256_0_5576e6_none.png)
/phpstan/phpstan
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5786616/Screenshot_2019-02-15_14.20.00.png)
GrumPHP
A tool that creates a git hook
and does a list of checks before you can commit.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682673/font-awesome_4-7-0_github-square_256_0_5576e6_none.png)
/phpro/grumphp
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5786625/demo.gif)
PHPCS Fixer
A tool created by Sensiolabs to fix most violations against the PHP PSR-1 and PSR-2 standards.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5682673/font-awesome_4-7-0_github-square_256_0_5576e6_none.png)
/FriendsOfPHP/PHP-CS-Fixer
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5786633/68747470733a2f2f7261772e6769746875622e636f6d2f706665666665726c652f61746f6d2d7068702d63732d66697865722f6d61737465722f7068702d63732d66697865722e676966.gif)
Step 5
Experimenting
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Exploring
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5725325/Screenshot_2019-01-29_16.29.56.png)
My Blog 🤓
Testing
$ 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)
Step 6
Practicing
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/9039362/iO-logo_white.png)
Being a good developer,
is an art
![](https://s3.amazonaws.com/media-p.slid.es/uploads/202607/images/5725385/cyborg-drawing-picture-picture-id937060922.jpg)
3 Tips
- It's a lifestyle
- Be your own critic, and identify mistakes
- Read (Books, blogs, ...)
Interaction loading!
Interaction loading!
Clean Code
By Joeri Timmermans
Clean Code
Talk inspired upon my blogpost: https://www.joeritimmermans.be/blog/how-to-save-a-kitten-by-writing-clean-code/
- 1,546