SlideShare a Scribd company logo
Hello
@PeteJMcFarlane
Simple Made Easy
How to write code you
won't hate tomorrow
<!/> "#
KISS
KISS
KISS
KISS
Keep It Simple, Stupid
how do we measure code
quality?
how do we measure code quality?
4 number of classes
4 number of tests
4 using latest framework
4 PSR compliant
4 number of bugs shipped
how do we measure code quality?
can we:
4 read it easily
4 reason about it clearly
4 replace it quickly
readable
4 small functions/classes
4 good names
4 avoid nested code (ifs/loops) - extract methods
4 avoid else
4 return early or use a guard clause
4 avoid negation !
$data = $db->query("INSERT LONG, COMPLICATED SQL QUERY HERE");
echo '<ul>';
for ($i = 0; $i < count($data); $i++) {
echo "<li>$data[$i][0] - $data[$i][1]</li>";
}
echo '</ul>';
$people = $db->query("INSERT LONG, COMPLICATED SQL QUERY HERE");
echo '<ul>';
for ($i = 0; $i < count($people); $i++) {
echo "<li>$people[$i][0] - $people[$i][1]</li>";
}
echo '</ul>';
$people = $db->query("INSERT LONG, COMPLICATED SQL QUERY HERE");
echo '<ul>';
foreach ($people as $person) {
echo "<li>$person[0] - $person[1]</li>";
}
echo '</ul>';
$people = $db->query("INSERT LONG, COMPLICATED SQL QUERY HERE");
echo '<ul>';
foreach ($people as $person) {
echo "<li>{$person['name']} - {$person['email']}</li>";
}
echo '</ul>';
$people = $db->query("INSERT LONG, COMPLICATED SQL QUERY HERE");
$listElements = array_reduce($people, function ($html, $person) {
return $html . "<li>{$person['name']} - {$person['email']}</li>";
});
return '<ul>' . $listElements . '</ul>';
$people = $db->query("INSERT LONG, COMPLICATED SQL QUERY HERE");
return array_reduce($people, function ($html, $person) {
return $html . "<li>{$person['name']} - {$person['email']}</li>";
}, '<ul>') . '</ul>;
Input:
_ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_|
|_| ||_ _| | _||_| ||_| _|
Output:
"0123456789"
return implode(array_map(function ($char) {
return $this->translate($char);
}, array_map('implode', transpose(array_map(function ($line) {
return str_split($line, 3);
}, explode("n", $ascii))))));
$lines = explode("n", $ascii);
$chunksOf3 = array_map(function ($line) {
return str_split($line, 3);
}, $lines);
$transposed = transpose($chunksOf3);
$characters = array_map('implode', $transposed);
$numbers = array_map(function ($char) {
return $this->translate($char);
}, $characters);
return implode($numbers);
if ($request->isValid()) {
$user = $request->getUser();
if ($user->hasPermissions()) {
foreach ($request->getUpdates() as $update) {
if ($update != null) {
$this->processUpdate($update);
}
}
} else {
throw new PermissionDeniedException;
}
} else {
throw new InvalidRequestException;
}
if (!$request->isValid()) {
throw new InvalidRequestException;
} else {
$user = $request->getUser();
if ($user->hasPermissions()) {
foreach ($request->getUpdates() as $update) {
if ($update != null) {
$this->processUpdate($update);
}
}
} else {
throw new PermissionDeniedException;
}
}
if (!$request->isValid()) {
throw new InvalidRequestException;
}
$user = $request->getUser();
if ($user->hasPermissions()) {
foreach ($request->getUpdates() as $update) {
if ($update != null) {
$this->processUpdate($update);
}
}
} else {
throw new PermissionDeniedException;
}
$this->guardAgainstInvalidRequest($request);
$user = $request->getUser();
if ($user->hasPermissions()) {
foreach ($request->getUpdates() as $update) {
if ($update != null) {
$this->processUpdate($update);
}
}
} else {
throw new PermissionDeniedException;
}
$this->guardAgainstInvalidRequest($request);
$user = $request->getUser();
if (!$user->hasPermissions()) {
throw new PermissionDeniedException;
}
foreach ($request->getUpdates() as $update) {
if ($update != null) {
$this->processUpdate($update);
}
}
$this->guardAgainstInvalidRequest($request);
$this->guardAuthorisedUser($request->getUser());
foreach ($request->getUpdates() as $update) {
if ($update != null) {
$this->processUpdate($update);
}
}
$this->guardAgainstInvalidRequest($request);
$this->guardAuthorisedUser($request->getUser());
$updates = array_filter($request->getUpdates());
$this->processUpdates($updates);
Good code doesn't just
emerge by accident
1
Me
function example()
{
$data = ...;
if (!$this->resultIsOK($data)) {
$result = false;
} else {
$result = true;
}
return $result;
}
function example()
{
$data = ...;
if ($this->resultIsOK($data)) {
$result = true;
} else {
$result = false;
}
return $result;
}
function example()
{
$data = ...;
if ($this->resultIsOK($data)) {
return true;
} else {
return false;
}
}
function example()
{
$data = ...;
if ($this->resultIsOK($data)) {
return true;
}
return false;
}
function example()
{
$data = ...;
return $this->resultIsOK($data) ? true : false;
}
function example()
{
$data = ...;
return $this->resultIsOK($data);
}
understandable
4 small, composable functions - that do one thing
4 hide complexity
4 good names - variables, functions, namespaces,
classes
4 small public API
4 TDD
4 Value objects
How to write code you won't hate tomorrow
<?php
$d = 10;
$t = 3600;
$s = $d / t;
echo $s;
class Distance
{
private $km;
private function __construct() {}
public static function fromKm($km)
{
$d = new static;
$d->km = $km;
return $d;
}
public function asKm()
{
return $this->km;
}
}
class Time
{
private $seconds;
private function __construct() {}
public static function fromSeconds($seconds)
{
$t = new static;
$t->seconds = $seconds;
return $t;
}
public function inHours()
{
return $this->seconds / 3600;
}
}
class Speed
{
private $kmph;
public static function fromDistanceAndTime(Distance $d, Time $t)
{
$s = new static;
$s->kmph = $d->asKm() / $t->inHours();
return $s;
}
public function asMph()
{
return $this->kmph * 1.6093;
}
}
<?php
$d = Distance::fromKm(10); // 10 km
$t = Time::fromSeconds(3600); // 1 hr
$s = Speed::fromDistanceAndTime($d, $t); // 10 kmph
echo $s->asMph(); // will print 16.093
adaptable
4 Things change - new business knowledge/rules,
changing technologies
4 small functions/classes/modules
4 throw away code
4 dependency injection
4 be abstract, use interfaces
NO CODE IS SIMPLER
THAN NO CODE
Simple enables change = Opportunity
4 do katas - readable, efficient, flexible
4 exercism.io, projecteuler.net
4 pair/mob program
4 code review
4 read others code bases
4 mentor at http://phpmentoring.org
4 attend/speak conferences & user groups
4 write a blog
Goodbye
@PeteJMcFarlane

