solid principles i: the secret of life

34
The Secret of Life a.k.a. SINGLE RESPONSIBILITY PRINCIPLE

Upload: kristjan-siimson

Post on 22-Jan-2018

268 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Solid Principles I: The Secret of Life

The Secret of Life a.k.a.

SINGLE RESPONSIBILITY PRINCIPLE

Page 2: Solid Principles I: The Secret of Life

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.

Page 3: Solid Principles I: The Secret of Life

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.

Page 4: Solid Principles I: The Secret of Life

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.

Page 5: Solid Principles I: The Secret of Life

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.

Page 6: Solid Principles I: The Secret of Life

DRYThe Single Responsibility Principle

A class should have only one reason to change.

Page 7: Solid Principles I: The Secret of Life

The Single Responsibility Principlehttp://www.youtube.com/watch?v=X4CvFWCULuI So what is that “one” thing? Declare a variable and we're

done?

DRY

Page 8: Solid Principles I: The Secret of Life
Page 9: Solid Principles I: The Secret of Life

Coincidental CohesionA coincidentally cohesive module is one whose elements

contribute to activities with no meaningful relationship to one another.

Page 10: Solid Principles I: The Secret of Life

Coincidental CohesionFix car

Bake cake

Walk dog

Fill out astronaut-application form

Have a beer

Get out of bed

Go to movies

Page 11: Solid Principles I: The Secret of Life

Coincidental CohesionNo well-defined function.

Fortunately rare.

Caused by misguided attempts of morons to save time or memory (both usually unsuccessful).

Page 12: Solid Principles I: The Secret of Life

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.

Page 13: Solid Principles I: The Secret of Life

Logical CohesionExample:

Go by carGo by trainGo by boatGo by place

A person would have to pick one for a journey.

Page 14: Solid Principles I: The Secret of Life

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.

Page 15: Solid Principles I: The Secret of Life

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.

Page 16: Solid Principles I: The Secret of Life

Temporal CohesionPut out milk bottlesPut out catTurn off TVBrush teeth

Page 17: Solid Principles I: The Secret of Life

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.

Page 18: Solid Principles I: The Secret of Life

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.

Page 19: Solid Principles I: The Secret of Life

Procedural CohesionExample:

Clean utensils from previous mealPrepare turkey for roastingMake phone callTake showerChop vegetablesSet table

Page 20: Solid Principles I: The Secret of Life

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.

Page 21: Solid Principles I: The Secret of Life

Communicational CohesionA communicationally cohesive module is one whose

elements contribute to activities that use same input or output data.

Page 22: Solid Principles I: The Secret of Life

Communicational CohesionSuppose we want to find out details about a book:

Find title of bookFind price of bookFind publisher of bookFind author of book

Page 23: Solid Principles I: The Secret of Life

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.

Page 24: Solid Principles I: The Secret of Life

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.

Page 25: Solid Principles I: The Secret of Life

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

Page 26: Solid Principles I: The Secret of Life

Functional CohesionA functionally cohesive module contains elements that all

contribute to the execution of one and only one problem-related task.

Page 27: Solid Principles I: The Secret of Life

Functional CohesionExamples:

Compute cosine of angleVerify alphabetic syntaxRead transaction recordDetermine customer mortgage payment…

Page 28: Solid Principles I: The Secret of Life

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

Page 29: Solid Principles I: The Secret of Life

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…

Page 30: Solid Principles I: The Secret of Life

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

Page 31: Solid Principles I: The Secret of Life

Determining Module Cohesion

Page 32: Solid Principles I: The Secret of Life

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.

Page 33: Solid Principles I: The Secret of Life

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.

Page 34: Solid Principles I: The Secret of Life

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.