Information Hiding

Download Report

Transcript Information Hiding

Information Hiding
A very short talk
26-Jul-16
Modularization

Whenever a program is broken into two parts, there comes into being an
interface between them:





This interface should be kept as narrow and as explicit as possible.
How to make it narrow?





The parameter list
Any global or common
More subtle ways of information transmission
Have each method do only one thing
Be able to describe what it does without saying “but” or “except”
Keep parameter lists short
Avoid global variables
How to make it explicit?


Avoid global variables
Avoid side effects
Information hiding

The principle of information hiding is, informally,
Every method should mind its own business

To use a method, you need to know:



You do not need to know:


What information do you need to give it?
What does it tell you (or do for you) in return?
How it does its job
The method should not need to know:

How you do your job
What’s wrong with these?



int max (int size, int a[]) {
sort (a, size);
return a[0];
int max (int size, int a[]) {
int i;
temp = a[0];
for (i = 1; i < size; i++)
if (a[i] > temp) temp = a[i];
return temp;
}
int max (int size, int a[]) {
int i, temp;
temp = 0;
for (i = 0; i < size; i++)
if (a[i] > temp) temp = a[i];
return temp;
}
A reasonably good program
void play() {
setup();
player = who_goes_first();
do {
if (player == HUMAN) {
do {
move = get_humans_move();
} while !legal(move);
game_over = make_move(HUMAN, move);
player = COMPUTER;
}
else { /* player == COMPUTER */
move = choose_computers_move();
game_over = make_move(COMPUTER, move);
player = HUMAN;
}
} while(!game_over);
}
A not-so-good program
void play() {
setup();
player = who_goes_first();
do {
if (player == HUMAN) {
do {
move = get_humans_move();
check_if_legal(move);
movecounter++;
} while(!ok);
make_move(move);
}
else { /* player == COMPUTER */
move = choose_computers_move();
make_move(move);
}
} while (!game_over);
}






What routine or routines update
player? Do they do it in such a
way that the main program works?
If the human's move is not ok, has
player already been updated?
How does make() know whose
move to make?
Who sets ok?
When does the game end? Who's
responsible for deciding this?
Is movecounter being initialized
properly? Computed properly?
What is it anyway, and who uses
it?
Test-Driven Development (TDD)

It is difficult to add JUnit tests to an existing program


The program probably wasn’t written with testing in mind
It’s actually better to write the tests before writing the code you
want to test




When tests are written first, you have a clearer idea what to do when you
write the methods
Because the tests are written first, the methods are necessarily written to
be testable
Writing tests first encourages you to write simpler, single-purpose
methods
Because the methods will be called from more than one environment (the
“real” one, plus your test class), they tend to be more independent of the
environment
The End