资源描述:
《writing_idiomatic_python_3》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、WritingIdiomaticPythonJeffKnupp2013iiiCopyright2013byJeffKnuppAllrightsreserved.Nopartofthisbookmaybereproducedinanyformorbyanyelectronicormechanicalmeanswithoutpermissioninwritingfromtheauthor.JeffKnuppVisitmeatwww.jeffknupp.comPrefaceThere’safamousoldquoteaboutwritingmaintainablesoftw
2、are:Alwayscodeasiftheguywhoendsupmaintainingyourcodewillbeaviolentpsychopathwhoknowswhereyoulive.--JohnWoodscomp.lang.c++WhileI’mnotusuallyoneforaphorisms,thisonestrikesachordwithme.Maybeit’sbecauseI’vespentmyprofessionalcareerwritingsoftwareathugecompanies,butIhaveyettoinheritcodet
3、hatdidn’teventuallycausemetocursetheoriginalauthoratsomepoint.Everyone(besidesyou,ofcourse,dearreader)strugglestowritecodethat’seasytomaintain.WhenPythonbecamepopular,manythoughtthat,becauseofitsterseness,itwouldnaturallyleadtomoremaintainablesoftware.Alas,maintainabilityisnotanemer
4、gentpropertyofusinganexpressivelan-guage.BadlywrittenPythoncodeisjustasunmaintainableasbadlywrittenC++,Perl,Javaandalltherestofthelanguagesknownfortheir,ahem,read-ability.Tersecodeisnotafreelunch.Sowhatdowedo?Resignourselvestomaintainingcodewecan’tunderstand?RantonTwitterandTheDaily
5、WTFabouttheawfulcodewehavetoworkon?Whatmustwedotostopthepain?Write.Idiomatic.Code.It’sthatsimple.Idiomsinaprogramminglanguageareasortoflinguafrancatoletfuturereadersknowexactlywhatwe’retryingtoaccomplish.Wemaydocumentourcodeextensively,writeexhaustiveunittests,andholdcodereviewsthre
6、etimesaday,butthefactremains:whensomeoneelseneedstomakechanges,thecodeisking.Ifthatsomeoneisyou,allthedocumentationintheworldwon’thelpyouunderstandunreadablecode.Afterall,howcanyouevenbesurethecodeisdoingwhatthedocumentationsays?We’reusuallyreadingsomeoneelse’scodebecausethere’sapro
7、blem.Butidiomaticcodehelpshere,too.Evenifit’swrong,whencodeiswrittenidiomat-iiiivPREFACEically,it’sfareasierspotbugs.Idiomaticcodereducesthecognitiveloadonthereader.Afterlearningalanguage’sidioms,you’llspendlesstimewondering“Wait,whyaretheyusinganamedtuplethere”andmoretimeunderstand
8、ingwhatthecodeactua