Andsoyoumaythink, Okay, thisis a purefunction, right?
Becausewe'reonlytakingtheinputstocreatetheoutput.
Butagain, theproblemisthatthisactuallyhas a sideeffect.
Itischangingtheinput A Here, Asyouremember, wepassedinarrayandweaddthatfourelementtoitandactuallychangedourglobalarrayvariablewith a purefunction, youneverwanttochangetheinputsthatyou'readdingintotheelement.
Soherewehaveourinputsofourfunction.
Wecanneverchangetheminappearfunctionbecausethat's a sideeffect.
Nowingeneral, it's prettydifficulttomake a function, actuallydosomethingdifferentwhenyoucallitwiththesameinputsifyouaren't actuallydoinganythingwithstuffoutsideoftheinputs.
So, forexample, inherewe'reonlyusingtheinputs, soit's hardtomakeitactuallydosomethingdifferentbecausewe'realwaysusingjusttheinputsandnothingoutsideofitbut a functionsuchasmathdotrandom.
You'realwaysgoingtobegetting a randomnumbereverytimeyoucallthis.
Sothisis a functionthateventhoughit's usingthesameexactinputs, it's actuallyreturning a differentoutputtouseachtime.
So, forexample, insideherewealsoupendedmathatrandomtotheendofthis.
Soeventhoughwe'reusingonlytheinputswegetandwe'renotaffectinganythingoutsideofourfunction, sinceourfunctionactuallygives a differentresulteverysingletimewecallitwiththesameinputs.
Butthereasonthatthisis a detrimentisbecauseitmakesitveryhardtodothingssuchascalling a database, becausecalling a databaseandchangingsomeoftherecordsischangingsomethingoutsidethatfunctionsoappearfunctioncanaccessthedatabase.
But I highlyrecommendmakingasmuchofyourcodeispossible, purefunctionsjustbecausehowdead, simpletheretouseandhoweasyitmakesyourcoattotestonLeemakethesmallthingsthatactuallyneedtointeractwithotherthingsoutsidethefunction.
I promiseyou, onceyoustartgettingintothehabitofdoingthat, it's goingtomakeyourlifesomucheasierbecause I can't tellyouhowmanytimes I wrotecodethatwasanimpurefunction.
Itdidsomanythingsinsideofit.
Andthen I needtodochangeonethinginmyapplication, And I hadtogothroughandchange a bunchofdifferentfunctionsbecause I didn't knowifsomethingwasgoingtobreakbecause I hadallthesesideeffectsallovertheplace.
Itwasjustanabsolutemess.
I'verunintothatsomanydifferenttimes, andit's somucheasierwhen I knowif I needtochange, appearfunction, I couldjustchangeit.