• No results found

Chapter 9: Building Bigger Programs

N/A
N/A
Protected

Academic year: 2021

Share "Chapter 9: Building Bigger Programs"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter  9:      

Building  Bigger  Programs  

(2)

How  to  Design  Larger  Programs  

—  Building  something  larger  requires  good  software  

engineering.  

—  Design  

—  Top-­‐down:  Start  from  requirements,  then  identify  the  pieces  to  write,  

then  write  the  pieces.  

—  Bottom-­‐up:  Start  building  with  the  pieces  you  know,  test  them,  

combine  them,  and  keep  going  until  you  have  your  program  

—  Coding  and  Debugging:  figure  out  what  doesn’t  work,  why,  

and  how  to  fix  it.  Done  at  the  same  time  as  you  write  the  code.  

—  Testing:  make  sure  the  program  does  what  it  is  supposed  to  do  

and  does  it  well.  

—  Maintenance:  add  new  features,  solved  newly  discovered  bugs  

(3)

Top-­‐Down  Design  

— 

Start  from  a  problem  statement.  

—  What  are  you  trying  to  do?  

— 

Refine  the  problem  statement.  

—  Use  hierarchical  decomposition  to  define  subparts.  

— 

Continue  refining  until  you  know  how  to  write  the  

functions.  

— 

Use  

procedural  abstraction

 so  that  higher-­‐level  

functions  are  written  in  terms  of  lower-­‐level.  

—  Procedural  abstraction  allows  us  to  focus  in  the  big  

picture  instead  of    on  the  little  details  

(4)

Top-­‐Down  Design  

Top-­‐level   function   1st  level  Sub-­‐ function   2nd  level  Sub-­‐ function   2 nd  level     Sub-­‐function   1st  level  Sub-­‐ function   2nd  level     Sub-­‐function   2 nd  level     Sub-­‐function   3rd  level    
(5)

Example  Top-­‐Down  Design:  

An  Adventure  Game  

— 

Text-­‐based  game  

— 

The  player  explores  a  house  

moving  between  rooms    

— 

To  move  the  player  uses  

commands  such  as  “move  

north”,  “move  east”,    “quit”,  

etc.  

(6)

Example  Top-­‐Down  Design:  

An  Adventure  Game  

Top-­‐level  function:  

1. 

Tell  the  user  how  to  play  

the  game.  

2. 

Describe  the  room  the  

user  is  currently  in.  

3. 

Get  the  player’s  command.  

4. 

Figure  out  the  next  room  

the  user  goes  to.  

5. 

Return  to  Step  2,  and  

(7)

Some  new  func@ons  

—  printNow():  Takes  a  string  as  input,  and  prints  it  on  the  

Command  Area  immediately.  

—  print() waits  until  the  program  is  done  before  it  prints.  

—  requestString(prompt): Takes  a  prompt  string  as  input,  

accepts  a  string  from  the  user  in  a  dialog  window,  then   returns  the  user’s  input.  

—  Other  input  functions  in  JES:    

—  requestNumber(prompt)

—  requestInteger(prompt)

—  requestIntegerInRange(prompt, min, max)

—  Counter  part  in  Python:      

—  raw_input(prompt) -­‐  for  strings  

(8)

Some  new  func@ons  

— 

JES  also  provides  some  output  functions:  

—  showError (string)

—  showWarning (string)

—  showInformation (string)

(9)

An  important  new  loop  

— 

A  

while  

loop  repeats  a  block  of  instructions  until  a  

test/condition  becomes  false.  

— 

Format:  

while (condition) : instructions   true false condition instructions
(10)

Beware  of  infinite  loops  

—  All  loops  must  eventually  terminate  

—  Something  inside  the  loop  must  eventually  make  the  test  condition   false  

—  If  a  loop  does  not  have  a  way  of  stopping,  it  is  called  an  

infinite  loop  

—  An  infinite  loop  continues  to  repeat  until  the  program  is   interrupted  

—  Infinite  loops  occur  when:  

—  the  programmer  forgets  to  write  code  inside  the  loop  that  

makes  the  test  condition  false.  

—  The  condition  that  controls  the  loop  is  malformed   —  You  should  avoid  writing  infinite  loops  

(11)

Examples  

stars = 1 while ( stars <= 10) : print “*” stars = stars + 1  

#this next loop is an infinite loop stars = 1

while ( stars <= 10) : print “*”

#this is an infinite loop too stars = 1 while ( stars >= 1) : print “*” starts = starts + 1

 

(12)

Wri@ng  the  top  level  func@on  

— 

Our  top  level  function,  

playGame  

is  a  direct  translation  

from  our  outline.  

 

Top-­‐level  function:  

1.  Tell  the  user  how  to  play  the  game.  

2.  Describe  the  room  the  user  is  currently  in.  

3.  Get  the  player’s  command.  

4.  Figure  out  the  next  room  the  user  goes  to.  

5.  Return  to  Step  2,  and  repeat  until  the  user  Quits  

 

(13)

Wri@ng  the  top  level  func@on  

— 

This  top-­‐level  function  makes  sense,  even  without  

knowing  the  lower  level  functions  

(showIntroduction,

showRoom, pickRoom

)  -­‐-­‐  lower  level  function  have  not  

been  written  yet  

 

— 

The  lower-­‐level  functions  are  being  described  in  

terms  of  their  inputs,  outputs,  and  what  they  should  

do  

à

 Abstraction  

 

— 

Once  you  have  written  the  top-­‐level  function,  start  

(14)

Top-­‐Down  Design  then…..  

— 

Makes  it  easier  to  maintain  code  

—  Different  parts  of  a  program  (e.g.  different  functions)  

can  change  without  having  to  change  the  whole   program  

— 

Allows  us  to  

plan

 functions  before  we  write  them  

