Tải bản đầy đủ - 0 (trang)
Chapter 67.  Don't write unintentionally nongeneric code

Chapter 67.  Don't write unintentionally nongeneric code

Tải bản đầy đủ - 0trang

Summary

Committoabstractions,nottodetails:Usethemostgeneric

andabstractmeanstoimplementapieceoffunctionality.



Discussion

Whenwritingcode,usethemostabstractmeansavailablethat

getthejobdone.Thinkintermsofwhatoperationsputthe

least"capabilitiesstrain"ontheinterfacestheyoperateon.This

habitmakesyourcodemoregeneric,andthereforemore

reusableandmoreresilienttochangesinitssurroundings.

Onthecontrary,codethatgratuitouslycommitstodetailsis

rigidandfragile:

Use!=insteadof
generalandsoappliestoalargerclassofobjects;using<

asksforordering,andonlyrandom-accessiteratorscan

implementoperator<.Ifyouuseoperator!=yourcodewill

"port"easilytootherkindsofiterators,suchasforwardand

bidirectionaliterators.

Preferiterationtoindexedaccess:Mostcontainersdon't

supportindexedaccess;forexample,listcan'timplement

itefficiently.Butallcontainerssupportiterators.Iterationis

abetterapproachbecauseitismoregeneral,anditcanstill

beusedinconjunctionwithindexedaccessasneeded.

Useempty()insteadofsize()==0:"Empty/non-empty"

isamoreprimitiveconceptthan"exactsize."Forexample,

youmightnotknowthesizeofastreambutcanalwaystalk

aboutemptiness,andthesameappliestoinputiterators.

Somecontainers,suchaslist,naturallyimplementempty

moreefficientlythansize.

Usethehighestclassinthehierarchythatoffersthe

functionalityyouneed:Whenprogrammingwithdynamic

polymorphicclasses,don'tdependondetailsyoudon'tneed



andthattieyoutospecificderivedclasses.

Writeconst-correctcode(seeItem15):Takingconst&

parametersputslessstrainonthecallerbecauseconst&

coversconstantandnon-constantobjectsalike.



Exceptions

Insomecases,indexinginsteadofiterationenablescompilers

todobetteroptimization.Beforeyou'retemptedtomakeuseof

this,makesureyoureallyneeditandthatyourcompilerreally

doesit.(SeeItem8).



References

[Koenig97]Đ12.7,Đ17-18[Meyers01]Đ4[Stroustrup00]

Đ13,Đ17.1.1[Sutter04]Đ1,Đ5,Đ34



ErrorHandlingandExceptions

Errorhandlingisadifficulttaskforwhichthe

programmerneedsallthehelpthatcanbeprovided.

BjarneStroustrup,[Stroustrup94]Đ16.2

Therearetwowaystowriteerror-freeprograms;

onlythethirdoneworks.

AlanPerlis

Thequestionisn'twhetherornotwewillmake

programmingerrors;thequestioniswhetherornotwe

willarrangeforourcompilerandtoolstofindthemforus.

Thissectiondocumentshard-wonwisdomandbest

practices,someofthemgainedonlyafteryearsand

mountainsofpain.Followtheserulesandguidelines.Stick

tothem.Whenitcomestowritingrobust,safe,and

securesoftware,weneedallthehelpwecanget.

OurvoteforthemostvaluableIteminthissectiongoesto

Item69:Establisharationalerrorhandlingpolicy,and

followitstrictly.



68.Assertliberallytodocumentinternal

assumptionsandinvariants

Summary

Discussion

Examples

References



Summary

Beassertive!Useassertoranequivalentliberallytodocument

assumptionsinternaltoamodule(i.e.,wherethecallerand

calleearemaintainedbythesamepersonorteam)thatmust

alwaysbetrueandotherwiserepresentprogrammingerrors

(e.g.,violationsofafunction'spostconditionsdetectedbythe

callerofthefunction).(SeealsoItem70.)Ensurethat

assertionsdon'tperformsideeffects.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 67.  Don't write unintentionally nongeneric code

Tải bản đầy đủ ngay(0 tr)

×