Clean Code
What is Bad Code and Good code
Hello world!
I AM JEREMIAH CABALLERO
● I am Web Development Team Lead
in Sun*Philippines
● Advocate of Clean Architecture
and Clean Code
● Active member of Laravel
Cebu,ReactJS Cebu, Javascript
Cebu and Vue.js Cebu.
● Have my own community which is
Cod_ex.
2
Hello world!
I AM JEREMIAH CABALLERO
● You can follow me on:
Medium: @jeremiah.caballero.jc
Instagram: @caballerojeremiah
Twitter: @jkablyero
3
4
Outline
To what we will discuss
● Clean Code
● Meaningful Names
● Functions
● Comments
5
Master programmers think of
systems as stories to be told
rather than programs to be
written
-Robert C. Martin
6
1
CLEAN CODE
What and Why is Clean
Code?
7
BAD CODE
Bad Code are hard to read,
maintain and test
8
ART OF CLEAN CODE
Consider Code as a Piece of Art.
9
WHAT IS CLEAN CODE
To Write code with Elegance
12
Dictionary says
El·e·gance /ˈeləɡəns/
noun
1. the quality of being graceful and
stylish in appearance or
manner; style.
13
2
MEANINGFUL NAMES
Names are everywhere in
software.
14
USE INTENTION-REVEALING
NAMES
It is easy to say that names
should reveal intent.
15
//store last name and
first name
$x;
$y;
$firstname;
$lastname;
$x = [0,1,2,3];
function getThem () {
foreach($x as $num) {
return num;
};
}
What does it mean?
● What does x mean?
● What does it get?
● What is num?
19
$numberSequence = [0,1,2,3];
function getEachNumberFromSeq() {
foreach($numberSequence as $number) {
echo $number;
}
}
USE PRONOUNCEABLE NAMES
Just like words code is
pronounceable.
21
class DtaRcrd102 {
private $genymdhms;
private $modymdhms;
private $pszqint = ”102”;
}
class Customer {
private $generationTimestamp;
private $modificationTimestamp;
private $recordId = ”102”;
}
USE SEARCHABLE NAMES
Name your variable or function
that can be search easily
24
for (int j=0; j<34; j++) {
s += (t[j]*4)/5;
}
//constant variable
define(“WORK_DAYS_PER_WEEK”, 5);
$realDaysPerIdealDay = 4;
$sum = 0;
for ($j=0; $j < NUMBER_OF_TASKS; $j++) {
$realTaskDays = $taskEstimate[j] * $realDaysPerIdealDay;
$realTaskWeeks = ($realdays / WORK_DAYS_PER_WEEK);
//code here
}
METHOD NAMES
Methods should have verb or
verb phrase
27
Verb Methods
get();
save();
deletePost();
28
function getList() {
//code here
}
3
Functions
Name that function
30
SMALL!
The first rule of functions is
that they should be small.
31
public static renderPageWithSetupsAndTeardowns(
PageData $pageData, $isSuite
) throws Exception {
$isTestPage = $pageData.hasAttribute("Test");
if ($isTestPage) {
$testPage = pageData.getWikiPage();
$newPageContent = new StringBuffer();
includeSetupPages(testPage, newPageContent, isSuite);
newPageContent.append(pageData.getContent());
includeTeardownPages(testPage, newPageContent, isSuite);
$pageData.setContent(newPageContent.toString());
}
return $pageData.getHtml();
}
public static renderPageWith(PageData $pageData, $isSuite)
throws Exception {
if (isTestPage($pageData))
includeSetupAndTeardownPages($pageData, $isSuite);
return $pageData.getHtml();
}
DO ONE THING
A function must do one thing
34
public static renderPageWithSetupsAndTeardowns(
PageData $pageData, $isSuite
) throws Exception {
$isTestPage = $pageData.hasAttribute("Test");
if ($isTestPage) {
$testPage = pageData.getWikiPage();
$newPageContent = new StringBuffer();
includeSetupPages(testPage, newPageContent, isSuite);
newPageContent.append(pageData.getContent());
includeTeardownPages(testPage, newPageContent, isSuite);
$pageData.setContent(newPageContent.toString());
}
return $pageData.getHtml();
}
DO ONE THING
1. Determining whether the page is a test
page.
2. If so, including setups and teardowns.
3. Rendering the page in HTML.
36
public static renderPageWith(PageData $pageData, $isSuite)
throws Exception {
if (isTestPage($pageData))
includeSetupAndTeardownPages($pageData, $isSuite);
return $pageData.getHtml();
}
4
Comments
Explain yourself
38
GOOD COMMENTS
Some comments are necessary or
beneficial.
39
LEGAL COMMENTS
40
// Copyright (C) 2003,2004,2005 by Object
Mentor, Inc. All rights reserved.
// Released under the terms of the GNU
General Public License version 2 or later.
INFORMATIVE COMMENTS
41
// Returns an instance of the
Responder being tested.
protected abstract Responder
responderInstance();
WARNING OF CONSEQUENCES
42
// Don't run unless you
// have some time to kill.
public function testWithReallyBigFile()
{
writeLinesToFile(10000000);
$response.setBody(testFile);
$response.readyToSend(this);
$responseString = $output.toString();
assertSubString("Content-Length: 1000000000",
$responseString);
assertTrue($bytesSent > 1000000000);
}
BAD COMMENTS
If there is good comments there
is bad comments also.
43
REDUNDANT COMMENTS
44
// Utility method that returns when this.closed is true. Throws
an exception
// if the timeout is reached.
public waitForClose(RtimeoutMillis)
{
if(!$closed)
{
wait($timeoutMillis);
if(!$closed)
throw new Exception("MockResponseSender could not be
closed");
}
}
NOISE COMMENTS
45
/** The day of the month. */
private $dayOfMonth;
NOISE COMMENTS
46
/** The day of the month. */
private $dayOfMonth;
SCARY NOISE COMMENTS
47
/** The name. */
private $name;
/** The version. */
private $version;
/** The licenceName. */
private $licenceName;
/** The version. */
private $info;
LET’S REVIEW SOME POINTERS
Clean Code
What is clean code and the
purpose why it is written.
MEANINGFUL NAMES
How to write meaningful
names in our code.
FUNCTIONS
How to write functions and
how write functions more
shorter
COMMENTS
How to write comments that
explains the program’s
intent and know how to
identify bad comments.
48
Master programmers think of
systems as stories to be told
rather than programs to be
written
-Robert C. Martin
49
Thanks!
ANY QUESTIONS?
50
51
SlidesCarnival icons are editable
shapes.
This means that you can:
● Resize them without losing
quality.
● Change fill color and
opacity.
Isn’t that nice? :)
Examples:
52
Now you can use any emoji as an icon!
And of course it resizes without losing quality and you can
change the color.
How? Follow Google instructions
https://twitter.com/googledocs/status/730087240156643328
✋👆👉👍👤👦👧👨👩👪💃🏃
💑❤😂😉😋😒😭👶😸🐟🍒🍔
💣📌📖🔨🎃🎈🎨🏈🏰🌏🔌🔑
and many more...
��
53

Jeremiah Caballero - Introduction of Clean Code