资源描述:
《C语言编程规范_Linux.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、LinuxkernelcodingstyleThisisashortdocumentdescribingthepreferredcodingstyleforthelinuxkernel.Codingstyleisverypersonal,andIwon't_force_myviewsonanybody,butthisiswhatgoesforanythingthatIhavetobeabletomaintain,andI'dpreferitformostotherthingstoo.Pleaseatl
2、eastconsiderthepointsmadehere.Firstoff,I'dsuggestprintingoutacopyoftheGNUcodingstandards,andNOTreadit.Burnthem,it'sagreatsymbolicgesture.Anyway,heregoes:Chapter1:IndentationTabsare8characters,andthusindentationsarealso8characters.Therearehereticmovement
3、sthattrytomakeindentations4(oreven2!)charactersdeep,andthatisakintotryingtodefinethevalueofPItobe3.Rationale:Thewholeideabehindindentationistoclearlydefinewhereablockofcontrolstartsandends.Especiallywhenyou'vebeenlookingatyourscreenfor20straighthours,yo
4、u'llfinditaloteasiertoseehowtheindentationworksifyouhavelargeindentations.Now,somepeoplewillclaimthathaving8-characterindentationsmakesthecodemovetoofartotheright,andmakesithardtoreadona80-characterterminalscreen.Theanswertothatisthatifyouneedmorethan3l
5、evelsofindentation,you'rescrewedanyway,andshouldfixyourprogram.Inshort,8-charindentsmakethingseasiertoread,andhavetheaddedbenefitofwarningyouwhenyou'renestingyourfunctionstoodeep.Heedthatwarning.Thepreferredwaytoeasemultipleindentationlevelsinaswitchsta
6、tementistoalignthe"switch"anditssubordinate"case"labelsinthesamecolumninsteadof"double-indenting"the"case"labels.E.g.:switch(suffix){case'G':case'g':mem<<=30;break;case'M':case'm':mem<<=20;break;case'K':case'k':mem<<=10;/*fallthrough*/default:break;}Don
7、'tputmultiplestatementsonasinglelineunlessyouhavesomethingtohide:if(condition)do_this;do_something_everytime;Don'tputmultipleassignmentsonasinglelineeither.Kernelcodingstyleissupersimple.Avoidtrickyexpressions.Outsideofcomments,documentationandexceptinK
8、config,spacesareneverusedforindentation,andtheaboveexampleisdeliberatelybroken.Getadecenteditoranddon'tleavewhitespaceattheendoflines.Chapter2:BreakinglonglinesandstringsCodingstyleisallaboutreadabilityandmaintainabilityusingcomm