Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.

Similar presentations


Presentation on theme: "1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang."— Presentation transcript:

1 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang Student : Shi-kun Hou ID : E9406008

2 2 Outline Introduction Good start Classes from nouns Test before code Test Leading test Specifications Before You Code Class must have behavior Public methods first Method Delegation Instance varables

3 3 Introduction This paper describes some of the patterns for novice programmers to learn Java programs Patterns –Getting started –Writing tests –Identifying classes –Writing methods –Identifying instance variables

4 4 Patterns Getting started –Good start –Classes from nouns Writing tests –Test before code –Test –Leading test –Specification before code

5 5 Patterns Identifying classes –Class –One major responsibility –Class captures an abstraction –Class must have behavior Writing methods –Public methods first –Method –Delegation –Helper method Identifying instance variables –Instance variable

6 6 Good start Starting a program is difficult. –Sometime, a problem looks too difficult. You feel like have to learn about the problem area. How much is enough? –Other times, a problem looks really easy. A mess of question:file,classes,methods and if statements. A program consists of a set of objects interacting to solve a problem.

7 7 Good start Find the objects you need and then write classes to represent them. – identify all the objects and classes ? Identify a class or two that you think will be in your program.

8 8 Classes from nouns A problem description usually talks about the most important concepts in the problem. The nouns in the description name things: –concrete things : people, toasters –processes : the computation of a wage –abstract things : categories

9 9 Classes from nouns start with the nouns used in the problem description as candidate names for classes. –Remove duplication by eliminating synonyms –Add names that must be present. –Eliminate candidates that seem to be beyond the purpose of program.

10 10 Test before code Starting to write a class is difficult, too. You need to know what messages the object responds to. Write a test for the class before you write any code for the class. The test is a user of the class Use the test to specify some intended behavior of the class

11 11 Test You still have to write the code. That may seem like an efficient way to work! –write code for several hours without compiling and testing –then shift into a mode where you compile and debug for a while. But that efficiency is probably an illusion. The longer you write code without compiling, the more errors your code is likely to contain.

12 12 Test Taking small steps –Start with a single test case. –See what objects are needed to satisfy the test. –Write just enough code to make the test pass.

13 13 Leading test Some tests are trivial, others are very hard. How do I know which test to write next? Pick a test –helps you know when you have successfully implemented the next piece of functionality –require some new code, but not a lot of new code. –Favor easy tests over hard ones, and important operations over unimportant ones.

14 14 Specification Before You Code You have read the programming assignment More questions than are answered in this initial document You would like to clarify your idea of what you are being asked to do. Write a specification before you begin to write code.

15 15 Specification Before You Code This specification can be a more careful description of the solution enumerates classes and possible methods To specify the solution with a UML diagram

16 16 Class must have behavior A class must have some behavior That distinguishes it from other objects that currently exist specialized comparisons represent behavior

17 17 Public methods first An object exists to serve a purpose to other objects. The essential parts of any class are the methods. the methods allow an object to do the things that other objects expect it to do. A class may end up having private methods, but other objects won't know that.

18 18 Public methods first The public methods specify the behavior of the object. They implement the behavior that other objects will see. write the public methods of your class first. Write just enough code to implement a method that satisfies a test.

19 19 Method Objects do things, and objects know things. a client that uses an object will send it messages –asking it to do something –asking about something it knows. Think of the methods in your class in terms of these two kinds of responsibility: –methods that do things –methods that report what the object knows.

20 20 Method you may want to write a method that does both. But consider –the object that expects the method only to report something It may send the message and then be surprised that that state of the receiver has changes. –the object that expects the method only to do something. It may send the message and not realize that the value returned carries important information

21 21 Method design methods so that they either do something or return something If a method does something, give it a name that sounds like a command. If a method reports something, give it a name that describes its result.

22 22 Delegation A program is a set of objects collaborating to solve a problem. if two objects do the same thing write code that is identical or similar in two different parts of program Duplication of this sort is bad. –When implement the behavior in a different way, then you have to change it in two or more places. –Programmers will have a harder time understanding your program

23 23 Delegation Therefore, ask another object to do a task if it knows how. Write new code for the part of the behavior that the other object cannot do.

24 24 Instance variable When do I create an instance variable? Instance variables should always be private outside users won't be able to tell whether a variable exists inside a class. create an instance variable when multiple public methods need to share the same data the history of that data is important to all those methods.

25 25 The end

