Computer Science Canada Proper Format |
Author: | Flea [ Mon Oct 18, 2004 6:22 pm ] |
Post subject: | Proper Format |
I use a lot of procedures in my bigger programs. In fact, my programs are almost entirely procedures with just a little code to call them. I know that "goto" in other languages is really frowned upon by a lot of people (university profs @ mac for example).. but to me, procedures seem the same, since a procedure is (im guessing) just two goto lines with code in between. So my question is, should i keep building my programs like this or will it cause problems later on? I mean i could have like a zillion nested loops in a big mash of code... I know its probably up to my discresion, but which is the proper thing to do? |
Author: | wtd [ Mon Oct 18, 2004 6:55 pm ] | ||||||||
Post subject: | |||||||||
You should rewrite as many of those procedures as possible as functions. But even if you keep the procedures, you should make sure that you don't have any reliance on global variables. Consider something like a program where you want to move something around the screen, and you're keeping track of x and y coordinates. You might have a procedure to move them diagonally, where each coordinate gets increased by 1.
Instead of relying on those variables existing, let's pass in any two variables.
The other major improvement you can make is to learn to use records to tie related pieces of information together. Instead of passing in the x and y coordinates separately, we create a record which holds both of them and lets us treat the two integers together as a single piece of data.
Of course, we can improve on even that, by incorporating classes, where we hide the actual data, and only allow very select access to it. This prevents unintended data corruption.
|
Author: | apomb [ Tue Oct 19, 2004 1:23 pm ] |
Post subject: | |
how is he going to use pointers, turing doesnt support pointers, ... or does it that is a good question, cuz i copied that last bit of wtd's code and it made pointer bold ... hmmm, i never knew turing could use pointers... can someone pleasehep me out with this one. sorry flea for hijacking your thread, but i really want to know if wtd's code would work... cuz theres a couple of errors |
Author: | wtd [ Tue Oct 19, 2004 1:31 pm ] | ||
Post subject: | |||
A very simple example that does work:
|
Author: | Flea [ Tue Oct 19, 2004 4:24 pm ] |
Post subject: | |
That info is extremely useful and easy to understand. You can definately explain things well, wtd, thanks a ton =D even compwiz is learning |
Author: | wtd [ Tue Oct 19, 2004 4:32 pm ] |
Post subject: | |
Flea wrote: That info is extremely useful and easy to understand. You can definately explain things well, wtd, thanks a ton =D
Thank you, and you're welcome. |