SSJX.CO.UK
Browser Games | Cybiko | Amiga | Applications | Windows Games | Python | Guides | Site Map

Programming - Best Practices

This guide will give you tips and ideas on how to improve your programs! Note that any example code is demostrated in the language that it is easiest to highlight the issue with, the issue still applies to any language. They are probably subjective and I do not always follow them, but here you go!

Enable Any Warnings/Strict Modes That Are Available

You will make mistakes, so it is better the compiler/interpreter catches them early before they turn into problems. Enabling any strict modes will force you to write better code, e.g.:

Most compilers have switches to enable more thorough checking too. Use them.

Use Array Lengths In Loops

The following is in javascript to more clearly show the problem. The block below (and the equivalent in other languages) will happily run and compile. The problem is, if you go back and change the array size, you risk trashing memory and making your program unstable. Depending on how this is triggered this could be used in various attacks.

var score=new array(10);

for(var i=0;i<10;i++)
{
// do something
}

A better way is to get the size of the array and use that:

var score=new array(10);

for(var i=0;i<score.length;i++)
{
// do something
}

Using strict modes and compiler warnings, this type of over/underflow error are usually caught but the above is still worth doing.

Try To Avoid Unnecessary Type Conversions

This is probably best illustrated below:

dim as string one="1"
dim as integer two=2
dim as integer total

total=int(one)+two

Nothing wrong with the program, but it is wasteful converting a string to an integer when it should have been and integer to begin with.

Pre-Calculate / Reordering Of Any Complex Maths Within Loops

The code below fills and array (could be a screen or bitmap). The bit of interest is the (10*j)+i.

for j=0 to 9
	for i=0 to 9
		screen[(10*j)+i]=0
	next i
next j

As 'j' does not change inside the inner loop, part of the equation can be removed from it as shown below. This does add another variable (called start in the example) but that is usually a lower cost than a multply.

for j=0 to 9
	start=(10*j)
	for i=0 to 9
		screen[start+i]=0
	next i
next j

Between the two code blocks we have gone from having 100 multiply's to just 10!

Use Sizes That Work For Shift Instructions

In the above example we have the line start=(10*j) to read/write to our 10x10 grid. Using a better size would enable us to use a Shift instruction instead which are usually much faster.

If we used a 16x16 map, we could use a shift left of 4 instead of the multiply:

for j=0 to 15
	start=(j<<4)
	for i=0 to 15
		screen[start+i]=0
	next i
next j

Note that if the larger 16x16 size is allocated for a map, the actual used size could still be 10x10.

Choose The Best Data Type

Our latest game will use an array to store its map:

// Space for a 256 x 256 tile map 
const w=256,h=256
dim as integer map(w*h)

Again, nothing wrong with the above, it allocates 64KB for the map. However, if we know that each value will be less than 255 the code below will use a quarter of the memory just by changing its type:

// Space for a 256 x 256 tile map 
const w=256,h=256
dim as byte map(w*h)

For reasons of speed and processor optimisation, the first example may be best, but it is always worth checking.

Re-Inventing The Wheel

Sometimes it is worth re-inventing if the alternatives are too complex, too big or worse than making it yourself. Other times it is not:

Standard rectangle within a rectangle collision function (in ActionScript / Flex):

if (swans.x>=player.x && (swans.x<=(player.x+player.w))
{
	if (swans.y>=player.y && (swans.y<=(player.y+player.h))
	{
	
	}
}
				
// x4 for each corner....

Or we could just use the existing function which is less typing and most likely much faster:

swans.rectangle(10,10,100,100).intersects(player.rectangle(10,10,100,100) )

The point being that it is worth checking if the language already has a function for what you are doing. Java and ActionScript have a huge number of methods as standard which may include something you are currently re-inventing.

Do You Really Need Large Dependancies?

Both programs below accomplish the same thing:

// Java Hello World
public class HelloWorld { 
   public static void main(String[] args) { 
      System.out.println("Hello World!");
   }
}
// C Hello World
#include <stdio.h>

void main()
{
	printf("Hello World");
}

The key difference being the Java version has a massive dependancy on requiring the Java Runtime Environment. That's a 300MB install for what is a 2KB native program. There are many good reasons to use Java (portability / features), this is not one of them.

Other oversized dependancies include various database systems, does your project really need MS/MySQL? If you are just dealing with a small number of records then maybe something like SQLite would do instead. Or maybe just use the languages standard file methods?

If you need to include the dependancy with your program, it can lead to the next issue...

Bandwidth Is Not Free!

Slight tangent from programming... On this site, some of the popular(!) downloads are offered as 7Zip files. These are compressed archive files much like Zip file, the difference being that they are even smaller. Animator is a 714KB zip file and a 499KB 7zip file, that makes the 7zip about a third smaller.

Although hosting two archives takes up more space on the site, more people will appreciate the quicker download. Those with download limits will prefer a smaller download even more. Although some people may not have the 7zip program installed, for large programs the 1MB 7zip installer could save a much bigger download. There is also the option of a 7zip self extracting file.

It should be noted that there are alternatives to 7zip, I use that as an example as it is the one that I use.

This same thinking can be applied to websites:

10/04/2017

Play-Asia.com - Buy Games & Codes for PS4, PS3, Xbox 360, Xbox One, Wii U and PC / Mac.
Support this site : Visit Play-Asia for games and accessories for new and classic games consoles!