33
Code Smells: Ooh, Ooh, That Smell ! Amandeep Batra October 2016

Code Smells

Embed Size (px)

Citation preview

Page 1: Code Smells

CodeSmells:Ooh,Ooh,ThatSmell!AmandeepBatra

October2016

Page 2: Code Smells

Agenda

ØWhatareCodeSmells?ØWhydotheyoccur?ØWhatistheImpact?ØCodeSmellsØRealWorldExampleCode

Page 3: Code Smells

InterestingBayAreaConversations

ØWe’dbetternottouchthiscode.Lasttimewedid…ØIthoughtwehadatestforthis...ØItalwaysworked,whotouchedit?ØWhereise-mailaboutthisissue?ØAssigningthistoXwhowillcomebackonnextThursdayØJustaddaTODOcommentØLetsputaHackfornowØLetscreateaSIMforRefactoringfornowØLetsDeliverResultsfornow

Page 4: Code Smells

WhatareCodeSmells?

ØCodeSmellsareanti-patternwhichrepresentshazardsinthecodeØItisadesignthatduplicates,complicatesortightlycouplescodeØAnothersimilartermwecommonlyuseiscalledTechnicalDebtØRecognizinghazardsinthecodeisthefirststepinimprovingcode

Page 5: Code Smells

Whydotheyoccur?

ØConsciously:whenwedeferØUnconsciously:whenwedon’teven

Page 6: Code Smells

Whatistheimpact?

Page 7: Code Smells

LostIntent

Page 8: Code Smells

LostIntentExample

Page 9: Code Smells

InefficientNaming

Page 10: Code Smells

InefficientNaming

InaGame,weneedtowriteafunctionondirectionthesoldierisfacing

getDrctn():Shorteing avowelisbadgetDir():MisleadinggetDirection():Right

Page 11: Code Smells

DuplicatedCode

• DRY:Don’tRepeatYourself

Page 12: Code Smells

DuplicatedCode

• DRY:Don’tRepeatYourself

Page 13: Code Smells

DeodorantComment

ØIfyouwriteintentionrevealingcodethencommentsareunnecessary

Page 14: Code Smells

LongMethod

ØLikeaverylargeburger,toohardtodigest

Shortercomposedmethodsare- MoreReadable- Providegreateropportunityforcodesharing,lessduplication- Easiertotest

Page 15: Code Smells

LargeClass

ØLikepeople,classessufferwhentheytaketoomanyresponsibilities

Page 16: Code Smells

ØAllofuswoulddisposeofapetgoldfishifitmetanunhappyend.Yethowmanyofusdisposeofcodethatisnolongerbeingused?

ØLeadsto• IncreasedComplexity• AccidentalChanges• MoreDeadCode

DeadCode

Page 17: Code Smells

OddballSolutionØWhenaproblemissolvedonewaythroughoutasystemandsameissolvedanotherwayinsamesystem

Page 18: Code Smells

OddballSolution

Page 19: Code Smells

PrimitiveObsessionØCodethatexhibitsprimitiveobsessiontendstorelyon:• primitives(likeintegersorstrings)• low-levelmethods• classesthatdon'teasilysolveproblems

ØCodethatsuffersfromthissmellfailstoleveragehigherlevelabstractions.

if(someString.indexOf("substring")!=-1)

Insteadof

if(someString.contains("substring"))

Page 20: Code Smells

SpeculativeGenerality

ØOh,Ithinkwewillneedtheabilitytodothatsomeday"andthuswantallsortsofhooksandspecialcasestohandlethingsthataren'trequired

Page 21: Code Smells

CombinatorialExplosion

ØWhennewcombinationsofdataorbehaviorfurtherbloatanalreadybloateddesign

Page 22: Code Smells

CombinatorialExplosion

ØListreadOnlySortedList =newReadOnlyList(newSortedList(newArrayList(...)));

Page 23: Code Smells

BlackSheep

ØAblacksheepisafamilymemberthatdoesn'tfitinsowell,itcouldbeasub-classoramethodinaclass

Page 24: Code Smells

BlackSheep

Page 25: Code Smells

FeatureEnvy

ØResponsibilitiesofperformingajobisinwrongplace

Page 26: Code Smells

FeatureEnvy

Page 27: Code Smells

RefusedBequest

ØSubclassesgetstoinheritthemethodsanddataoftheirparents.Butwhatiftheydon'twantorneedwhattheyaregiven?

public abstract class AbstractCollection…public abstract void add(Objectelement);

public class Mapextends AbstractCollection…//Donothingbecauseusermustinputkeyandvaluepublic void add(Objectelement){}

Page 28: Code Smells

OtherSmellsØLongParameterizedList:Themoretheparameters,difficultistoread,preferanObject

ØIndecentExposure:Bewareofclassesthatunnecessarilyexposetheirinternals.Aggressivelyrefactorclassestominimizetheirpublicsurface.

Page 29: Code Smells

ExampleCodeØLet’sfindoutsomecodesmellshere

Page 30: Code Smells

SomeTips

Ø“Alwayscodeasifpersonwhowillmaintainyourcodeisamaniacserialkillerwhoknowswhereyoulive”Ø “Alwaysleavethecodeinbetterstatethanitistoday”

Page 31: Code Smells

ResourcesØRefactoringtoPatternsBookØCodingHorrorBlog

Page 32: Code Smells

Q&A

Page 33: Code Smells

Thanks!