How to Structure Your Programming Code

I keep in mind my 1st fumble with basic on my ZX Spectrum laptop back within the Eighties, tillingthrough pages of basic commands and example code with none real plan of however I may write programs myself. it had been like reading a wordbook wherever I may learn sure words and their meanings with restricted info on however I may construct them into entire sentences to jot down a document. each technologist UN agency has splattered in basic has most likely encounter the notable"Hello Word" routine that consists of a two-line program that prints this phrase unlimited times on the screen.
Your program code has to be written as in small stages directions victimisation the commands that your selection of programing language understands. It means that reading your programming manual to be told that commands you would like to use for what you wish your program to try to to. within the "Hello World" example you'd 1st want a command that prints "Hello World" onto the screen, then you'd want a second command to print it once more multiple times, while not writing multiple print statements.
Check out this instance. to form things easy i'm victimisation old-school basic with line numbers - most likely as a result of i am a retro-freak.
10 print "Hello World"
20 goto 10
The best structure for writing any program code is to form it clear and simple to follow. Some programmers place multiple commands on one line which may build your code troublesome to follow if you're making an attempt to iron out bugs. Spreading your code over multiple lines truly makes the program work higher and becomes a lot of decipherable.
Another counseled apply is to separate every a part of your program code victimisation REM Statements. REM (short for Remark) permits you to place comments before every section of code to prompt you what every half will. this is often particularly helpful if you want to edit your code at a later date.
10 rem created Variables
20 let A=1: let B=2
30 rem *******
40 rem Print Variables to Screen
50 rem *******
60 print A,B
Anything when the REM command is neglected by the pc and you'll be able to use as several REM statements as you wish to form larger gaps in your code for simple reading. different programming languages permit you to use blank lines or indent the primary line of the routine.
Now i will be able to show you ways to structure the complete program code. keep in mind that the pchas to follow in small stages directions therefore you would like to jot down every instruction within theorder you wish it to run.
CONSTRUCTION OF CODE
Set up screen resolution and variables: the primary section of your program would set the screen resolution and also the variables.
Read info into arrays: If you've got info you wish to place into associate degree array victimisation the DIM command then you'll be able to use a For/Next loop and also the browse command. it's best to position the info statements for the array to browse from at the tip of your program.
Set up main screen: this is often the section wherever you'd use a software package (GOSUB Command) to line up the most screen. in a very shoot-em-up sort game you'd have a routine that attracts the sprites and game screen then returns to subsequent line of the code it came from.
Main Program Loop: Once the program is up and running the most program loop jumps to variedroutines victimisation subroutines then returns to subsequent line within the loop.
Program Routines: it's sensible structure to position all the programming routines when the most loop. you'd have separate routines that update the screen, check for joystick input, check for collision detection so on. when every check you come back to the most loop.
Data Statements: Finally you'll be able to list all the info statements at the tip of the program that makes it easier to search out and proper if want be.
CONCLUSION
Creating your code with many REM Statements and short lines makes your code look cleaner and easier to follow. There is also a time you wish to enhance the program or use a routine for an additional program.
No comments