Clean Code

By Joeri Timmermans

Hi, I’m Joeri!

32 years old
Gaming, cycling, food, reading


Accountancy – Informatics


Interactive Multimedia Design


Developer

 

Project Manager

 

(1999 – 2007)

(2017 - now)

(2011 - 2017)

(2007 - 2011)

/pix-art

/in/joeri-timmermans

www.pix-art.be

@joeri_timmer

Blogging 📖

https://www.intracto.com/nl-be/blog

Public speaking 🎙

/intracto

/company/intracto

www.intracto.com

@intractobe

Strategy / Creation / Marketing / Technology

We push companies towards digital success

Belgium

  • Herentals
  • Antwerp
  • Mechelen
  • Ghent
  • Brussels

Netherlands

  • Den Bosch
  • Amsterdam
  • Utrecht
  • Eindhoven
  • Tilburg
  • Gorinchem
  • Breda

+700 Experts

Strategy

Creation

Marketing

Technology

Focus on growth

+30% each year

Constant evolution

Never the same

Ambition

Focus on knowledge

Personal growth

Education

Conferences

Open source

Interactivity!

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

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;
    }
    
    ...
}

Organising

Simple

Complex

VS

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);
    }
    
    ...
}

Return often

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

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

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 homepage i should see notifications 
  # features/pagevisit.feature:4
    Given I am on "/interactive-multimedia-design-imd"   
    # FeatureContext::visit()
    Then I should see "Interactive multimedia design (IMD)" 
    # FeatureContext::assertPageContainsText()
    Then I should see "Professionele Bachelor" 
    # 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

  1. It's a lifestyle
  2. Be your own critic, and identify mistakes
  3. Read (Books, blogs, ...)

Interaction loading!

Interaction loading!

Clean Code

By Joeri Timmermans

Loading comments...

More from Joeri Timmermans