SlideShare a Scribd company logo
Antonio	Perić-Mažar	
29.10.2016	-		#sfcampua
Maintainable	+	Extensible	=	
Clean	...	yes,	Code!
@antonioperic
About	me
• Antonio	Perić-Mažar,	

mag.	ing.	comp.	
• CEO,	Co-Founder	@	Locastic	
• Co-Founder	@	Shift	Conference	
• Software	developer,	Symfony2	
• Open	Source	Contributor		
• SFUGCRO

• w:	www.locastic.com	
• m:	antonio@locastic.com	
• t:	@antonioperic
@antonioperic
Locastic
• We	help	clients	create	amazing	web	and	mobile	apps	(since	2011)	
• design	and	development	agency	
• mobile	development	
• web	development	
• UX/UI	
• Training	and	Consulting	
• Shift	Conference,	Symfony	Croatia	
• www.locastic.com	
• t:	@locastic
@antonioperic
Locastic
@antonioperic
Questions?
• Who	thinks	he	is	producing	good	code?	
• What	is	bad	code?	
• What	is	good	code?	
• Do	you	know	how	to	measure	code	quality?
@antonioperic
@antonioperic
Note:	when	we	asked	for	this	code	
we	got	it	in	zip	file!!!	No	VCS	at	all!
@antonioperic
@antonioperic
@antonioperic
@antonioperic
@antonioperic
@antonioperic
This	was	one	huge	WTFn	project!
@antonioperic
@antonioperic
Code	Smell!!!	(Bad	Code)
• Use	globals	
• Anything	hardcoded	(url,	ip,	etc)	
• Long	classes	or	methods	
• Deep	structures	
• Long	and	bad	names	
• Lack	of	formatting		
• Lack	of	standard
@antonioperic
Code	Smell!!!	(Bad	Code)
• Overuse	of	public	static	
• Too	many	parameters	
• Weird	order	of	parameters		
• GOD	Object	(he	can	do	everything	and	he	
knows	everything)	
• No	input	validation	
• No	comments
@antonioperic
Code	Smell!!!	(Bad	Code)
• Takes	1-2	sentences	to	explain	
• Copy-pasted	-	repetitive	code	
• Never	used	inheritance,	interface	or	
composition	
• not	readable	
• not	testable
@antonioperic
Code	Smell!!!	(Bad	Code)
• hard	to	understand	
• hard	to	maintain		
• gives	a	headache	
• you	don’t	won’t	to	work	on/with	it
@antonioperic
@antonioperic
Avoid	being	STUPID
Singleton	
Tight	coupling	
Untestability		
Premature	optimization	
Indescriptive	naming	
Duplication
@antonioperic
Avoid	being	STUPID
Singleton	
Tight	coupling	
Untestability		
Premature	optimization	
Indescriptive	naming	
Duplication
@antonioperic
@antonioperic
But	why	we	still	doing	it…
• We	are	lazy	
• Short	deadlines	
• Bad	decisions	
• Fast	-	good	-	cheap	
• We	are	always	the	best	developer	ever	:)
@antonioperic
Code	has	expiration	date!
@antonioperic
Yesterday!
@antonioperic
Good	developers	are	shipping!
balance shipping vs good code
@antonioperic
Developers	are	evolving!
@antonioperic
@antonioperic
Language	is	evolving
• Frameworks	(Symfony	<3)	
• Package	managers	(composer)	
• Standards	
• PHP7
@antonioperic
“Any	fool	can	write	code	that	a	computer	can	
understand.	Good	programmers	write	code	that	
humans	can	understand.“
Martin Fowler
@antonioperic
CODE
@antonioperic
@antonioperic
Clean	code
• Code	that	doesn’t	exist	
• KISS	
• DRY	
• SOLID	
• Don’t	reinvent	the	wheel	
• Design	Patterns	
• Testable	->	Tested
@antonioperic
Clean	code
• Flexible	
• Maintainable	
• Easy	to	refactor	
• Readable
@antonioperic
Clean	code	is	easy	to	understand,	
easy	to	change	and	reusable.
@antonioperic
@antonioperic
Why	we	should	care…
• Someone	else	will	use	that	code	
• You	will	use	that	code	
• Bad	code	does	a	lot	of	things	but	none	well	
• You	don’t	write	code	just	to	be	there	
• Maintaining	should	be	easy	
• Good	code	save	money	and	time
@antonioperic
@antonioperic
If	technical	debt	is	not	repaid,	it	can	accumulate	
'interest',	making	it	harder	to	implement	
changes	later	on.
@antonioperic
@antonioperic
Guidance	for	better	(clean)	code
@antonioperic
#1	Naming	matters
• Don’t	abbreviate	
• $string->b	=	false;	
• $string->bold	=	false;	
• Make	code	readable	
• Meaningful	names		
• Improves	readability	
• Good	code	should	be	talking	to	you	
• You	should	read	it	like	a	book,	whitespace	is	important
@antonioperic
#1	Naming	matters
• Two	world	words	variable	name	
• $data,	$data2	
• $total,	$total2,	$total3
@antonioperic
#1	Naming	matters
@antonioperic
#1	Naming	matters
• Don’t	be	silly!
@antonioperic
#1	Naming	matters
• Good	name	should	explain	
• why	it	exist	
• what	it	does	
• how	to	use	it	
• If	you	need	comment	for	explain	it,	it	is	bad	
name	
• Class	should	use	a	noun,	method	a	verb
@antonioperic
#2	Code	standard
• Don’t	mix	styles	in	project,	use	whitespace	
• Easier	to	navigate	inside	project	
• Less	time	for	boarding	new	developer	
• PHP-FIG	
• Stick	with	PSR		
• PSR-0	autoloading	
• PSR-1	basing	code	style	
• PSR-2	detailed	coding	style
@antonioperic
#2	Code	standard
• Symfony	follows	the	standards	defined	in	the	
PSR-0,	PSR-1,	PSR-2	and	PSR-4	documents.	
• http://symfony.com/doc/current/contributing/
code/standards.html
@antonioperic
#3	Use	only	one	level	of	indentation	
per	method
• {{{	}}}	try	to	avoid	this	
• force	single	responsibility	
• small,	simple,	easier	to	understand	
• our	mind	model	is	important	
• separate	logic	blocks	and	complex	level	of	
indentation	into	methods
@antonioperic
#4	Don’t	use	else
• reduce	code	complexity	
• early	returns	
• use	Exceptions	
• it	reduce	complex	of	our	mind	model	
• method	should	not	do	more	then	one	thing	
• don’t	use	intermediate	variable	
• use	polymorphism,	switch	is	signal	for	polymorphism	
• don’t	abuse	polymorphism
@antonioperic
#4	Don’t	use	else
@antonioperic
#5	Keep	your	classes/methods	
small
• easier	to	test	
• do	one	thing,	and	do	one	thing	well	
• all	functions	should	return	something	and	
avoid	returning	NULL	
• keep	methods	small	(max.	20	lines	of	code)	
• avoid	side-effects	
• avoid	global	state
@antonioperic
#6	Use	Exceptions
• Use	them	
• Make	sure	to	catch	them	
• Specify	the	nature	of	the	error	
• throw	new	InvalidArgumentException();	
• Don’t	use	special	error	codes
@antonioperic
#7	Design	patterns
• use	them,	they	give	you	solution	for	some	well	
known	problems
@antonioperic
#8	Use	SPL
• 	The	Standard	PHP	Library	(SPL)	is	a	collection	of	
interfaces	and	classes	that	are	meant	to	solve	
common	problems.		
• SPL	provides	a	set	of	standard	datastructure,	a	
set	of	iterators	to	traverse	over	objects,	a	set	
of	interfaces,	a	set	of	standard	Exceptions,	a	
number	of	classes	to	work	with	files	and	it	
provides	a	set	of	functions	like	
spl_autoload_register()
@antonioperic
#8	Use	SPL
• Datastructures	
• SplDoublyLinkedList	—	The	SplDoublyLinkedList	class	
• SplStack	—	The	SplStack	class	
• SplQueue	—	The	SplQueue	class	
• SplHeap	—	The	SplHeap	class	
• SplMaxHeap	—	The	SplMaxHeap	class	
• SplMinHeap	—	The	SplMinHeap	class	
• SplPriorityQueue	—	The	SplPriorityQueue	class	
• SplFixedArray	—	The	SplFixedArray	class	
• SplObjectStorage	—	The	SplObjectStorage	class
@antonioperic
#8	Use	SPL
• Iterators	
• AppendIterator	—	The	AppendIterator	class	
• ArrayIterator	—	The	ArrayIterator	class	
• CachingIterator	—	The	CachingIterator	class	
• CallbackFilterIterator	—	The	CallbackFilterIterator	class	
• DirectoryIterator	—	The	DirectoryIterator	class	
• EmptyIterator	—	The	EmptyIterator	class	
• FilesystemIterator	—	The	FilesystemIterator	class	
• FilterIterator	—	The	FilterIterator	class	
• GlobIterator	—	The	GlobIterator	class	
• InfiniteIterator	—	The	InfiniteIterator	class	
• …
@antonioperic
#8	Use	SPL
• Interfaces	
• Countable	—	The	Countable	interface	
• OuterIterator	—	The	OuterIterator	interface	
• RecursiveIterator	—	The	RecursiveIterator	interface	
• SeekableIterator	—	The	SeekableIterator
@antonioperic
#8	Use	SPL
• Exceptions	
• 	BadFunctionCallException	—	The	BadFunctionCallException	class	
• 	BadMethodCallException	—	The	BadMethodCallException	class	
• 	DomainException	—	The	DomainException	class	
• 	InvalidArgumentException	—	The	InvalidArgumentException	class	
• LengthException	—	The	LengthException	class	
• LogicException	—	The	LogicException	class	
• OutOfBoundsException	—	The	OutOfBoundsException		
• OutOfRangeException	—	The	OutOfRangeException		
• OverflowException	—	The	OverflowException		
• RangeException	—	The	RangeException		
• RuntimeException	—	The	RuntimeException		
• 	UnderflowException	—	The	UnderflowException		
• UnexpectedValueException	—	The	UnexpectedValueException	class
@antonioperic
#9	Law	of	demeter	(LoD)
• Each	unit	should	have	only	limited	knowledge	
about	other	units:	only	units	"closely"	
related	to	the	current	unit.	
• Each	unit	should	only	talk	to	its	friends;	don't	
talk	to	strangers.	
• Only	talk	to	your	immediate	friends	
• Help	you	build	code	that	is	easier	to	test
@antonioperic
#9	Law	of	demeter
@antonioperic
#9	Law	of	Demeter
@antonioperic
#9	Law	of	demeter	(LoD)
• Benefits:	
• Keep	object	instant	ion	simple	
• Easier	to	mock	objects	
• Simplify	dependency	graph
@antonioperic
#10	Comments
• Good	comments	
• Bad	comments	
• PHPDoc	
• Don’t	use	comments	for	tracking	history	of	
changes
@antonioperic
#11	Use	tools
• PHPCS	
• PHP-CS-Fixer	
• pDepend	(performs	static	code	analysis)	
• PHPLOC	(A	tool	for	quickly	measuring	the	size	of	a	PHP	project)	
• SensioLabs	Insight	
• phpmd	(mess	detector)	
• PHPCPD	(copy	paste	detector)	
• PHPDCP	(dead	code	detector)	
• learn	more	about	your	IDE	(PHPStorm	is	amazing	tool)
@antonioperic
#12	improve	yourself
• read	a	lot	of	code	
• http://www.livecoding.tv/	
• work	in	pairs	
• do	open	source	-	you	will	get	a	lot	of	free	reviews	
from	amazing	developers	
• show	your	code	to	other	developers	
• go	to	conferences		
• be	responsive	for	your	code
@antonioperic
“I	live	and	breathe	my	code	and	my	code	is	
mostly	the	fun	stuff.	You	know	the	logic,	the	
stuff	that	makes	cool	applications,	right?.“
Misko Havery
@antonioperic
#11	Boy-scout	rule
@antonioperic
#12	Boy	scout	rule
• Always	check	a	module	in	cleaner	than	when	
you	checked	it	out.	
• Always	leave	code	in	better	shape	then	you	
found	it
@antonioperic
Conclusion
• Clean	code	is:	
• structured	
• simple	
• consistent	
• Use	boy	scout	rule	
• Do	small	steps	
• There	is	no	silver	bullet	only	hard	work
@antonioperic
References
• https://cleancoders.com/	
• Clean	Code:	A	Handbook	of	Agile	Software	Craftsmanship,	Robert	C.	
Martin	
• Dotting	Your	I’s	and	Crossing	Your	T7s,	Juliette	Reinders	Folmer,	IPC	
Sprint	2015	
• Your	code	sucks,	let's	fix	it	-	By	Rafael	Dohms		
• Clean	Code,	Tobias	Schlitt,	WebSummerCamp	2016	
• Google	Tech	Talks,	Clean	Code	
• https://www.youtube.com/watch?v=HZJxjlvBbVA,	Clean	Code,	
David	Donahue	
• Plenty	of	other	talks	at	Youtube
@antonioperic
Thank	you!
www.locastic.com
antonio@locastic.com
@antonioperic


