How to structure your programming code

I remember my first fumbling with basic on my ZX Spectrum computer in the 1980s, flipping through pages of basic commands and example code with no real idea of ​​how I might 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 turn them into complete sentences to write a document. Any programmer who has dabbled in the basics has probably come across the famous “Hello Word” routine, which consists 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 commands that your choice of programming language understands. It means reading your programming manual to know 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 to print “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 retro fanatic.

10 “Hello World” impressions
20 go to 10

The best structure for writing any program code is to make it clear and easy to follow. Some programmers put multiple commands on one line, which can make it hard to keep track of your code if you’re trying to fix bugs. Spreading your code across multiple lines actually makes the program work better and becomes more readable.

Another best practice is to separate each piece of code in your program using REM statements. REM (short for Remark) allows you to put comments before each section of code to remind you 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
30rem *******
40 rem Print variables on screen
50 rem******
60 impressions A,B

The computer ignores anything after the REM command, and you can use as many REM statements as you like to make 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 program code. Remember that the computer must follow instructions step by step, so you must write each instruction in the order that you want it to be executed.

CODE CONSTRUCTION

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

Read information in arrays: If you have information that you want to put into an array with the DIM command, you can use a For/Next loop and the READ command. It’s best to put the data declarations to be read by the array at the end of your program.

Set the main screen: This is the section where you would use a subroutine (GOSUB Command) to set up the main screen. In a shoot-em-up type game, you would have a routine that draws the game sprites and 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 of the loop.

Schedule Routines: It is a good structure to place all programming routines after the main loop. It would have separate routines that update the screen, check for joystick input, check for collision detection, etc. After each check, it returns to the main loop.

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

CONCLUSION

Creating your code with lots of REM statements 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.

You may also like...

Leave a Reply

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