SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
Code	Smells:	Ooh,	Ooh,	That	Smell	!	
Amandeep	Batra
October	2016
Agenda
ØWhat	are	Code	Smells?
ØWhy	do	they	occur?
ØWhat	is	the	Impact?
ØCode	Smells
ØReal	World	Example	Code
Interesting	Bay	Area	Conversations
ØWe’d	better	not	touch	this	code.	Last	time	we	did…
ØI	thought	we	had	a	test	for	this...
ØIt	always	worked,	who	touched	it?
ØWhere	is	e-mail	about	this	issue?
ØAssigning	this	to	X	who	will	come	back	on	next	Thursday
ØJust	add	a	TODO	comment	
ØLets	put	a	Hack	for	now
ØLets	create	a	SIM	for	Refactoring	for	now	
ØLets	Deliver	Results	for	now
What	are	Code	Smells?
ØCode	Smells	are	anti-pattern	which	represents	hazards	in	the	code
ØIt	is	a	design	that	duplicates,	complicates	or	tightly	couples	code
ØAnother	similar	term	we	commonly	use	is	called	Technical	Debt
ØRecognizing	hazards	in	the	code	is	the	first	step	in	improving	code
Why	do	they	occur?
ØConsciously:	when	we	defer
ØUnconsciously:	when	we	don’t	even
What	is	the	impact?
Lost	Intent
Lost	Intent	Example
Inefficient	Naming
Inefficient	Naming
In	a	Game,	we	need	to	write	a	function	
on	direction	the	soldier	is	facing
getDrctn()	:	Shorteing a	vowel	is	bad
getDir()	:	Misleading
getDirection()	:	Right
Duplicated	Code
• DRY:	Don’t	Repeat	Yourself
Duplicated	Code
• DRY:	Don’t	Repeat	Yourself
Deodorant	Comment
ØIf	you	write	intention	revealing	code	then	comments	are	unnecessary
Long	Method
ØLike	a	very	large	burger,	too	hard	to	digest
Shorter	composed	methods	are
- More	Readable
- Provide	greater	opportunity	for	code	sharing,	less	duplication	
- Easier	to	test
Large	Class	
ØLike	people,	classes	suffer	when	they	take	too	many	responsibilities
ØAll	of	us	would	dispose	of	a	pet	goldfish	if	it	met	an	unhappy	end.	Yet	
how	many	of	us	dispose	of	code	that	is	no	longer	being	used?
ØLeads	to	
• Increased	Complexity
• Accidental	Changes
• More	Dead	Code
Dead	Code
Oddball	Solution
ØWhen	a	problem	is	solved	one	way	throughout	a	system	and	same	is	
solved	another	way	in	same	system
Oddball	Solution
Primitive	Obsession
ØCode	that	exhibits	primitive	obsession	tends	to	rely	on:
• primitives	(like	integers	or	strings)
• low-level	methods
• classes	that	don't	easily	solve	problems
ØCode	that	suffers	from	this	smell	fails	to	leverage	higher	level	
abstractions.
if	(someString.indexOf("substring")	!=	-1)
Instead	of	
if	(someString.contains("substring"))
Speculative	Generality
ØOh,	I	think	we	will	need	the	ability	to	do	that	someday"	and	thus	
want	all	sorts	of	hooks	and	special	cases	to	handle	things	that	aren't	
required
Combinatorial	Explosion
ØWhen	new	combinations	of	data	or	behavior	further	bloat	an	already	
bloated	design
Combinatorial	Explosion
ØList	readOnlySortedList =	new	ReadOnlyList(	new	SortedList(	new	
ArrayList(...)	)	);
Black	Sheep
ØA	black	sheep	is	a	family	member	that	doesn't	fit	in	so	well,	it	could	
be	a	sub-class	or	a	method	in	a	class
Black	Sheep
Feature	Envy
ØResponsibilities	of	performing	a	job	is	in	wrong	place
Feature	Envy
Refused	Bequest
ØSubclasses	gets	to	inherit	the	methods	and	data	of	their	parents.	But	what	if	
they	don't	want	or	need	what	they	are	given?
public abstract class AbstractCollection…	
public abstract void add(Object	element);
public class Map	extends AbstractCollection…	
//	Do	nothing	because	user	must	input	key	and	value
public void add(Object	element)	{	}
Other	Smells
ØLong	Parameterized	List	:	The	more	the	parameters,	difficult	is	to	
read,	prefer	an	Object
ØIndecent	Exposure	:	Beware	of	classes	that	unnecessarily	expose	
their	internals.	Aggressively	refactor	classes	to	minimize	their	public	
surface.
Example	Code
ØLet’s	find	out	some	code	smells	here
Some	Tips
Ø“Always	code	as	if	person	who	will	maintain	your	
code	is	a	maniac	serial	killer	who	knows	where	you	
live”
Ø “Always	leave	the	code	in	better	state	than	it	is	
today”
Resources
ØRefactoring	to	Patterns	Book
ØCoding	Horror	Blog
Q&A
Thanks	!