More Related Content

PPT
Makiety uzytkownikow
PPTX
Lean UX vs Design Thinking (lang: PL)
PPT
Symfony2 and AngularJS
PDF
Measuring What Matters: A UX Approach to Metrics :: UX Days Tokyo [April 2015]
PDF
UX design, service design and design thinking
PDF
WUD na Pradze - Specjaliści UX w Polsce 2015
PDF
A recipe for effective leadership
PDF
Surviving in a Microservices Environment
Makiety uzytkownikow
Lean UX vs Design Thinking (lang: PL)
Symfony2 and AngularJS
Measuring What Matters: A UX Approach to Metrics :: UX Days Tokyo [April 2015]
UX design, service design and design thinking
WUD na Pradze - Specjaliści UX w Polsce 2015
A recipe for effective leadership
Surviving in a Microservices Environment

Similar to Maintainable + Extensible = Clean ... yes, Code! (20)

PDF
Lessons learned while developing with Sylius
PDF
How to do a podcast
KEY
Eating our own Dogfood - Twitter Mobile
PDF
Build your business on top of Open Source
PPTX
Global Legal Hackathon in London Introduction
PDF
Any Image To Ico Converter Free Tool
PPTX
UX Trends for Startups leveraging Voice & Text - Phonegap Meetup - 4YFN - MWC
PDF
Optimizing the Confluence User Experience
PDF
Reinventing npmjs.com
PDF
Sotware Engineering Adapting to the AI Revolution: Thriving in the Age of GenAI
PDF
process
PPTX
How pair programming can strengthen teams
PPTX
Voice Assistand Meena. Akash Chandan Hafiz Monojit (1).pptx
PPTX
Mobile App & Game Biz
PDF
My InfoSec journey led me to create my own IR tools, how, and why you should too
PPTX
SPSNL17 - Getting started with SharePoint development for the reluctant IT Pr...
PDF
Applications in Pharo
PDF
MongoDB World 2019: Look, Ma, No Servers! Serverless Application Development ...
PDF
design_process
PDF
design_process
Lessons learned while developing with Sylius
How to do a podcast
Eating our own Dogfood - Twitter Mobile
Build your business on top of Open Source
Global Legal Hackathon in London Introduction
Any Image To Ico Converter Free Tool
UX Trends for Startups leveraging Voice & Text - Phonegap Meetup - 4YFN - MWC
Optimizing the Confluence User Experience
Reinventing npmjs.com
Sotware Engineering Adapting to the AI Revolution: Thriving in the Age of GenAI
process
How pair programming can strengthen teams
Voice Assistand Meena. Akash Chandan Hafiz Monojit (1).pptx
Mobile App & Game Biz
My InfoSec journey led me to create my own IR tools, how, and why you should too
SPSNL17 - Getting started with SharePoint development for the reluctant IT Pr...
Applications in Pharo
MongoDB World 2019: Look, Ma, No Servers! Serverless Application Development ...
design_process
design_process
Ad