26 26 Examples : Test before code Examples : writing a program to model a toaster making toast. –create a new class named Toaster –A Toaster toasts certain objects of type Bread.

27 27 Examples : Test before code –a simple test public void testCanSetDoneness() { Toaster toaster = new Toaster(); toaster.setDoneness(Toast.MEDIUM); assertEquals( toaster.getDoneness(), Toast.MEDIUM ); } There is a Toaster class. There is a Toast class. Toast contains a static constant named MEDIUM. Toaster contains a method named setDoneness() that takes a parameter of type compatible with Toast.MEDIUM. Toaster contains a method named getDoneness() that takes no arguments but returns a value of the same type as Toast.MEDIUM Toaster has some way of remembering the degree of doneness so that the value set by setDoneness() can later be retrieved with getDoneness().

28 28 Examples : Test before code –you probably want to make sure that the bread reflects that level of toasting when the Toaster is done. public void testToastIsToastedToCorrectDegreeOfDoneness() { Toaster toaster = new Toaster(); Bread bread = new RyeBread(); toaster.setDoneness( Toast.MEDIUM ); assertEquals( bread.getDoneness(), Toast.NOT_TOASTED ); toaster.toast( bread ); assertEquals( bread.getDoneness(), Toast.MEDIUM ); } – It contains both Bread objects and Toast objects.

29 29 Examples : Test before code –What is the relationship between them? –Just toast a piece of Bread. public void testToastBread() { Toaster toaster = new Toaster(); Bread bread = new RyeBread(); assertFalse( bread.isToasted() ); toaster.toast( bread ); assertTrue( bread.isToasted() ); } a Bread class an isToasted() method to Bread a toast() method to the Toaster class that takes a Bread object as an argument

30 30 Examples : Test before code –The only thing we've used the Toast class for is to contain the static final values that indicate doneness –move those values to the Toaster class public void testCanSetDoneness() { Toaster toaster = new Toaster(); toaster.setDoneness( Toaster.MEDIUM ); assertEquals( toaster.getDoneness(), Toaster.MEDIUM ); } This design of the Toaster and Bread classes should feel a lot like design in real life.

31 31 Examples : Test create a new VirtualPet class, following trivial test case public void testVirtualPetExists(){ VirtualPet pet = new VirtualPet(); assertNotNull(pet); } –Create the corresponding production code: class VirtualPet{ } The test should now compile and pass.

32 32 Examples : Test –now refactor your test like this private VirtualPet pet; protected void setUp(){ pet = new VirtualPet(); } public void testVirtualPetExists(){ asserNotNull(pet); } This should also pass. –suppose you want your pet to have a name. public void testCanNamePet(){ pet.setName("Igor"); assertSame(pet.getName(), "Igor"); }

33 33 Examples : Test –make small changes to your VirtualPet class: class VirtualPet{ private String name ; public void setName(String name){ this.name = name; } public String getName(){ return name; } –What happens if you ask for the name of an unnamed VirtualPet?

34 34 Examples : Test – Perhaps you want to gave an empty String returned. Then test it like this public void testNameOfUnnamedPetIsEmptyString(){ assertSame(pet.getName(),""); } This forces you to go back and initialize the String name in VirtualPet. –Small tests, one at a time.

35 35 Example : Leading test Example : John Conway's game of life. –It takes place on an infinite grid with square cells. –Each cell has as neighbors the eight cells that touch it. – you can set up the board in any state you wish and start the game.

36 36 Example : Leading test –The state of the board at each discrete point in time is determined by the state of the board at the previous time by the following rules: A live cell stays alive only if it has two or three live neighbors, otherwise it dies. A dead cell comes to life only if it has three live neighbors, otherwise it remains dead. ○: life ╳: dies

37 37 Example : Leading test –A leading test to ensure that we can create a new cell and when it is created it will not be alive. public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(!cell.isAlive()); } –The next smallest thing to check on is that we can set a cell to be alive. public void testLiveCellIsAlive(){ Cell cell = new Cell(); cell.setAlive(true); assertTrue(cell.isAlive()); }

38 38 Example : Leading test –A more complicated test to set up might be this: public void testLiveCellWithThreeLiveNeighborsStaysAlive(){ cell.setAlive(true); for (int i = 0; i <3; i++){ cell.incrementNumberOfLiveNeighbors(); } assertTrue(!cell.needsToChange()); }


Download ppt "1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang."

Similar presentations


Ads by Google