— 

Allows  us  to  split  the  work  (the  program)  among  

different  programmers  (or  teams)    

—  Follow  the  plan  –  each  programmer/team  works  on  a  

(15)

Top-­‐Down  Design  

playGame()  

showIntro()   ShowRoom()  

showKitchen()   showPorch()   showLR()   showEntryway()   showDR()  

(16)

BoGom-­‐Up  Design  

— 

Start  with  some  idea  of  what  we  want  to  do  (problem  

statement)  

— 

Instead  of  refining  the  problem,  focus  on  building  the  

solution  program  

—  Reuse  code  from  other  programs  as  much  as  possible  

—  Combine  pieces  of  code  from  other  programs  

—  Write  new  pieces  of  code/functions    as  needed  

(17)

Debugging  

—  Debugging:    figuring  out  what  the  program  is  doing,  how  

that  differs  from  what  we  want  it  to  do,  and  how  to  fix  it.    

—  Two  kinds  of  errors:  

—  Syntax  error:  something  is  wrong  with  the  code  itself    

—  e.g.  misspelled  keyword,  spacing  errors,  mismatched  parentheses,  

a  forgotten  colon  

—  The  program  does  not  work  (it  does  not  run)  

—  Python  will  give  you  an  error  message  

—  This  type  of  error  is  easier  to  fix  

—  Logic  error:    something  is  wrong  with  the  logic  of  the  program  

—  The  program  works/runs  but  it  does  not  do  what  it’s  supposed  to  

do  

—  Python  will  not  give  you  an  error  message  

(18)

Tes@ng  your  program

 

— 

Two  main  approaches:  

 

— 

Glass-­‐box  testing:    consider  how  the  program  is  

written  and  test  every  possible  path  through  

the  program  

 

— 

Black-­‐box  testing:    consider  how  the  program  is  

supposed  to  behave  and  how  it  responds  to  

valid  and  invalid  inputs  

— 

Use  a  combination  of  these  two  approaches  to  test  

(19)

Tips  on  Tes@ng  and  Debugging  

— 

Try  both  expected,  and  unexpected  input.  

— 

Learn  to  trace  code  

— 

Add  

print    

and  

printNow  

statements    

—  They  can  help  you  check  the  values  of  variables  used  in  the  

program  or  check  if  you  are  entering  the  functions  

—  You  can  delete  them  later  when  no  longer  needed  

— 

Don’t  be  afraid  to  change  the  program  

—  Use  comments  to  “remove”  parts  temporarily  when  testing.  

— 

Use  the  programming  environment  debugging  tools  

(20)

Seeing  the  Variables:  showVars()  

—  This  function  will  show  

you  all  the  variables  and   their  values  at  the  point   when  it’s  executed  

—  showVars  can  also  be  

used  in  the  command   area  to  see  the  variables   created  there  

(21)

Using  the  Watcher    

—  The  Watcher  allows  you  

to  see  which  lines  of  code   are  being  executed  as  

they  execute  

—  Open  the  Watcher  from  

the  main  menu  in  JES   then  use  the  command   area  as  normal.  

(22)

Using  the  Watcher  

— 

With  the  watcher  you  can:  

—  Pause  execution  of  the  program  

—  Step  through  the  program  from  a  given  point  

—  Stop  execution  

—  Speed  up  /  slow  down  execution  

(23)

Improving  the  Adventure  Game  

— 

When  testing,  we  discover:  

—  It’s  hard  to  tell  which  room  was  which  when  playing  

the  game.  

—  Add  a  printNow  in  the  showRoom  function  to    separate  or  

space  out  one  room  description  form  another  

—  We  can’t  figure  out  what  we  typed  where.  

—  Add  a  printNow  in  the  playGame  function  to  display  back  

what  the  user  typed.  

—  The  user  can  give  an  invalid  input  

—  Add  an  error  message  in  pickRoom  and  return  the  user  to  

(24)

Running  programs  outside  of  JES  

—  Once  you  make  a  larger  program,  you  may  want  to  run  it  in  

Jython  directly.  

—  To  use  the  JES  libraries  in  other  Jython  interpreters,  follow  

these  steps  in  the  command  line  (of  the  Jython  interpreter):  

1.  >>> import sys

2.  Insert  the  JES  sources  into  your  sys.path  

>>>sys.path.insert(0, r“C:\Program Files (x86)\JES 4.3\Sources”)

3.  >>> from media import *

—  In  a  Mac  you  would  follow  these  steps  in  the  command  line  

(of  the  Jython  interpreter):  

>>> import sys

>>> sys.path.insert(0, “/Applications/jes-4-3.app/Contents/Resources/Java/”)

References

Related documents

In relation to social work practice, this discussion has highlighted the need for social workers to be aware that some of the people that they might be supporting might be

He is currently assigned as the Chief of the White Collar Enforcement Group where he supervises 15 attorneys involved in the prosecution of complex fraud cases, including health care

In the case of your home, accentuating the good can mean a faster sale for more money; failing to deal with the bad can mean months on the market and a lower-than-desired sales

 El caràcter seqüencial d’aquest nou sistema es basa en el caràcter autolimitant de la condensació epoxi-amina i en el fet que les dues etapes de curat tenen lloc en

The findings indicated that there was significant difference between the level of exposure of pupils in public and private schools to English at home; the usage of English

Pengetahuan awal mereka dijadikan dasar untuk mengembangkan pembelajaran, hal ini sejalan dengan apa yang dikemukaan oleh Ausubel (dalam Dahar, 1988:134), bahwa

This chapter describes the application of micro scratch technique to determine the fracture properties of the porcine and bovine cortical bone specimens.. Thus,

Therefore, the current study examined the performance effects of emotional stimuli in a working memory task compared to a traditional working memory task in a sample of