Easy Way To Enchance Your Coding

Lazy people do this: They get rid of redundant code. Only those who want to code same things over and over don’t do this.

It’s easy to copy & paste a block of code from one place to another “because you are in the hurry” or think it will be “harmless”.

Listen very carefully: Don’t copy & paste code blocks and start getting rid of redundant code. Reuse and exploitation of previous librariers and source code is fine, but too often developers copy their own code without thinking the consequences. Common problems with redundant code are:
– When updating your code, you need to update several places which takes more time
– When updating your original code you forget to update the other places. This causes visible or hidden bugs which take more time to fix
– When adding more elements into game it’s harder to do and takes time and might cause errors.

If you are about to copy & paste code, please don’t. Instead – create a function or a method to handle that. If you have variables like var1 and var2 replace them with an array. If you are not familiar with programming arrays or functions then you might want to check their definitions from the Internet. There are lots of places where you can go to check out these.

Here’s a small example – let’s assume you have made a block of code that checks out different items :

if (item1 == “sword”) { …
} else if (item1 == “shield”) { …
} else if (item2 == “bow”) { …
if (item2 == “sword”) { …
} else if (item2 == “shield”) { …
} else if (item2 == “bow”) { …

First you could get rid of redundant code by the use of functions.

updateItem(item1) ;

function updateItem(item) {
if (item == “sword”) { …
} else if (item == “shield”) { …
} else if (item == “bow”) { …

Then you might want to get rid of those unnecessary variables item1 and item2. You can do this by creating an array containing the values: (this might not look so critical now, but if you want to add item3 later in the game you don’t need to go through all your code and add function call updateItem(item3) anymore. This is very helpful in the future.)

items[] = array(); // defined somewhere in your code
for (i=0; i < items .length; i++) { updateItem(items[i]) ; } function updateItem(item) { if (item == "sword") { ... } else if (item == "shield") { ... } else if (item == "bow") { ... } }

There you go, nice and clean. This is very simple way to program, but it will do a massive aid in the future – especially if you plan to code sequels or add-ons for your game.

Conclusions: Use functions/methods to get rid of redundant code. If you have redundant variables, use arrays to make coding easier. Avoid copy & pasting code blocks.

Juuso Hietalahti


  1. Actually you should avoid putting more code into loops.
    When you use functions inside a loop, make at least sure they are declared as inline, else the copy/paste style of coding would be better, as it consumes less CPU cycles.

  2. @Srekel: Yes, I think that would be an accurate statement. Hopefully they learn from here – or from other source. And, even experienced coders can be reminded to get rid of redundant code. Agile methods speak about this as well.

    …I might even bet that you have some redundant code lying in your programs? ;)

  3. Seriously, doesn’t everyone except beginners know about functions and their use?

  4. Ya, it’s a better way :

    class CItem
    virtual void Update() = 0; // pure virtual

    class CSword : public CItem
    void Update(); // sword update

    class CBow : public CItem
    void Update(); // bow update

    list list;
    list.add(new CSword);
    list.add(new CBow);

    for (int i=0; iUpdate();

Comments are closed.