Code Smells

Preview:

Citation preview

CodeSmells:Ooh,Ooh,ThatSmell!AmandeepBatra

October2016

Agenda

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

InterestingBayAreaConversations

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

WhatareCodeSmells?

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

Whydotheyoccur?

ØConsciously:whenwedeferØUnconsciously:whenwedon’teven

Whatistheimpact?

LostIntent

LostIntentExample

InefficientNaming

InefficientNaming

InaGame,weneedtowriteafunctionondirectionthesoldierisfacing

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

DuplicatedCode

• DRY:Don’tRepeatYourself

DuplicatedCode

• DRY:Don’tRepeatYourself

DeodorantComment

ØIfyouwriteintentionrevealingcodethencommentsareunnecessary

LongMethod

ØLikeaverylargeburger,toohardtodigest

Shortercomposedmethodsare- MoreReadable- Providegreateropportunityforcodesharing,lessduplication- Easiertotest

LargeClass

ØLikepeople,classessufferwhentheytaketoomanyresponsibilities

ØAllofuswoulddisposeofapetgoldfishifitmetanunhappyend.Yethowmanyofusdisposeofcodethatisnolongerbeingused?

ØLeadsto• IncreasedComplexity• AccidentalChanges• MoreDeadCode

DeadCode

OddballSolutionØWhenaproblemissolvedonewaythroughoutasystemandsameissolvedanotherwayinsamesystem

OddballSolution

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

ØCodethatsuffersfromthissmellfailstoleveragehigherlevelabstractions.

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

Insteadof

if(someString.contains("substring"))

SpeculativeGenerality

ØOh,Ithinkwewillneedtheabilitytodothatsomeday"andthuswantallsortsofhooksandspecialcasestohandlethingsthataren'trequired

CombinatorialExplosion

ØWhennewcombinationsofdataorbehaviorfurtherbloatanalreadybloateddesign

CombinatorialExplosion

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

BlackSheep

ØAblacksheepisafamilymemberthatdoesn'tfitinsowell,itcouldbeasub-classoramethodinaclass

BlackSheep

FeatureEnvy

ØResponsibilitiesofperformingajobisinwrongplace

FeatureEnvy

RefusedBequest

ØSubclassesgetstoinheritthemethodsanddataoftheirparents.Butwhatiftheydon'twantorneedwhattheyaregiven?

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

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

OtherSmellsØLongParameterizedList:Themoretheparameters,difficultistoread,preferanObject

ØIndecentExposure:Bewareofclassesthatunnecessarilyexposetheirinternals.Aggressivelyrefactorclassestominimizetheirpublicsurface.

ExampleCodeØLet’sfindoutsomecodesmellshere

SomeTips

Ø“Alwayscodeasifpersonwhowillmaintainyourcodeisamaniacserialkillerwhoknowswhereyoulive”Ø “Alwaysleavethecodeinbetterstatethanitistoday”

ResourcesØRefactoringtoPatternsBookØCodingHorrorBlog

Q&A

Thanks!

Recommended