Tải bản đầy đủ - 0 (trang)
Chapter 5.  Give one entity one cohesive responsibility

Chapter 5.  Give one entity one cohesive responsibility

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

Summary

Focusononethingatatime:Prefertogiveeachentity

(variable,class,function,namespace,module,library)onewelldefinedresponsibility.Asanentitygrows,itsscopeof

responsibilitynaturallyincreases,butitsresponsibilityshould

notdiverge.



Discussion

Agoodbusinessidea,theysay,canbeexplainedinone

sentence.Similarly,eachprogramentityshouldhaveoneclear

purpose.

Anentitywithseveraldisparatepurposesisgenerally

disproportionatelyhardertouse,becauseitcarriesmorethan

thesumoftheintellectualoverhead,complexity,andbugsofits

parts.Suchanentityislarger(oftenwithoutgoodreason)and

hardertouseandreuse.Also,suchanentityoftenoffers

crippledinterfacesforanyofitsspecificpurposesbecausethe

partialoverlapamongvariousareasoffunctionalityblursthe

visionneededforcrisplyimplementingeach.

Entitieswithdisparateresponsibilitiesaretypicallyhardto

designandimplement."Multipleresponsibilities"frequently

implies"multiplepersonalities"acombinatorialnumberof

possiblebehaviorsandstates.Preferbriefsingle-purpose

functions(seealsoItem39),smallsingle-purposeclasses,and

cohesivemoduleswithcleanboundaries.

Prefertobuildhigher-levelabstractionsfromsmallerlower-level

abstractions.Avoidcollectingseverallow-levelabstractionsinto

alargerlow-levelconglomerate.Implementingacomplex

behavioroutofseveralsimpleonesiseasierthanthereverse.



Examples

Example1:realloc.InStandardC,reallocisaninfamous

exampleofbaddesign.Ithastodotoomanythings:allocate

memoryifpassedNULL,freeitifpassedazerosize,reallocate

itinplaceifitcan,ormovememoryaroundifitcannot.Itis

noteasilyextensible.Itiswidelyviewedasashortsighted

designfailure.

Example2:basic_string.InStandardC++,

std::basic_stringisanequallyinfamousexampleof

monolithicclassdesign.Toomany"nice-to-have"featureswere

addedtoabloatedclassthattriestobeacontainerbutisn't

quite,isundecidedoniterationvs.indexing,andgratuitously

duplicatesmanystandardalgorithmswhileleavinglittlespace

forextensibility.(SeeItem44'sExample.)



References

[Henney02a][Henney02b][McConnell93]Đ10.5

[Stroustrup00]Đ3.8,Đ4.9.4,Đ23.4.3.1[Sutter00]Đ10,Đ12,

Đ19,Đ23[Sutter02]Đ1[Sutter04]Đ37-40



6.Correctness,simplicity,andclarity

comefirst

Summary

Discussion

Examples

References



Summary

KISS(KeepItSimpleSoftware):Correctisbetterthanfast.

Simpleisbetterthancomplex.Clearisbetterthancute.Safeis

betterthaninsecure(seeItems83and99).



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

Chapter 5.  Give one entity one cohesive responsibility

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

×