Structure Your Programming Code

How to Structure Your Programming Code

I bear in mind my first fumble with simple on my ZX Spectrum laptop again withinside the 1980s, plowing thru pages of simple instructions and instance code with none actual concept of ways I may want to write applications myself. It changed into like analyzing a dictionary in which I may want to research positive phrases and their meanings with constrained records on how I may want to assemble them into complete sentences to put in writing a document. Every programmer who has dabbled in simple has possibly come upon the famous “Hello Word” habitual which includes a two-line software that prints this word limitless instances at the display.

Check out this instance. To make matters easy I am the usage of old-college simple with line numbers – possibly due to the fact I’m a retro-freak.
10 print “Hello World”
20 goto 10
The first-rate shape for writing any software code is to make it clean and clean to observe. Some programmers placed a couple of instructions on one line that may make your code hard to observe if you are attempting to iron out bugs. Spreading your code over a couple of traces virtually makes this system paintings higher and turns into greater readable.
Another advocated exercise is to split every part of your software code into the usage of REM Statements. REM (brief for Remark) lets you position feedback earlier than every segment of code to remind you what every component does. This is particularly beneficial if you want to edit your code at a later date.

10 rem Set Up Variables
20 allow A=1: allow B=2
30 rem *******
forty rem Print Variables to Screen
50 rem *******
60 print A, B

Anything after the REM command is omitted via way of means of the laptop and you could use as many REM statements as you need to expand gaps to your code for clean analysis. Other programming languages permit you to use clean traces or indent the primary line of the habitual.
Now I will display to you a way to shape the complete software code. Remember that the laptop wishes to observe step-via way of means of-step commands so that you want to put in writing every coaching withinside the order you need it to run.


Set up display decision and variables: The first segment of your software could set the display decision and the variables.
Read records into arrays: If you’ve got records you need to position into an array the usage of the DIM command then you could use a For/Next loop and the READ command. It is first-rate to an area the records statements for the array to study from on the cease of your software.
Set up fundamental display: This is the segment in which you’ll use a subroutine (GOSUB Command) to installation the primary display. In a shoot-em-up kind recreation you’ll have a habitual that attracts the sprites and recreation display after which returns to the following line of the code it got here from.

Main Program Loop

Once this system is up and walking the primary software loop jumps to diverse exercises the usage of subroutines after which returns to the following line withinside the loop.
Program Routines: It is the ideal shape to area all of the programming exercises after the primary loop. You could have separate exercises that replace the display, take a look at for joystick input, take a look at for collision detection, and so on. After everyone takes a look at you come to the primary loop.
Data Statements: Finally you could listing all of the records statements on the cease of this system which makes it less difficult to discover and accurate if want be.

Creating your code with masses of REM Statements and brief traces makes your code appearance cleanser and less difficult to observe. There can be a time you need to enhance this system or use a habitual for some other software.