![Page 1: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/1.jpg)
Coupling & Cohesion
CMSC 201- Fall '11
![Page 2: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/2.jpg)
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.
![Page 3: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/3.jpg)
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.
![Page 4: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/4.jpg)
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
![Page 5: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/5.jpg)
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
![Page 6: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/6.jpg)
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.
![Page 7: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/7.jpg)
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
![Page 8: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/8.jpg)
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
![Page 9: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/9.jpg)
Common Coupling
More than one component is modifying a common data structure
This is accomplished by global variables Disallowed in CMSC 201
![Page 10: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/10.jpg)
Common Coupling (Avoid This!)
![Page 11: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/11.jpg)
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
![Page 12: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/12.jpg)
Eliminating Control Coupling
![Page 13: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/13.jpg)
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
![Page 14: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/14.jpg)
Eliminating Stamp Coupling
![Page 15: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/15.jpg)
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
![Page 16: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/16.jpg)
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.
![Page 17: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/17.jpg)
Jeff's advice
![Page 18: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/18.jpg)
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
![Page 19: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/19.jpg)
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!
![Page 20: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/20.jpg)
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 ^^^
![Page 21: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/21.jpg)
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!
![Page 22: Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class](https://reader035.vdocument.in/reader035/viewer/2022081515/56649f035503460f94c174f0/html5/thumbnails/22.jpg)
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!