home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 215 / 215.d81 / t.basics < prev    next >
Text File  |  2022-08-26  |  9KB  |  358 lines

  1. u
  2.     B E G I N N I N G   B A S I C
  3.  
  4.        "The Top-Down Approach"
  5.  
  6.          by Jeffrey L. Jones
  7.  
  8.     You're about to start on an
  9. application program.  It's a phone
  10. list manager.  You type in the first
  11. line:
  12.  
  13. 10 poke53280,0:poke53281,0:print[CLR]
  14.  
  15.  
  16.     Okay, you know what color the
  17. border and background are going to be
  18. but what about the program itself? How
  19. will you create a usable program from
  20. what is essentially a thought? What
  21. I'm about to say might go against some
  22. people's natural urges but the
  23. "top-down" approach is usually the
  24. best (as well as fastest) way to to
  25. create programs.
  26.  
  27.     Top-down means basically creating
  28. the interface (menus) first, then
  29. creating the guts of the program. You
  30. print the menu on the screen, even
  31. have a working highlight bar. If an
  32. item on the menu should have a
  33. sub-menu, get that working, too. So
  34. essentially you start off with a
  35. program that looks finished, shows you
  36. most of its functions, but doesn't
  37. work. In my book that's one level
  38. better than starting off with a
  39. program that DOESN'T look finished and
  40. doesn't work to boot. This gives you
  41. an animated outline of the finished
  42. product. With the main menu working,
  43. at least you can visualize how the
  44. program will work and not leave
  45. anything out.
  46.  
  47.     Imagine building the elevators and
  48. cafeteria before the building. At
  49. least if you wait till the building is
  50. erected and floors assigned, you'll
  51. know how many buttons to fit into the
  52. elevators, and which floors should be
  53. skipped. The old horse before the cart
  54. syndrome is similar to creating a
  55. program before creating its interface
  56. with the user.
  57.  
  58.     Filling in your hollow program
  59. becomes a relative breeze. All you do
  60. is "fill in" the features, starting
  61. from the top of your menu and working
  62. your way down. Programming this way,
  63. you'll discover that the program
  64. almost writes itself for you.
  65. Additionally your code will
  66. automatically become sectioned and
  67. easily modifiable. You're
  68. sub-routining and even writing
  69. structured code without having to
  70. think about it. You may also find that
  71. in the process of filling in features,
  72. you're creating routines that other
  73. features can use to get their jobs
  74. done.
  75.  
  76.    -----------------------------
  77.     PITFALLS TO SUBROUTINING
  78.    -----------------------------
  79.  
  80.     While on the subject of
  81. subroutining, three commonly wasteful
  82. practices used by many programmers
  83. are:
  84.  
  85.     a. Clearing lines on the screen
  86.     b. Plotting the cursor
  87.     c. Overcoming line link problems
  88.  
  89.     Probably no one sees as much of
  90. other people's code as we do. The
  91. aforementioned tasks commonly cause
  92. budding programmers to make the
  93. mistake of defining large strings over
  94. and over again, which eventually take
  95. their toll through garbage collection.
  96. Garbage collection causes your
  97. computer to pause while it cleans up
  98. the mess that the programmer has made
  99. of memory. Garbage collection is
  100. almost always the programmer's fault.
  101.  
  102.     These pitfalls also take their
  103. toll by making programs larger. I'm
  104. not talking a little larger -- I'm
  105. talking about programs that we've
  106. published at 60-blocks that were 110
  107. when we received them.
  108.  
  109.   To clear a line on the screen,
  110. please DON'T define 40 spaces and
  111. print them. Instead:
  112.  
  113.   POKE 781,line:SYS 59903
  114.  
  115.   In assembly language:
  116.  
  117.   LDX #line
  118.   JSR $E9FF    ;59903
  119.  
  120.     To clear a range of lines, do it
  121. in a FOR NEXT loop.
  122.  
  123.     To plot the cursor on a particular
  124. row and column, just do the following
  125. in BASIC:
  126.  
  127.   POKE214,row:POKE211col:SYS 58732
  128.  
  129.     That is the most efficient way
  130. for BASIC.  In assembly language the
  131. KERNAL PLOT routine is better
  132. suited:
  133.  
  134.   LDX #row
  135.   LDY #column
  136.   CLC
  137.   JSR $FFF0
  138.  
  139.     To TAB backwards:
  140.  
  141.   POKE 211,column:SYS 58732
  142.  
  143.     In assembly language:
  144.  
  145.   LDA #column
  146.   STA 211
  147.   JSR 58732    ;$E56C
  148.  
  149.     Lastly, line links are a major
  150. cause of bad code. Because lines on
  151. your screen can bcome "linked", you
  152. may have fewer than 25 logical lines
  153. on your screen (as few as 13). Your
  154. screen editor links lines because
  155. when you're typing in programs, one
  156. line may wrap around.  So the
  157. computer has to "link" the two lines
  158. that the code takes up and consider
  159. them one line. This is a neccesary
  160. evil, while you're coding, but it
  161. becomes a hassle when the editor
  162. continues to link lines when it
  163. doesn't have a need to (while your
  164. program is running). In general, any
  165. time you print to the 39th column,
  166. lines are linked. If the line
  167. immediately beneath the line to be
  168. linked is already linked, that
  169. section of the screen is scrolled
  170. down.
  171.  
  172.     So you know that you want to move
  173. to line 12, but every time you try to
  174. plot the cursor there, it lands on
  175. line 15. The only way to get the
  176. cursor where you want seems to be
  177. physically printing HOME and a series
  178. of CRSR DOWNS. This wasteful tactic
  179. is usually compounded by X number of
  180. CRSR RIGHTS to the desired column
  181. position.
  182.  
  183.     The logical solution to the line
  184. link problem would be to unlink the
  185. lines. This can be done in a FOR
  186. NEXT loop:
  187.  
  188.   FORI=217TO242:POKEI,PEEK(I)OR128:
  189. NEXT:RETURN
  190.  
  191.     In assembly language:
  192.  
  193.   LDY #24
  194.   LINKS LDA 217,Y
  195.   ORA #128
  196.   STA 217,Y
  197.   DEY
  198.   BPL LINKS
  199.   RTS
  200.  
  201.     Note that I made both versions
  202. into subroutines since you may need
  203. them many times.
  204.  
  205.     Main menus are usually the cause
  206. of linked lines. By the time you've
  207. gotten it presented, you've printed
  208. to the 39th column more than a few
  209. times. So right after you finish
  210. your main menu (or any major revision
  211. to the screen) you should clear your
  212. line links by GOSUBing to a subroutine
  213. that does it. Or even better: Don't
  214. print to the 39th column.
  215.  
  216.     I mention these programming
  217. tactics because they'll make your
  218. programs shorter and more efficient.
  219. Moreover, you'll find that your
  220. programs act as you expect them to.
  221. Bugs are a cinch to fix.  When
  222. anything goes wrong, you know what to
  223. change.
  224.  
  225.  -----------------------------------
  226.  B A S I C   A N D   T O P - D O W N
  227.  -----------------------------------
  228.  
  229.     One BASIC command invaluable to
  230. top-down programming is ON
  231. GOTO/GOSUB.
  232.  
  233.     ON GOTO is a highly under-used
  234. but useful command. It not only
  235. gives you the power to dispatch
  236. control of a program to many lines
  237. with one command, but also offers
  238. extended IF THEN logic if you know
  239. how to use fancy formulae (BASICS
  240. #66).
  241.  
  242.     When used normally, you shove a
  243. value (n) at ON GOTO and it turns
  244. over program control to the nth
  245. line:
  246.  
  247. 10 GET A$:IF A$<"1" OR A$>"5"THEN 10
  248.  
  249. 20 ON VAL(A$)GOTO 150,160,170,180,190
  250.  
  251.  
  252.     On line 20 the program can GOTO
  253. any of 5 lines. Line 10 makes sure
  254. that a value between 1 and 5 is
  255. given.  If the value is 1 then the
  256. program will branch to line 150.  If
  257. the value is 3 the program will
  258. branch to line 170 (or item 3 in the
  259. list).
  260.  
  261.     If somehow A$ gets to this GOTO
  262. and is less than 1 or more than 5
  263. then the GOTO is skipped.
  264.  
  265.     It's rather simple. If you're
  266. skimming past this section because ON
  267. GOTO confuses you, don't give up.
  268. Read it again.
  269.  
  270.     ON GOTO is your alternative to:
  271.  
  272. 10 GET A$:IF A$<"1" OR A$>"5"THEN 10
  273.  
  274. 20 IFA$="1"THEN150
  275.  
  276. 30 IFA$="2"THEN160
  277.  
  278. 40 IFA$="3"THEN170
  279.  
  280. 50 IFA$="4"THEN180
  281.  
  282. 60 IFA$="5"THEN190
  283.  
  284.  
  285.     Imagine if you had 10 branches!
  286.  
  287.     Note that ON GOSUB works the same
  288. as ON GOTO:
  289.  
  290. 10 GET A$:IF A$<"1" OR A$>"5"THEN 10
  291.  
  292. 20 ON VAL(A$)GOSUB 150,160,170,180,190
  293.  
  294. 30 GOTO 10
  295.  
  296.     No matter which line is branched
  297. to, the program will RETURN to line
  298. 20 and execute any further
  299. instructions on that line (if any).
  300. In this case there are no further
  301. instructions and the program moves on
  302. to line 30. Again, imagine this
  303. routine without ON GOSUB:
  304.  
  305. 10 GET A$:IF A$<"1" OR A$>"5"THEN 10
  306.  
  307. 20 IFA$="1"THEN GOSUB 150:GOTO 10
  308.  
  309. 30 IFA$="2"THEN GOSUB 160:GOTO 10
  310.  
  311. 40 IFA$="3"THEN GOSUB 170:GOTO 10
  312.  
  313. 50 IFA$="4"THEN GOSUB 180:GOTO 10
  314.  
  315. 60 IFA$="5"THEN GOSUB 190:GOTO 10
  316.  
  317.     I'd call it an improvement. And
  318. if you're using a good RENUMBER
  319. routine, it should have no problem
  320. renumbering your ON GOTO lines.
  321.  
  322.     Programs like INSTANT MENU,
  323. published on this issue, make the
  324. task of top-down programming quite
  325. simple. Learn to use it and you'll
  326. save yourself many a headache. You'll
  327. also find yourself writing better,
  328. more structured code.
  329.  
  330. JLJ
  331.  
  332.  
  333.  AFTER-NOTE: I have just one caveat
  334. with Jeff's "Top Down" Scheme. The
  335. first step for me is getting the most
  336. elemental code of the progr