How to structure your programming code

I remember my first stumble with Basic on my ZX Spectrum computer in the 1980s, as I was browsing through basic command pages and example code without any real idea of ​​how to write programs myself. It was like reading a dictionary where I could learn some words and their meanings with limited information on how to compose them in complete sentences for writing a document. Perhaps every programmer who has basic software encounters the popular "Hello Word" routine consisting of a two-line program that prints this phrase unlimited times on the screen.

Your program code should be written as step-by-step instructions using the commands that your choice of programming language understands. It means reading your programming guide to find out what commands you need to use for what you want the program to do. In the example of "Hello World", you will first need a command that prints "Hello World" on the screen, then you will need a second command to print it again several times, without writing multiple print data.

Check out this example. To simplify things, I'm using old-school basics with line numbers – perhaps because I'm weird.

10 Hello World Edition
20 goto 10

The best structure for writing any program code is to make it clear and easy to follow. Some programmers place multiple commands on one line which makes it difficult to follow your code if you are trying to get rid of errors. Spreading the code across multiple lines makes the program work better and becomes more readable.

Another recommended practice is to separate each part of your program code using REM data. REM (Short for Remark) lets you put comments before each section of code to remind you of what each part does. This is especially useful if you want to modify the code at a later time.

10 rem Prepare variables
20 Let A = 1: Let B = 2
30 rem *******
40 in-kind print variables to screen
50 rem *******
60 Publication A, B.

Nothing after the REM command is ignored by the computer, you can use as many REM statements as you want, and you want to create more holes in your code for ease of reading. Other programming languages ​​allow you to use blank lines or indentation for the first line of the routine.

Now I'm going to show you how to structure the entire program icon. Remember that your computer needs to follow the step-by-step instructions, so you need to write each instruction in the order you want it to run.

Build the code

Setting screen resolution and variables: The first section of the program will set the screen resolution and variables.

Read the information in the matrices: If you have information you want to put in an array using the DIM command, you can use the For / Next loop and the READ command. It is better to put the data data of the array to be read from at the end of the program.

Home screen setup: This is the section where you will use a subroutine (GOSUB Command) to set up the home screen. In shoot type game, you will have a routine that draws sprites and game screen and then returns to the next line of code that came from it.

Main program episode: Once the program is launched and the main program loop is launched, it moves to multiple procedures using a subroutine and then returns to the next line in the loop.

Program actions: It is a good architecture to place all programming procedures after the main loop. You will have separate actions updating the screen, checking the joystick entry, collision detection etc. After each check you return to the main ring.

Data data: Finally, you can list all data data at the end of the program making it easier to find and correct it if necessary.

conclusion

Create your own code with lots of REM phrases and short lines makes your code look cleaner and easier to follow. There may be a time when you want to improve the program or use a routine for another program.

&

    Leave Your Comment

    Your email address will not be published.*