How to Structure Your Programming Code - EXPERTLENSE

Friday, 15 March 2019

How to Structure Your Programming Code

i bear in mind my first fumble with fundamental on my zx spectrum computer again in the Nineteen Eighties, ploughing through pages of simple commands and instance code with none real concept of ways i may want to write programs myself. it changed into like studying a dictionary wherein i could learn certain phrases and their meanings with constrained facts on how i may want to construct them into entire sentences to write a record. every programmer who has dabbled in fundamental has in all likelihood come across the well-known "good day word" recurring which consists of a two-line program that prints this word unlimited times at the screen.

your program code wishes to be written as step-by means of-step instructions the usage of the commands that your desire of programming language is familiar with. it approach analyzing your programming guide to analyze which commands you need to apply for what you want your program to do. inside the "whats up global" example you would first want a command that prints "hello international" onto the screen, and then you definitely could want a 2nd command to print it again more than one instances, with out writing a couple of print statements.

take a look at out this situation. to make matters easy i am using vintage-school primary with line numbers - probable due to the fact i am a retro-freak.

10 print "howdy global"
20 goto 10

the great shape for writing any software code is to make it clear and easy to comply with. some programmers placed more than one commands on one line which can make your code difficult to comply with if you are attempting to iron out insects. spreading your code over multiple strains in reality makes this system paintings better and becomes greater readable.

some other encouraged practice is to split every a part of your program code the usage of rem statements. rem (short for observation) lets in you to place remarks earlier than every segment of code to remind you what every element does. this is especially useful if you wish to edit your code at a later date.

10 rem installation variables
20 permit a=1: permit b=2
30 rem *******
forty rem print variables to screen
50 rem *******
60 print a,b

some thing after the rem command is not noted by means of the pc and you may use as many rem statements as you need to increase gaps in your code for smooth analyzing. different programming languages will let you use clean lines or indent the primary line of the habitual.

now i will show you how to structure the entire program code. keep in mind that the computer desires to follow step-with the aid of-step instructions so that you want to write each preparation inside the order you want it to run.

production of code

installation display screen decision and variables: the first phase of your program might set the display resolution and the variables.

read records into arrays: if you have information you want to put into an array the usage of the dim command then you could use a for/subsequent loop and the read command. it is quality to vicinity the information statements for the array to read from at the give up of your application.

installation fundamental display: this is the section wherein you would use a subroutine (gosub command) to installation the principle screen. in a shoot-em-up type game you would have a recurring that attracts the sprites and game display screen and then returns to the following line of the code it came from.

most important program loop: as soon as the program is up and walking the primary application loop jumps to numerous workouts using subroutines and then returns to the subsequent line in the loop.

application workouts: it is good structure to vicinity all the programming routines after the main loop. you will have separate workouts that replace the display, test for joystick input, check for collision detection and so on. after every test you come to the main loop.

statistics statements: eventually you could list all of the facts statements at the cease of this system which makes it simpler to discover and correct if want be.

conclusion

growing your code with lots of rem statements and brief strains makes your code look cleaner and less complicated to observe. there may be a time you want to enhance this system or use a recurring for some other application.

No comments:

Post a Comment