More Related Content

PDF
Sorting arrays in PHP
PPTX
PHP Functions & Arrays
PPTX
Class 8 - Database Programming
PPT
Class 5 - PHP Strings
PPTX
PHP PPT FILE
PPT
Class 2 - Introduction to PHP
PDF
The Perl6 Type System
PPT
Class 4 - PHP Arrays
Sorting arrays in PHP
PHP Functions & Arrays
Class 8 - Database Programming
Class 5 - PHP Strings
PHP PPT FILE
Class 2 - Introduction to PHP
The Perl6 Type System
Class 4 - PHP Arrays

What's hot (17)

PPT
Functional Pe(a)rls version 2
PPTX
PDF
Improving Dev Assistant
PDF
Perl6 a whistle stop tour
PPTX
Perl6 a whistle stop tour
PDF
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
PDF
Is Haskell an acceptable Perl?
PDF
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
PDF
Introduction to Clean Code
PDF
PHP object calisthenics
PDF
tutorial7
PDF
Neatly folding-a-tree
PPT
php 2 Function creating, calling, PHP built-in function
PPT
PHP variables
PPTX
Crafting beautiful software
PPSX
Tuga IT 2017 - What's new in C# 7
PDF
Perl6 one-liners
Functional Pe(a)rls version 2
Improving Dev Assistant
Perl6 a whistle stop tour
Perl6 a whistle stop tour
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Is Haskell an acceptable Perl?
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
Introduction to Clean Code
PHP object calisthenics
tutorial7
Neatly folding-a-tree
php 2 Function creating, calling, PHP built-in function
PHP variables
Crafting beautiful software
Tuga IT 2017 - What's new in C# 7
Perl6 one-liners
Ad

Similar to How to write code you won't hate tomorrow (20)

PPTX
Php functions
KEY
Good Evils In Perl (Yapc Asia)
PPTX
Php & my sql
PPSX
What's New In C# 7
PDF
PHP and Rich Internet Applications
ODP
perl usage at database applications
KEY
Can't Miss Features of PHP 5.3 and 5.4
PPTX
Tidy Up Your Code
PDF
PHP and Rich Internet Applications
PDF
R57shell
PDF
PHP for Adults: Clean Code and Object Calisthenics
PDF
Why async and functional programming in PHP7 suck and how to get overr it?
PDF
Scripting3
PDF
PHPCon 2016: PHP7 by Witek Adamus / XSolve
KEY
Introduction à CoffeeScript pour ParisRB
PDF
Lithium: The Framework for People Who Hate Frameworks
DOC
Jsphp 110312161301-phpapp02
ZIP
Drupal Development (Part 2)
PDF
JavaScript for PHP developers
PPTX
Building a horizontally scalable API in php
Php functions
Good Evils In Perl (Yapc Asia)
Php & my sql
What's New In C# 7
PHP and Rich Internet Applications
perl usage at database applications
Can't Miss Features of PHP 5.3 and 5.4
Tidy Up Your Code
PHP and Rich Internet Applications
R57shell
PHP for Adults: Clean Code and Object Calisthenics
Why async and functional programming in PHP7 suck and how to get overr it?
Scripting3
PHPCon 2016: PHP7 by Witek Adamus / XSolve
Introduction à CoffeeScript pour ParisRB
Lithium: The Framework for People Who Hate Frameworks
Jsphp 110312161301-phpapp02
Drupal Development (Part 2)
JavaScript for PHP developers
Building a horizontally scalable API in php
Ad

Recently uploaded (20)

PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
medical staffing services at VALiNTRY
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Transform Your Business with a Software ERP System
PPTX
Essential Infomation Tech presentation.pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Nekopoi APK 2025 free lastest update
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Design an Analysis of Algorithms I-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PTS Company Brochure 2025 (1).pdf.......
Softaken Excel to vCard Converter Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
top salesforce developer skills in 2025.pdf
medical staffing services at VALiNTRY
2025 Textile ERP Trends: SAP, Odoo & Oracle
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Transform Your Business with a Software ERP System
Essential Infomation Tech presentation.pptx
ai tools demonstartion for schools and inter college
Nekopoi APK 2025 free lastest update
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Choose the Right IT Partner for Your Business in Malaysia
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

How to write code you won't hate tomorrow