Tải bản đầy đủ - 0 (trang)
Chapter 10.  Minimize global and shared data

Chapter 10.  Minimize global and shared data

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

Summary

Sharingcausescontention:Avoidshareddata,especiallyglobal

data.Shareddataincreasescoupling,whichreduces

maintainabilityandoftenperformance.



Discussion

ThisstatementismoregeneralthanItem18'sspecific

treatment.

Avoiddatawithexternallinkageatnamespacescopeoras

staticclassmembers.Thesecomplicateprogramlogicand

causetightercouplingbetweendifferent(and,worse,distant)

partsoftheprogram.Shareddataweakensunittesting

becausethecorrectnessofapieceofcodethatusesshared

dataisconditionedonthehistoryofchangestothedata,and

furtherconditionsthefunctioningofacresofyet-unknowncode

thatsubsequentlyusesthedatafurther.

Namesofobjectsintheglobalnamespaceadditionallypollute

theglobalnamespace.

Ifyoumusthaveglobal,namespace-scope,orstaticclass

objects,besuretoinitializesuchobjectscarefully.Theorderof

initializationofsuchobjectsindifferentcompilationunitsis

undefined,andspecialtechniquesareneededtohandleit

correctly(seeReferences).Theorder-of-initializationrulesare

subtle;prefertoavoidthem,butifyoudohavetousethem

thenknowthemwellandusethemwithgreatcare.

Objectsthatareatnamespacescope,staticmembers,or

sharedacrossthreadsorprocesseswillreduceparallelismin

multithreadedandmultiprocessorenvironmentsandarea

frequentsourceofperformanceandscalabilitybottlenecks.

(SeeItem7.)Strivefor"shared-nothing;"prefer

communication(e.g.,messagequeues)overdatasharing.

Preferlowcouplingandminimizedinteractionsbetweenclasses.

(See[Cargill92].)



Exceptions

Theprogram-widefacilitiescin,cout,andcerrarespecial

andareimplementedspecially.Afactoryhastomaintaina

registryofwhatfunctiontocalltocreateagiventype,and

thereistypicallyoneregistryforthewholeprogram(but

preferablyitshouldbeinternaltothefactoryratherthana

sharedglobalobject;seeItem11).

Codethatdoesshareobjectsacrossthreadsshouldalways

serializeallaccesstothosesharedobjects.(SeeItem12and

[Sutter04c].)



References

[Cargill92]pp.126.136,169-173[Dewhurst03]Đ3

[Lakos96]Đ2.3.1[McConnell93]Đ5.1-4[Stroustrup00]

ĐC.10.1[Sutter00]Đ47[Sutter02]Đ16,AppendixA

[Sutter04c][SuttHysl03]



11.Hideinformation

Summary

Discussion

Exceptions

References



Summary

Don'ttell:Don'texposeinternalinformationfromanentitythat

providesanabstraction.



Discussion

Tominimizedependenciesbetweencallingcodethat

manipulatesanabstractionandtheabstraction's

implementation(s),datathatisinternaltotheimplementation

mustbehidden.Otherwise,callingcodecanaccessor,worse,

manipulatethatinformation,andtheintended-to-be-internal

informationhasleakedintotheabstractiononwhichcalling

codedepends.Exposeanabstraction(preferablyadomain

abstractionwhereavailable,butatleastaget/setabstraction)

insteadofdata.

Informationhidingimprovesaproject'scost,schedule,and/or

riskintwomainways:

Itlocalizeschanges:Informationhidingreducesthe"ripple

effect"scopeofchanges,andthereforetheircost.

Itstrengthensinvariants:Itlimitsthecoderesponsiblefor

maintaining(and,ifitisbuggy,possiblybreaking)program

invariants.(SeeItem41.)

Don'texposedatafromanyentitythatprovidesanabstraction

(seealsoItem10).Dataisjustonepossibleincarnationof

abstract,conceptualstate.Ifyoufocusonconceptsandnoton

theirrepresentationsyoucanofferasuggestiveinterfaceand

tweakimplementationatwillsuchascachingvs.computingonthe-flyorusingvariousrepresentationsthatoptimizecertain

usagepatterns(e.g.,polarvs.Cartesian).

Acommonexampleistoneverexposedatamembersofclass

typesbymakingthempublic(seeItem41)orbygivingout

pointersorhandlestothem(seeItem42),butthisapplies

equallytolargerentitiessuchaslibraries,whichmustlikewise

notexposeinternalinformation.Modulesandlibrarieslikewise



prefertoprovideinterfacesthatdefineabstractionsandtraffic

inthose,andtherebyallowcommunicationwithcallingcodeto

besaferandlesstightlycoupledthanispossiblewithdata

sharing.



Exceptions

Testingcodeoftenneedswhite-boxaccesstothetestedclassor

module.

Valueaggregates("C-stylestructs")thatsimplybundledata

withoutprovidinganyabstractiondonotneedtohidetheir

data;thedataistheinterface.(SeeItem41.)



References

[Brooks95]Đ19[McConnell93]Đ6.2[Parnas02]

[Stroustrup00]Đ24.4[SuttHysl04a]



12.Knowwhenandhowtocodefor

concurrency

Summary

Discussion

References



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

Chapter 10.  Minimize global and shared data

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

×