您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 综合/其它 > Data Structures And Program Design In C++
Section1.3ProgrammingStyle11(a)(d)(g)(j)(b)(e)(h)(k)(c)(i)(l)(f)Figure1.1.SimpleLifeconfigurationsanimportantmoraltocomputerprogramming:Evenifdataandalgorithmsexist8before,itisonlywhentheyaregivenmeaningfulnamesthattheirplacesintheprogramcanbeproperlyrecognizedandappreciated,thattheyfirstacquirealifeoftheirown.purposeofcarefulnamingForaprogramtoworkproperlyitisoftheutmostimportancetoknowexactlywhateachclassandvariablerepresentsandtoknowexactlywhateachfunctiondoes.Documentationexplainingtheclasses,variables,andfunctionsshouldthere-forealwaysbeincluded.Thenamesofclasses,variables,andfunctionsshouldbechosenwithcaresoastoidentifytheirmeaningsclearlyandsuccinctly.Findinggoodnamesisnotalwaysaneasytask,butisimportantenoughtobesingledoutasoursecondprogrammingprecept:9ProgrammingPreceptAlwaysnameyourclasses,variablesandfunctionswiththegreatestcare,andexplainthemthoroughly.C++goessomedistancetowardenforcingthispreceptbyrequiringthedeclarationofvariablesandallowsusalmostunlimitedfreedominthechoiceofidentifying12Chapter1ProgrammingPrinciplesnames.Constantsusedindifferentplacesshouldbegivennames,andsoshoulddifferentdatatypes,sothatthecompilercancatcherrorsthatmightotherwisebedifficulttospot.WeshallseethattypesandclassesplayafundamentalroleinC++programs,anditisparticularlyimportantthattheyshouldstandouttoareaderofourpro-grams.Weshallthereforeadoptacapitalizationconvention,whichwehavealreadyusedintheLifeprogram:Weuseaninitialcapitalletterintheidentifierofanyclassorprogrammerdefinedtype.Incontrast,weshalluseonlylowercaselettersfortheidentifiersoffunctions,variables,andconstants.Thecarefulchoiceofnamescangoalongwayinclarifyingaprogramandinhelpingtoavoidmisprintsandcommonerrors.Someguidelinesare1.Givespecialcaretothechoiceofnamesforclasses,functions,constants,andguidelinesallglobalvariablesusedindifferentpartsoftheprogram.Thesenamesshouldbemeaningfulandshouldsuggestclearlythepurposeoftheclass,function,variable,andthelike.2.Keepthenamessimpleforvariablesusedonlybrieflyandlocally.Mathemati-ciansusuallyuseasinglelettertostandforavariable,andsometimes,whenwritingmathematicalprograms,itmaybepermissibletouseasingle-letternameforamathematicalvariable.However,evenforthevariablecontrollingaforloop,itisoftenpossibletofindashortbutmeaningfulwordthatbetterdescribestheuseofthevariable.3.Usecommonprefixesorsuffixestoassociatenamesofthesamegeneralcate-gory.Thefilesusedinaprogram,forexample,mightbecalledinput_filetransaction_filetotal_fileout_filereject_file4.Avoiddeliberatemisspellingsandmeaninglesssuffixestoobtaindifferentnames.Ofallthenamesindexindxndexindexxindex2index3onlyone(thefirst)shouldnormallybeused.Whenyouaretemptedtointro-ducemultiplenamesofthissort,takeitasasignthatyoushouldthinkharderanddevisenamesthatbetterdescribetheintendeduse.5.Avoidchoosingcutenameswhosemeaninghaslittleornothingtodowiththeproblem.Thestatementsdo{study();}while(TV.in_hock());if(!sleepy)play();elsenap();maybefunnybuttheyarebadprogramming!Section1.3ProgrammingStyle136.Avoidchoosingnamesthatareclosetoeachotherinspellingorotherwiseeasytoconfuse.7.Becarefulintheuseoftheletter“l”(smallell),“O”(capitaloh),and“0”(zero).Withinwordsornumberstheseusuallycanberecognizedfromthecontextandcausenoproblem,but“l”and“O”shouldneverbeusedaloneasnames.Considertheexamplesl=1;x=1;x=l;x=O;O=01.3.2DocumentationandFormatMoststudentsinitiallyregarddocumentationasachorethatmustbeenduredafteraprogramisfinished,toensurethatthemarkerandinstructorcanreadit,sothatnocreditwillbelostforobscurity.Theauthorofasmallprogramindeedcankeepallthedetailsinmind,andsoneedsdocumentationonlytoexplaintheprogramtosomeoneelse.Withlargeprograms(andwithsmallonesaftersomethepurposeofdocumentationmonthshaveelapsed),itbecomesimpossibletorememberhoweverydetailrelatestoeveryother,andthereforetowritelargeprograms,itisessentialthatappropriatedocumentationbepreparedalongwitheachsmallpartoftheprogram.Agoodhabitistopreparedocumentationastheprogramisbeingwritten,andanevenbetterone,asweshallseelater,istopreparepartofthedocumentationbeforestartingtowritetheprogram.Notalldocumentationisappropriate.Almostascommonasprogramswithlittledocumentationoronlycrypticcommentsareprogramswithverbosedocu-mentationthataddslittletounderstandingtheprogram.Henceourthirdpro-grammingprecept:10ProgrammingPreceptKeepyourdocumentationconcisebutdescriptive.Thestyleofdocumentation,aswithallwritingstyles,ishighlypersonal,andmanydifferentstylescanproveeffective.Thereare,nonetheless,somecommonlyacceptedguidelinesthatshouldberespected:1.Placeaprologueatthebeginningofeachfunctionincludingguidelines(a)Identification(programmer’sname,date,versionnumber).2(b)Statementofthepurposeofthefunctionandalgorithmused.(c)Thechangesthefunctionmakesandwhatdataituses.(d)Referencetofurtherdocumentationexternaltotheprogram.2.Wheneachvariable,constant,orclassisdeclared,explainwhatitisandhowitisused.Betterstill,makethisinformationevidentfromthename.2Tosavespace,programsprintedinthisbookdonotincludeidentificationlinesorsomeotherpartsoftheprologue,sincethesurroundingtextgivesthenecessaryinf
本文标题:Data Structures And Program Design In C++
链接地址:https://www.777doc.com/doc-4852342 .html