It was just another ordinary day for Lima. Though unexpectedly, the routine chore of picking apples, will send Lima into a dangerous journey. Labyrinthica: The Quest of Lima is a single-player, melee focused action game. Labyrinthica has similarities to rogue games, but is based on real-time action. You move Lima with the keyboard and aim with the mouse. Unlike similar games, you will need to fight monsters face to face rather than shoot them from a safe distance. You use both your weapon and shield in battle, though using a sword is not always the only way to harm enemies! Potions should be used wisely. Drinking that green potion might prove fatal. It might be poison! And throwing that bronze potion on the boar creature might make it go berserk. Potion colors are randomly assigned to each type. You will have to discover on your own what each potion color does. Labyrinthica awaits to test your wits and reflexes.

Post news Report RSS Copy paste and coding

Why should a programmer avoid copy pasting code. When should he\she do that, and what is the alternative?

Posted by on

Some people(my friend) think that if you can copy and paste some code it means it is a reusable code. That is wrong in two ways. First, reusable code means that the same code can be used in many different projects, it doesn't say how many times you need it in one project. Secondly, as a rule of thumb, copy pasting in programming is a bad thing(sort of).
In this post I will not talk about reusable code, but rather how to avoid copy pasting. There is actually a very important mechanism for copy pasting in code. That is inheritance. Inheritance helps you copying the same code from one class into a new class, without actually having the same code written twice. Lets try to show a code example in C++.

class BaseSpriteDrawer {
public:
void DrawCenter (int x, int y, Texture & t)
{
this->Draw (x-t.GetWidth()/2, y-t.GetHeight()/2, t);
};
virtual void Draw (int x, int y, Texture & t) = 0;
}

class SpriteDrawerA: public BaseSpriteDraw {
public:
void Draw (int x, int y, Texture & t);
};

class SpriteDrawerB: public BaseSpriteDraw {
public:
void Draw (int x, int y, Texture & t);
};

In this example you can see there are two different implementations for drawing sprites. Each one of those have different code for Draw. However, DrawCenter has the same code for both implementations so instead of writing the same code twice for each of the implementations, I have used inheritance. We have "copy pasted" the content of the class BaseSpriteDrawer or "copy pasted" DrawCenter.
The reason copy pasting is bad, is that you have the same code to maintain in two different places. If you discover a bug inside a function, you need to fix all the places you copy pasted that function. You just have more code to maintain, more copies of the same thing to maintain which translates into more work and more bugs.
There is also the additional bonus of saving you a lot of time. Sometimes you need the same code with small variations, and it would seem more work to figure out how to do this with inheritance, instead of just copy pasting and having two copies of the same code. However, in case you need a third copy of the code, with the inherited code it would be a lot easier to create the third "copy" of the code instead of copy pasting the same code for the second time.

If you are a beginner, I suggest you try to avoid copy pasting almost completely. It would be a good exercise or a good challenge to try doing something else each time you have the urge to copy paste some code. That being said, I myself don't completely avoid copy pasting. While as a rule of thumb, its better to avoid copy pasting, sometimes its the fastest immediate solution. The thing is, not all of my code get the same treatment. Some code get better treatment than other code. If its code that is not so important and is not long, then I might copy paste it. I also might copy paste something, and when I see I get back to the same code to add something new, I would fix my bad practice of copy pasting and make a better, cleaner code.

There is a lot more to talk about this subject, and a lot more examples and techniques of how to avoid copy pasting. Copy pasting is actually implied from spartan programming, I have already mentioned spartan programming and I hope to talk about it in future posts.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Follow Report Profile
Icon
Labyrinthica: The quest of lima
Platforms
Windows
Creator
PompiPompi
Engine
Custom
Contact
Send Message
Homepage
Pompipompi.net
Release date
Game watch
Follow
News
Browse
News
New
Post news
Report
Report
Share
Related Games