Contenu connexe

Similaire à Code Smells

Clean Code - Business Impact
Clean Code - Business ImpactClean Code - Business Impact
Clean Code - Business ImpactCathal King
 
Let's Make the PAIN Visible!
Let's Make the PAIN Visible!Let's Make the PAIN Visible!
Let's Make the PAIN Visible!Arty Starr
 
Reporting Metasystem Design and Penalization Strategy Best Practices (Present...
Reporting Metasystem Design and Penalization Strategy Best Practices (Present...Reporting Metasystem Design and Penalization Strategy Best Practices (Present...
Reporting Metasystem Design and Penalization Strategy Best Practices (Present...Intel® Software
 
Code quality as a built-in process
Code quality as a built-in processCode quality as a built-in process
Code quality as a built-in processElad Maimon
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsSteven Smith
 
The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022
The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022
The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022HostedbyConfluent
 
How To Do Kick-Ass Software Development, by Sven Peters
How To Do Kick-Ass Software Development, by Sven PetersHow To Do Kick-Ass Software Development, by Sven Peters
How To Do Kick-Ass Software Development, by Sven PetersZeroTurnaround
 
Big guns for small guys (reloaded)
Big guns for small guys (reloaded)Big guns for small guys (reloaded)
Big guns for small guys (reloaded)Jorge López-Lago
 
How to tell a better story (in code)(final)
How to tell a better story (in code)(final)How to tell a better story (in code)(final)
How to tell a better story (in code)(final)Bonnie Pan
 
Rethinking enterprise software - Codemotion 2014
Rethinking enterprise software - Codemotion 2014Rethinking enterprise software - Codemotion 2014
Rethinking enterprise software - Codemotion 2014Alberto Brandolini
 
Rethinking Enterprise Software - Brandolini
Rethinking Enterprise Software - BrandoliniRethinking Enterprise Software - Brandolini
Rethinking Enterprise Software - BrandoliniCodemotion
 
Software as a craft (February, 2018)
Software as a craft (February, 2018)Software as a craft (February, 2018)
Software as a craft (February, 2018)Rachel M. Carmena
 
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkTaming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkJoseph Yoder
 
IT transformation in the world of etsy
IT transformation in the world of etsyIT transformation in the world of etsy
IT transformation in the world of etsyPaul van der Marck
 

Similaire à Code Smells (20)

CLEAN WEB
CLEAN WEBCLEAN WEB
CLEAN WEB
 
Clean Code - Business Impact
Clean Code - Business ImpactClean Code - Business Impact
Clean Code - Business Impact
 
Let's Make the PAIN Visible!
Let's Make the PAIN Visible!Let's Make the PAIN Visible!
Let's Make the PAIN Visible!
 
Reporting Metasystem Design and Penalization Strategy Best Practices (Present...
Reporting Metasystem Design and Penalization Strategy Best Practices (Present...Reporting Metasystem Design and Penalization Strategy Best Practices (Present...
Reporting Metasystem Design and Penalization Strategy Best Practices (Present...
 
Software development
Software developmentSoftware development
Software development
 
Code quality as a built-in process
Code quality as a built-in processCode quality as a built-in process
Code quality as a built-in process
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 
The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022
The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022
The Shitposting AI With Thomas Endres & Jonas Mayer | Current 2022
 
Why #OpenDX?
Why #OpenDX?Why #OpenDX?
Why #OpenDX?
 
How To Do Kick-Ass Software Development, by Sven Peters
How To Do Kick-Ass Software Development, by Sven PetersHow To Do Kick-Ass Software Development, by Sven Peters
How To Do Kick-Ass Software Development, by Sven Peters
 
Big guns for small guys (reloaded)
Big guns for small guys (reloaded)Big guns for small guys (reloaded)
Big guns for small guys (reloaded)
 
How to tell a better story (in code)(final)
How to tell a better story (in code)(final)How to tell a better story (in code)(final)
How to tell a better story (in code)(final)
 
Rethinking enterprise software - Codemotion 2014
Rethinking enterprise software - Codemotion 2014Rethinking enterprise software - Codemotion 2014
Rethinking enterprise software - Codemotion 2014
 
Rethinking Enterprise Software - Brandolini
Rethinking Enterprise Software - BrandoliniRethinking Enterprise Software - Brandolini
Rethinking Enterprise Software - Brandolini
 
Tdd in practice
Tdd in practiceTdd in practice
Tdd in practice
 
Clean Code Pt I
Clean Code Pt IClean Code Pt I
Clean Code Pt I
 
From keyboard to production
From keyboard to productionFrom keyboard to production
From keyboard to production
 
Software as a craft (February, 2018)
Software as a craft (February, 2018)Software as a craft (February, 2018)
Software as a craft (February, 2018)
 
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkTaming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
 
IT transformation in the world of etsy
IT transformation in the world of etsyIT transformation in the world of etsy
IT transformation in the world of etsy
 

Code Smells