Præsentation er lastning. Vent venligst

Præsentation er lastning. Vent venligst

Trinvis forfinelse vha. STREAM Systematisk, gradvis udvikling af programmer.

Lignende præsentationer


Præsentationer af emnet: "Trinvis forfinelse vha. STREAM Systematisk, gradvis udvikling af programmer."— Præsentationens transcript:

1 Trinvis forfinelse vha. STREAM Systematisk, gradvis udvikling af programmer

2 dIntProg, E08STREAM.2 Oversigt Eksempel på udvikling af et program –forfinelse af en klasse (Date) Proces –systematisk, gradvis fremgangsmåde Teknikker –separation of concerns –nedbrydning i løst koblede delproblemer –mañana-princippet

3 dIntProg, E08STREAM.3 Proces, overordnet Gør det simpleste du kan der bringer dig videre –Udgangspunkt i klassemodel –En klasse ad gangen (tag altid det simpleste først) –Sørg for hele tiden at have en ”tilfreds” compiler –Sørg hele tiden for at det du har lavet virker (lad fremdriften drives af og afspejles i resultatet fra et testprogram)

4 dIntProg, E08STREAM.4 Example: iDate Enter 789 6 4 5 1 23 0 11 September 2008 Next iDate

5 dIntProg, E08STREAM.5 Example: iDate Enter 789 6 4 5 1 23 0 11 September 2008 Next iDate

6 dIntProg, E08STREAM.6 Incremental Development of iDate 1.Construct (part of) the user interface, no functionality 2.Make it work except for the last day of a month 3.Make it work for variable number of days 4.Make it work for leap years (except centuries) 5.Make it work for centuries (except 4-centuries) 6.Make it work for 4-centuries Assume 30 days in every month; ignore leap years Enter 789 6 4 5 1 23 0 11 September 2008 Next iDate req One possible development scenario

7 dIntProg, E08STREAM.7 For Your Eyes Only! Enter 789 6 4 5 1 23 0 11 September 2008 Next iDate Prev Ready for the summer holiday season this year

8 dIntProg, E08STREAM.8 For Your Eyes Only! Enter 789 6 4 5 1 23 0 10 September 2008 Next iDate Prev Ready for the holiday season this year

9 dIntProg, E08STREAM.9 For Your Eyes Only! Enter 789 6 4 5 1 23 0 11 September 2008 Next iDate Prev Ready for the holiday season this year

10 dIntProg, E08STREAM.10 Specifications and Development Trace b c d e a f

11 dIntProg, E08STREAM.11 S T R E A M STREAMSTREAM Stubs Tests Representation alternatives Evaluation Attributes Methods A carefully down-scaled version of a full and rich software engineering process. Formulated for a single class

12 dIntProg, E08STREAM.12 Stubs STREAM We assume that the classes and their observable (public) functionality is understood and given, for example as a Java interface. Create an implementation class that implements the interface using empty stub methods. ”A walking skeleton”

13 dIntProg, E08STREAM.13 Tests STREAM Make sure that (an initial set of) test cases are provided for each method in the class.

14 dIntProg, E08STREAM.14 Representation STREAM Consider representation alternatives for the class: Rep 1, Rep 2,... Support a ”think before code” culture Avoid the ”first thing that comes to mind” syndrome Rep 1 : A short description of the first representation alternative. Rep 2 : A short description of the second representation alternative.

15 dIntProg, E08STREAM.15 REM: Representation Evaluation Matrix Evaluation STREAM Evaluate the representations using a Representation Evaluation Matrix (REM) IMPL. EFFORTRep 1 Rep 2 method 1 ()ChallengingTrivial method 2 ()TrivialHard method 3 ()EasyHard Apply effort qualifiers from the TEACH scale: Trivial, Easy, Average, Challenging, Hard

16 dIntProg, E08STREAM.16 Attributes STREAM Define the fields needed for the chosen representation. Provide appropriate initializations for each field (partial implementation of constructors).

17 dIntProg, E08STREAM.17 Methods STREAM Implement the methods of the class. This step takes the form of a nested loop WHILE a method is unfinished: choose an unfinished method; implement the method WHILE method not complete: improve the method; test ‘Implement the method’ is itself a repetitive activity: Choose the methods in order of simplicity

18 dIntProg, E08STREAM.18 Programmeringsprincip... Mañana Princippet Opmuntrer til –”separation of concerns” –mange små metoder Når du ønsker du havde en bestemt hjælpemetode, skriv da din kode som om du havde den. Implementer den senere.

19 dIntProg, E08STREAM.19... og (nogle) regler Special Case –hvis du har brug for kode til håndtering af et specialtilfælde, skriv da denne kode i en separat metode Nested Loop –lav aldrig nestede løkker; lav i stedet en separat metode og lad repetenden i den første løkke være et kald af denne metode Code Duplication –hvis du skriver det samme kodesegment mere end en gang, flyt da segmentet ud i en separat metode Hard Problem –hvis du har brug for en løsning på et problem som du ikke umiddelbart kan løse, lav da en separat metode til løsning af problemet Heavy Functionality –hvis en sekvens af sætninger eller et udtryk bliver for omfattende eller kompliceret, flyt da dette (eller dele heraf) ud i en separat metode

