uterti-com

Just another WordPress site

How to structure your programming code

I remember my first mistake with the basics on my ZX Spectrum computer in the 1980s, flipping through pages of basic commands and example code without having a real idea of ​​how I could write programs myself. It was like reading a dictionary where you could learn certain words and their meanings with limited information on how you could construct them into complete sentences to write a document. Every programmer who has dabbled in the basics has probably come across the famous “Hello Word” routine, which is a two-line program that prints this phrase on the screen an unlimited number of times.

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 manual to learn what commands you need to use for what you want your program to do. In the “Hello World” example, you would first need a command that prints “Hello World” to the screen, and then you would need a second command to print it again multiple times, without writing multiple print statements.

Look at this example. To keep things simple, I’m using old-school basics with line numbers, probably because I’m a fan of retro.

10 print “Hello world”
20 go to 10

The best structure for writing any program code is to keep it clear and easy to follow. Some programmers put multiple commands on one line, which can make your code difficult to follow if you are trying to troubleshoot errors. Spreading your code over multiple lines makes the program work better and becomes more readable.

Another best practice is to separate each part of your program code using REM statements. REM (short for Remark) allows you to put comments before each section of code to remind you of what each part does. This is especially useful if you want to edit your code at a later date.

10 rem configuration variables
20 let A = 1: let B = 2
30 rem *******
40 on-screen remote printing variables
50 rem *******
60 printing A, B

Anything after the REM command is ignored by the computer and you can use as many REM statements as you like to create larger spaces in your code for easier reading. Other programming languages ​​allow you to use blank lines or indent the first line of the routine.

Now I will show you how to structure all the code of the program. Remember that the computer must follow step-by-step instructions, so you must write each instruction in the order you want it to be executed.

CONSTRUCTION OF CODE

Set the screen resolution and variables: The first section of your program would set the screen resolution and variables.

Read information in matrices: If you have information that you want to put into an array using the DIM command, then you can use a For / Next loop and the READ command. It is better to place the data declarations for the array to read at the end of your program.

Configure the main screen: This is the section where you would use a subroutine (GOSUB command) to configure the main screen. In a shoot-em-up type game, you would have a routine that draws the sprites and the game screen and then returns to the next line of code it came from.

Main program loop: Once the program is running, the main program loop jumps to various routines using subroutines and then returns to the next line in the loop.

Program routines: It is a good structure to place all programming routines after the main loop. You would have separate routines that update the screen, check joystick input, check for collision detection, and so on. After each verification, it returns to the main loop.

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

CONCLUSION

Creating your code with lots of REM statements and short lines makes your code look cleaner and easier to follow. At some point you may want to improve the program or use a routine for another program.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *