Chapter 1
Refactoring: A First Example
HowdoIbegintotalkaboutrefactoring?Thetraditionalwayisbyintroducingthe
historyofthesubject,broadprinciples,andthelike.Whensomebodydoesthatata
conference,Igetslightlysleepy.Mymindstartswandering,withalowpriority
backgroundprocesspollingthespeakeruntiltheygiveanexample.
TheexampleswakemeupbecauseIcanseewhatisgoingon.Withprinciples,itistoo
easytomakebroadgeneralizations—andtoohardtofigureouthowtoapplythings.An
examplehelpsmakethingsclear.
SoI’mgoingtostartthisbookwithanexampleofrefactoring.I’lltalkabouthow
refactoringworksandwillgiveyouasenseoftherefactoringprocess.Icanthendothe
usualprinciplesstyleintroductioninthenextchapter.
Withanyintroductoryexample,however,Irunintoaproblem.IfIpickalarge
program,describingitandhowitisrefactoredistoocomplicatedforamortalreaderto
workthrough.(Itriedthiswiththeoriginalbook—andendedupthrowingawaytwo
examples,whichwerestillprettysmallbuttookoverahundredpageseachtodescribe.)
However,ifIpickaprogramthatissmallenoughtobecomprehensible,refactoring
doesnotlooklikeitisworthwhile.
I’mthusintheclassicbindofanyonewhowantstodescribetechniquesthatareuseful
forrealworldprograms.Frankly,itisnotworththeefforttodoalltherefactoringthat
I’mgoingtoshowyouonthesmallprogramIwillbeusing.ButifthecodeI’mshowing
youispartofalargersystem,thentherefactoringbecomesimportant.Justlookatmy
exampleandimagineitinthecontextofamuchlargersystem.
THE STARTING POINT
Inthefirsteditionofthisbook,mystartingprogramprintedabillfromavideorental
store,whichmaynowleadmanyofyoutoask:“What’savideorentalstore?”Rather
thananswerthatquestion,I’vereskinnedtheexampletosomethingthatisbotholder
Offers & Deals
Highlights
Settings
Support
Sign Out
评论0
最新资源