coupling & cohesion cmsc 201- fall '11. vocabulary routine- a programming unit that...

Post on 03-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Coupling & Cohesion

CMSC 201- Fall '11

Vocabulary

Routine- A programming unit that performs a task, such as a function, procedure, method, or main class. (EX.) getValidInt, append

Module- A collection of objects that are logically related, such as constants, data types, variables, routines.

Component- A routine or module. In python can also be an object or class.

Kludge- A clumsy, patchy solution to a problem. Spaghetti Code- When functions use other functions,

and tracing your code resembles a bowl of spaghetti.

Program Structure

Every component should be clear about what it's inputs and outputs are.

This makes it so higher level functions can use the lower level functions, knowing that they will accomplish their intended goal.

Examples of lower level functions you have written are things such as getValidInt, enqueue, dequeue, pop, push, etc.

This leads to component independence.

Information Hiding

Components hide implementation details. If we have a guaranteed output given some input, then it doesn't matter how this gets accomplished.

Examples:

How many times in 201 have you appended something onto a list? We don't know the source code for append.

How many times have we used Wolfram Alpha to solve math problems for us?

At McDonald's, we give money, we get a McRib, and where it came from is not important

Component Coupling (The bad)

Coupling terminology: Tightly coupled: Many components rely on each

other Loosely coupled: Some weak dependencies* Uncoupled: No connections

Our goal is to always MINIMIZE coupling. Ways things can be coupled:

One component calling another One component passing data to another

component

Dependencies

When a component relies on the successful completion of a different component.

Before you drive your car, you need gasoline Before you print out a document, you need to plug your

printer in. Before you call your printMagicSquare() function, you

need to checkRows() and checkColumns() and checkDiagonals(), so printMagicSquare() is said to be dependent upon the checking functions.

Types of Coupling (Still the bad!)

Content Coupling: Component directly modifying the control flow of another component

Common Coupling: Modifying a common data structure from different components

Control Coupling: Booleans or other controls passed between components.

Stamp Coupling: Data Structures are passed between components

Data Coupling: Primitive data is passed between components

Content Coupling

A component is directly modifying the control flow of another component

This is very poor programming practice, and luckily not possible in python

Common Coupling

More than one component is modifying a common data structure

This is accomplished by global variables Disallowed in CMSC 201

Common Coupling (Avoid This!)

Control Coupling

Passing of control flags as component parameters

Unnecessary way to program, and should be avoided

New components should be created to avoid this

Eliminating Control Coupling

Stamp Coupling

Passing data structures (such as lists or stacks) to components to use

Not as bad as previous kinds of coupling, but still not ideal, and can often be eliminated

If we want to print out 100 items from a list, instead of passing a list to a component, the implementation should be calling the component 100 times, each time passing in the element from the list

Eliminating Stamp Coupling

Data Coupling

Not a bad thing...as a matter of fact how functions are called that pass parameters

Minimizing this can be difficult, and is unnecessary

Component Cohesion

Coincidental Cohesion: Components parts are unrelated

Logical Cohesion: Logically related task in same component

Temporal Cohesion: Performs tasks in sequence related by timing.

Procedural Cohesion: Tasks grouped together to ensure mandatory ordering.

Communicational Cohesion: Functions produce the same data set.

Sequential Cohesion: Output from one function is input to next

Functional Cohesion: Every Processing element is essential to the function.

Jeff's advice

1. Get a Whiteboard!!!

Planning your code before you write it is absolutely essential!

White-boards allow you to easily see what your program looks like, and possible holes

2. Learn Languages!!!

In my humble opinion: Most everyday useful: Perl or Python Most helpful at UMBC: Java Most fun: PLC

scheme Everyone should learn: Common Lisp What to learn next: Anything!

3. Don't code it if you don't get it!!!

Why to have something in your code: You know the expected inputs You know the expected outputs You know why it belongs in your code

Why not to have something in your code: If you don't know all three of those ^^^

4. Know when to stop

If you are typing the same thing over and over... If you're tired... If you're hungry... If you can't focus... If you get stuck!

Your code will deteriorate! Take a break! Take a nap! Have coffee!

5. Take Pride in your code

Little feels better than perfectly implemented and executed programming.

Have fun...listen to some music, grab some coffee, watch TV, browse the web, enjoy programming!

top related