20 dIntProg, E08STREAM.20 Example: iDate Enter 789 6 4 5 1 23 0 11 September 2008 Next iDate

21 dIntProg, E08STREAM.21 iDate: STREAM class Date { /** Advance the date to the next day */ public void setToNextDate() {} /** = a rep. of this date in the format yyyy-mm-dd */ public String toString() { return null; } } Date void setToNextDate() String toString()

22 dIntProg, E08STREAM.22 iDate: STREAM ?

23 dIntProg, E08STREAM.23 iDate: STREAM R 1 : Use three integer variables: day, month, and year R 2 : Use one integervariable: number of days since...

24 dIntProg, E08STREAM.24 iDate: STREAM Simplicity of implementation R1R1 R2R2 setToNextDateChallengingTrivial toStringTrivialHard R 1 : Use three integer variables: day, month, and year R 2 : Use one integer variable: number of days since...

25 dIntProg, E08STREAM.25 iDate: STREAM class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year; /** Advance the date to the next day */ public void setToNextDate() {} /** = a rep. of this date in the format yyyy-mm-dd */ public String toString() { return null; }

26 dIntProg, E08STREAM.26 iDate: STREAM 1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year; /** Advance the date to the next day */ public void setToNextDate() {} /** = a rep. of this date in the format yyyy-mm-dd */ public String toString() { return year + “-” + month + “-” + days; }

27 dIntProg, E08STREAM.27 iDate: STREAM 2.1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year; /** Advance the date to the next day */ public void setToNextDate() { day= day + 1; }... } Violates the invariant if day is daysInMonth Mañana Principle: Special Case rule

28 dIntProg, E08STREAM.28 iDate: STREAM 2.2 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year; /** Advance the date to the next day */ public void setToNextDate() { day= day + 1; checkDayOverflow(); } /** Handle special case where day > daysInMonth */ private void checkDayOverflow() {}... } Mañana Principle: Special Case rule

29 dIntProg, E08STREAM.29 iDate: STREAM 3.1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** Handle special case where day > daysInMonth */ private void checkDayOverflow() { if ( day > 30 ) { day= 1; month= month + 1; }... } Assumption: 30 days in every month Violates the invariant if month is 12 Mañana Principle: Special Case rule (again)

30 dIntProg, E08STREAM.30 iDate: STREAM 3.2 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** Handle special case where day > daysInMonth */ private void checkDayOverflow() { if ( day > 30 ) { day= 1; month= month + 1; checkMonthOverflow(); } } /** Handle special case where month > 12 */ private void checkMonthOverflow() {}... } Mañana Principle: Special Case rule (again)

31 dIntProg, E08STREAM.31 iDate: STREAM 4.1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** Handle special case where month > 12 */ private void checkMonthOverflow() { if ( month > 12 ) { month= 1; year= year + 1; }... } That’s it! Except for the assumption of 30 days in every month, we’re done. Mañana Principle: Hard Problem rule

32 dIntProg, E08STREAM.32 iDate: STREAM 3.3 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** Handle special case where day > daysInMonth */ private void checkDayOverflow() { if ( day > daysInMonth() ) { day= 1; month= month + 1; } } /** = the number of days in the current month */ private int daysInMonth() { return 30; }... } Mañana Principle: Hard Problem rule

33 dIntProg, E08STREAM.33 iDate: STREAM 5.1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** = the number of days in the current month */ private int daysInMonth() { // 1 2 12 int[] daysInMonth= [0, 31, 28,..., 31]; int res= daysInMonth[month]; return res; }... } Mañana Principle: Special Case rule Except for leap years, we’re done.

34 dIntProg, E08STREAM.34 iDate: STREAM 5.2 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** = the number of days in the current month */ private int daysInMonth() { int[] daysInMonth= [0, 31, 28,..., 31]; int res= daysInMonth[month]; if ( month==2 and isLeapYear() ) { res= res + 1; } return res; } /** = the current year is a leap year */ private boolean isLeapYear() { return false; }... } Mañana Principle: Heavy Functionality rule

35 dIntProg, E08STREAM.35 iDate: STREAM 6.1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** = the current year is a leap year */ private boolean isLeapYear() { return (divides(4, year) && !divides(100, year)) || divides(400, year); } /** = x divides y */ private boolean divides(int x, int y) { return false; }... } Mañana Principle: Heavy Functionality rule

36 dIntProg, E08STREAM.36 iDate: STREAM 7.1 class Date { private int day, // 1  day  daysInMonth month, // 1  month  12 year;... /** = x divides y */ private boolean divides(int x, int y) { return y % x == 0; }... }

37 dIntProg, E08STREAM.37 Programmeringsteknikker Separation of concerns –nedbryd i delproblemer –fokuser på et forhold ad gangen –opdel problemstillingen så de enkelte dele er uafhængige (løs kobling) Mañana-princippet –Special Case –Nested Loop –Code Duplication –Hard Problem –Heavy Functionality

38 dIntProg, E08STREAM.38 Edgar Alle Poe Top down design of the poem The Raven (1845) –www.eapoe.org/works/poems/ravenj.htm Essay The Philosophy of Composition (1846) –www.eapoe.org/works/essays/philcomp.htm


Download ppt "Trinvis forfinelse vha. STREAM Systematisk, gradvis udvikling af programmer."

Lignende præsentationer


Annoncer fra Google