TowardDevelopingGoodProgrammingStyleCversion,August1997(McCann)Everyprogramyouwritethatyouintendtokeeparoundformorethanacoupleofhoursoughttohavedocumentationinit.Don'ttalkyourselfintoputtingoffthedocumentation.Aprogramthatisperfectlycleartodayisclearonlybecauseyoujustwroteit.Putitawayforafewmonths,anditwillmostliketakeyouawhiletofigureoutwhatitdoesandhowitdoesit.Ifittakesyouawhiletofigureitout,howlongwouldittakesomeoneelsetofigureitout?Programmingstyleisatermusedtodescribetheeffortaprogrammershouldtaketomakehisorhercodeeasytoreadandeasytounderstand.Goodorganizationofthecodeandmeaningfulvariablenameshelpreadability,andliberaluseofcommentscanhelpthereaderunderstandwhattheprogramdoesandwhy.Probablythebestwaytodemonstratethevalueofgoodstyleiswithasimpleexample.(Evenifyoudon'tknowCverywellyet,keepreading;youcanbenefitfromthisexampleevenifyoucan'tunderstandit.)Takealookatthisprogram:#includestdio.hintmain(void){intseg[10]={6,2,5,5,4,5,6,3,7,6};intd1,d2,d3,d4,m=0,td,ts;for(d1=0;d12;d1++)for(d2=0;d210;d2++)for(d3=0;d36;d3++)for(d4=0;d410;d4++)if((!((d1==0)&&(d2==0)))&&(!((d1==1)&&(d22)))){if(d1==0){ts=seg[d2]+seg[d3]+seg[d4];td=d2+d3+d4;if(ts==td){m++;printf(%1d:%1d%1d\n,d2,d3,d4);}}else{ts=seg[d1]+seg[d2]+seg[d3]+seg[d4];td=d1+d2+d3+d4;if(ts==td){m++;printf(%1d%1d:%1d%1d\n,d1,d2,d3,d4);}}}return0;}Doyouhavethefaintestideaofwhatitaccomplishes?Howlongwouldittakeyoutoexplainhowitworks?Moretothepoint:Ifyouwereassignedtofixaproblemwiththiscode,howfrustratingwouldthetaskbetoyouandhowoftenwouldyoucursetheprogrammerwhowroteit?Nowimaginethatyouwroteitandsomeotherprogrammerwascursingyou!(Worse:Imagineyouwrotethis,itdidn'twork,andyouhadtoaskyourinstructorforhelpinfindingtheproblem.IfIwereyourinstructor,I'dflat-outrefusetolookatthisuntilyoucleaneditup.)StylePrincipleStructureanddocumentyourprogramthewayyouwishotherprogrammerswould.INDENTATION(Foramoredetailedtreatmentofcodeindentation,pleaseseetheIndentingCProgramspage.)Oneimmediateproblemthatthisprogramhasisthatitdoesnotadheretoaconsistentindentationpattern.Therearedozensofindentationstylesthatyoucouldadopt,andsomegeneralideasarecommontomostofthem:Thestyleisappliedconsistentlythroughouttheprogram.Codewithinablock(e.g.,insidealoop,orinthebodyofasubprogram)shouldbeindented.Ifablockisnestedwithinanotherblocktheinnerblock'sbodyshouldbeindentedrelativetotheenclosingblock.Avoidexcessivestairstepindentation(suchasyouoftenseewithgroupsofnestedIFstatements)becausethiswillforceyoutoattempttosqueezecodetofitonjusttherighthalfofthescreen/page.Ifstairsteppingbecomesaproblem,reducethenumberofspacesperindentation(from8to4,forexample)orswitchtoaverticalstyletemporarily.Here'sthecodefromabovewithaconsistentindentationapplied.Ialsotookthelibertyofaddingalittlewhitespace(blanklines)tohelpsetoffsectionsoftheprogram.Ithinkyou'llagreethatthisisanimprovement,butnotyetacceptable:#includestdio.hintmain(void){intseg[10]={6,2,5,5,4,5,6,3,7,6};intd1,d2,d3,d4,m=0,td,ts;for(d1=0;d12;d1++)for(d2=0;d210;d2++)for(d3=0;d36;d3++)for(d4=0;d410;d4++)if((!((d1==0)&&(d2==0)))&&(!((d1==1)&&(d22)))){if(d1==0){ts=seg[d2]+seg[d3]+seg[d4];td=d2+d3+d4;if(ts==td){m++;printf(%1d:%1d%1d\n,d2,d3,d4);}}else{ts=seg[d1]+seg[d2]+seg[d3]+seg[d4];td=d1+d2+d3+d4;if(ts==td){m++;printf(%1d%1d:%1d%1d\n,d1,d2,d3,d4);}}}return0;}DoyouseethestairstepeffectcausedbytheindentationofthenestedFORloops?(Heck,howcouldyoumissit?)AddtheIFstatementstothecodeandthereisn'tmuchspaceleftforcodeoneachlineifyou'retryingtostaywithinthe80columnlimitofmostscreens.Here'stheprogramwithaverticalstyleappliedtotheloops(notethatasimilarprocedurecanbeappliedtocloselynestedIFstatementsaswell):#includestdio.hintmain(void){intseg[10]={6,2,5,5,4,5,6,3,7,6};intd1,d2,d3,d4,m=0,td,ts;for(d1=0;d12;d1++)for(d2=0;d210;d2++)for(d3=0;d36;d3++)for(d4=0;d410;d4++)if((!((d1==0)&&(d2==0)))&&(!((d1==1)&&(d22)))){if(d1==0){ts=seg[d2]+seg[d3]+seg[d4];td=d2+d3+d4;if(ts==td){m++;printf(%1d:%1d%1d\n,d2,d3,d4);}}else{ts=seg[d1]+seg[d2]+seg[d3]+seg[d4];td=d1+d2+d3+d4;if(ts==td){m++;printf(%1d%1d:%1d%1d\n,d1,d2,d3,d4);}}}return0;}Thislooksmuchbetterbecausethere'snowroomtoindentthenestedIFstatementswithenoughroomleftovertodoadecentjobwiththestatementsinsidetheIFs.Noteveryonelikesthisapproach,however.Somepeoplepreferthatprogrammersmovethebodyoftheloopstoasubprograminstead.Inmyviewthisisacceptableonlyifthatsectionofcodecanlogicallystandonitsown.Evenifthiscode-relocationideadoesn'tsitrightwithyou,playalong;havingasubprogramwillbehandyasanexamplelateron.Here'sthecompleteprogramwiththisphilosophyapplied:#includestdio.hvoidcount(int,int,int,int,int*);intmain(void){intd1,d2,d3,d4,m=0;for(d1=0;d12;d1++)for(d2=0;d210;d2++)for(d3=0;d36;d3++)for(d4=0;d410;d4++)count(d1,d2,d3,d4,&m);return0;}voidcount(intd1,intd2,intd3,intd4,int*m){intseg[10]={6,2,5,5,4,5,6,3,7,6};intts,td;if((!((d1==0)&&(d2==0)))&&(!((d1==1)&&(d22)))){if(d1==0){ts=seg[d2]+seg[d3]+seg[d4];td=d2+d3+d4;if(ts==td){(*m)++;printf(%1d:%1d%1d\n,d2,d3,d4);}}else{ts=seg[d1]+seg[d2]+seg[d3]+seg[d4];td=d1+d2+d3