solid principles i: the secret of life
TRANSCRIPT
The Secret of Life a.k.a.
SINGLE RESPONSIBILITY PRINCIPLE
DRY?Curly’s Law
A variable should mean one thing, and one thing only. It should not mean one thing in one circumstance, and
carry a different value from a different domain some other time.
It should not mean two things at once. It must not be both a floor polish and a dessert topping. It should mean One Thing, and should mean it all of the
time.
DRY?Don’t Repeat Yourself
If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step of each other.
Even if they don’t, you’re guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur.
Don’t repeat yourself is important if you want flexible and maintainable software.
DRY?Once and Only Once
Each and one declaration of behavior should occur once, and only once. This is one of the main goals, if not the main goal, when refactoring code.
The design goal is to eliminate duplicated declarations of behavior, typically merging them or replacing multiple similar implementations with a unifying abstraction.
DRY?Single Point of Truth
Repetition leads to inconsistency and code that is subtly broken, because you changed only some repetitions when you needed to change all of them.
Often, it also means you haven’t properly thought though the organization of your code.
Any time you see duplicate code, that’s a danger sign. Complexity is a cost; don’t pay it twice.
DRYThe Single Responsibility Principle
A class should have only one reason to change.
The Single Responsibility Principlehttp://www.youtube.com/watch?v=X4CvFWCULuI So what is that “one” thing? Declare a variable and we're
done?
DRY
Coincidental CohesionA coincidentally cohesive module is one whose elements
contribute to activities with no meaningful relationship to one another.
Coincidental CohesionFix car
Bake cake
Walk dog
Fill out astronaut-application form
Have a beer
Get out of bed
Go to movies
Coincidental CohesionNo well-defined function.
Fortunately rare.
Caused by misguided attempts of morons to save time or memory (both usually unsuccessful).
Logical CohesionA logically cohesive module is a module in which
elements contribute to the same category or activities to be executed are selected from outside the module.
Logical CohesionExample:
Go by carGo by trainGo by boatGo by place
A person would have to pick one for a journey.
Logical CohesionOne activity would be used, but unlikely all of them
would be used on a particular journey.
Similar to coincidental cohesion in its schizophrenia.
People create logically cohesive modules to overlap part of functions that happen to have the same lines of code – but which are not executed at the same time.
A better name would be illogical cohesion.
Temporal CohesionA temporally cohesive module is one whose elements are
in activities that are related in time. The order when they are carried out is not important.
Temporal CohesionPut out milk bottlesPut out catTurn off TVBrush teeth
Temporal CohesionThey are unrelated, except that they are carried out at a
particular time.
Classic example: initialization module.
The only relationship between functions is that they happen to be carried out at the same time.
Coupling to procedural and temporal modules ranges from mediocre to poor.
Procedural CohesionA procedurally cohesive module is one whose elements
are involved in different and possibly unrelated activities in which control flows from each activity to the next. The order in which the activities are carried out is important.
Procedural CohesionExample:
Clean utensils from previous mealPrepare turkey for roastingMake phone callTake showerChop vegetablesSet table
Procedural CohesionThere isn’t “Get Dressed” or “Make another phone call”,
which might be on the list some other day.
Pieces of procedurally cohesive modules tend to have little relationship to one another, except that they are carried out in that specific order at a certain time.
Communicational CohesionA communicationally cohesive module is one whose
elements contribute to activities that use same input or output data.
Communicational CohesionSuppose we want to find out details about a book:
Find title of bookFind price of bookFind publisher of bookFind author of book
Communicational CohesionQuite maintainable, but there are some issues:
If another module needs the same data, but not all of it, then this module would have to disregard some data. For example, if we use customer details and another module needs to know only the loan balance, then it would have to disregard customer name… or duplicate the code of retrieving customer.
Temptation to share code among the activities within it. This makes it hard to change one activity without destroying another.
Sequential CohesionA sequentially cohesive module is one whose elements
are involved in activities that output data from one activity serves as input data to next.
Sequential CohesionExample:
Repaint your bright orange Corvette to classy red color.1. Clean car body2. Fill in holes in car3. Sand car body4. Apply primer
This cannot be summed up as a single function as it is not functionally cohesive. Missing:5. Put on final coat
Functional CohesionA functionally cohesive module contains elements that all
contribute to the execution of one and only one problem-related task.
Functional CohesionExamples:
Compute cosine of angleVerify alphabetic syntaxRead transaction recordDetermine customer mortgage payment…
Determining Module CohesionFunctional cohesion
A module doing one function can be summed up by a precise verb-object name.READ CUSTOMER TRANSACTION RECORDCALCULATE NORMAL AUTO INSURANCE PREMIUMDEDUCT FEDERAL TAXES
Sequential cohesionA number of assembly-line functions show sequential
cohesion.VALIDATE TRANSACTION AND (USE IT TO) UPDATE MASTER
RECORD
Determining Module CohesionCommunicational cohesion
A number of nonsequential functions working on the same dataCALCULATE AVERAGE AND MAXIMUM EMPLOYEE SALARY
Procedural cohesionLook for procedural or “flowcharty” names
LOOP ROUTINE or SWITCH MODULE
Or in worse casesSTART UP, END OF JOB, BEFORE…
Determining Module CohesionLogical cohesion
The name is an umbrella or general-purpose oneINPUT ALL or EDIT MODULE
And to use the module, a flag is needed.
Coincidental cohesionThe name is silly or meaningless
FRUNK-MODULE, PROCESS-ROUTINE, …EXECUTEEXECUTE
Determining Module Cohesion
Determining Module CohesionAFTIN2 After input, add control items and
verify totals
AFTIN2 After input, write proof tape, add control items, verify totals.
GENREPT Produce report: either a sales report, a project status report, or a customer transaction report.
Determining Module CohesionSYNCH Check syntactic correctness of
space-vehicle guidance parameters.
OUTTRAN Print transaction and store it indatabase.
UPCREDOUT Update current credit record and write it to database.
Determining Module CohesionSTARTIT Open database table, obtain first
transaction and first record, and output page headings.
NEWTRAN Update record in database and get next transaction.
CIRCDISP From an electronic connection matrix, produce a circuit diagram.