How to Structure Your Programming Code
I keep in mind my initial fumble with basic on my ZX Spectrum laptop back within the Eighties, plowing through pages of basic commands and example code with none real plan of however I may write programs myself. it absolutely was like reading a lexicon wherever I may learn sure words and their meanings with restricted data on however I may construct them into entire sentences to jot down a document. each software engineer WHO has splashed in basic has most likely bump into the famed "Hello Word" routine that consists of a two-line program that prints this phrase unlimited times on the screen.
Your program code must be written as gradual directions victimization the commands that your selection of artificial language understands. It means that reading your programming manual to find out what commands you wish to use for what you would like your program to try and do. within the "Hello World" example you'd initial would like a command that prints "Hello World" onto the screen, so you'd like a second command to print it once more multiple times, while not writing multiple print statements.
Check out this instance. to form things straightforward I'm victimization 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 straightforward to follow. Some programmers place multiple commands on one line which may create your code troublesome to follow if you're attempting to iron out bugs. Spreading your code over multiple lines truly makes the program work higher and becomes additional clear.
Another counseled observe is to separate every a part of your program code victimization REM Statements. REM (short for Remark) permits you to place comments before every section of code to prompt you what every half will. this can be particularly helpful if you would like 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 would like to form larger gaps in your code for simple reading. alternative programming languages permit you to use blank lines or indent the primary line of the routine.
Now I'll show you ways to structure the complete program code. keep in mind that the pc must follow gradual directions, therefore, you wish to jot down every instruction within the order you would like it to run.
CONSTRUCTION OF CODE
Set up screen resolution and variables: the primary section of your program would set the screen resolution and therefore the variables.
Read data into arrays: If you've got data you would like to place into Associate in Nursing array victimization the DIM command then you'll be able to use a For/Next loop and therefore 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 the main screen: this can be the section wherever you'd use a package (GOSUB Command) to line up the most screen. in a very shoot-em-up kind game you'd have a routine that attracts the sprites and game screen so returns to the successive line of the code it came from.
Main Program Loop: Once the program is up and running the most program loop jumps to varied routines victimization subroutines so returns to the successive line within the loop.
Program Routines: it's smart 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 would like me.
CONCLUSION
Creating the code that you make with lots of REM Statements and short lines makes your code look cleaner and easier to follow. There is also a time you would like to boost the program or use a routine for an additional program. Thanks.
Komentar
Posting Komentar