More from Antonio Peric-Mazar (20)

PDF
You call yourself a Senior Developer?
PDF
Using API Platform to build ticketing system #symfonycon
PDF
Using API platform to build ticketing system (translations, time zones, ...) ...
PDF
Are you failing at being agile? #digitallabin
PDF
Symfony 4: A new way to develop applications #ipc19
PDF
A year with progressive web apps! #webinale
PDF
The UI is the THE application #dpc19
PDF
Symfony 4: A new way to develop applications #phpsrb
PDF
REST easy with API Platform
PDF
A year with progressive web apps! #DevConMU
PDF
Service workers are your best friends
PDF
Progressive Web Apps are here!
PDF
Building APIs in an easy way using API Platform
PDF
Symfony4 - A new way of developing web applications
PDF
Building APIs in an easy way using API Platform
PDF
Drupal8 for Symfony developers - Dutch PHP
PDF
Drupal8 for Symfony Developers (PHP Day Verona 2017)
PDF
Drupal8 for Symfony Developers
PDF
Building real time applications with Symfony2
PPT
Building Single Page Application (SPA) with Symfony2 and AngularJS
You call yourself a Senior Developer?
Using API Platform to build ticketing system #symfonycon
Using API platform to build ticketing system (translations, time zones, ...) ...
Are you failing at being agile? #digitallabin
Symfony 4: A new way to develop applications #ipc19
A year with progressive web apps! #webinale
The UI is the THE application #dpc19
Symfony 4: A new way to develop applications #phpsrb
REST easy with API Platform
A year with progressive web apps! #DevConMU
Service workers are your best friends
Progressive Web Apps are here!
Building APIs in an easy way using API Platform
Symfony4 - A new way of developing web applications
Building APIs in an easy way using API Platform
Drupal8 for Symfony developers - Dutch PHP
Drupal8 for Symfony Developers (PHP Day Verona 2017)
Drupal8 for Symfony Developers
Building real time applications with Symfony2
Building Single Page Application (SPA) with Symfony2 and AngularJS
Ad

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
master seminar digital applications in india
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Basic Mud Logging Guide for educational purpose
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Structure & Organelles in detailed.
Renaissance Architecture: A Journey from Faith to Humanism
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
master seminar digital applications in india
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Basic Mud Logging Guide for educational purpose
Anesthesia in Laparoscopic Surgery in India
Abdominal Access Techniques with Prof. Dr. R K Mishra
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
TR - Agricultural Crops Production NC III.pdf
Complications of Minimal Access Surgery at WLH
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Structure & Organelles in detailed.

Maintainable + Extensible = Clean ... yes, Code!