28-oct-15 information hiding a very short talk. modularization whenever a program is broken into two...

9
Jul 20, 2 022 Information Hiding A very short talk

Upload: jemimah-goodman

Post on 03-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

Apr 20, 2023

Information Hiding

A very short talk

Page 2: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

Modularization Whenever a program is broken into two parts, there comes into being an

interface between them: The parameter list Any global or common More subtle ways of information transmission

This interface should be kept as narrow and as explicit as possible. How to make it narrow?

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

Page 3: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between
Page 4: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

Information hiding

The principle of information hiding is, informally, Every method should mind its own business

To use a method, you need to know: What information do you need to give it? What does it tell you (or do for you) in return?

You do not need to know: How it does its job

The method should not need to know: How you do your job

Page 5: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

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;}

Page 6: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

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);}

Page 7: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

A not-so-good program 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?

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);}

Page 8: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

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

Page 9: 28-Oct-15 Information Hiding A very short talk. Modularization Whenever a program is broken into two parts, there comes into being an interface between

The End