1 / 5

Information Hiding Example

Information Hiding Example. A very short talk. A reasonably good program.

maude
Télécharger la présentation

Information Hiding Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Information Hiding Example A very short talk

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

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

  4. 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

  5. The End

More Related