Clean Code
By Joeri Timmermans
Hi, I’m Joeri!
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
Campuses
/company/iodigital-com
www.iodigital.com
@iodigital_com
Here to help you grow
Stimulating
talent growth
Shaping campuses
In-depth expertise
/company/iodigital-com
www.iodigital.com
@iodigital_com
How did I end up here?
Blogging
https://coderwall.com/pix-art
Year 2012
https://www.pix-art.be
Year 2014
https://www.joeritimmermans.be
Year 2020
Public speaking
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 😢
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
Interaction loading!
Documentation
Pen & Paper
ERD Schema
Flowchart
Step 2
Writing
Organising
Simple
Complex
VS
Our own festival!
- Valid invitation
- User already signed up
- Destroy invitation
- Sign up user
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
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
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
“Always leave the campground cleaner than you found it.”
Step 4
Tools
Interaction loading!
Git
A tool that helps users control their code, from small to large projects and team collaboration
/git/git
PHP MD
A tool that looks for potential
problems in your code.
/phpmd/phpmd
PHPSTAN
A static analysis tool
to discover bugs in your code without running it.
/phpstan/phpstan
GrumPHP
A tool that creates a git hook
and does a list of checks before you can commit.
/phpro/grumphp
PHPCS Fixer
A tool created by Sensiolabs to fix most violations against the PHP PSR-1 and PSR-2 standards.
/FriendsOfPHP/PHP-CS-Fixer
Step 5
Experimenting
Exploring
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
Being a good developer,
is an art
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,626