bookmark_borderOverriding and methods

Today I had a little challenge. I had a Class, I call it Class A. It has a method, i call it “doSomething”. Class B extends Class A, and overrides this method with a totally new behavior. Class C who extends from Class B wants to have the original behavior from Class A.

Here is a picture:

The Problem

Now, the simplest but most stupid way to solve this is using copy / paste. Yes, you simply copy the contents of the method of Class A and paste it into the method of Class C and you’re all set right?

Wrong!

It might work for a while, but one of the first things you *should* be feel itchy about, is code duplication! (along with its problems you get when you want to maintain your code).

So what now?

Solution : Use method for shared behavior
Solution : Use method for shared behavior

Well, when you took the easy road and duplicated your code, you probably wanted to get rid of that duplicate code immidiatly… And how do you do that? .. Yes,  you create a new method which is put in Class A , and accessible from Class C and Bam! Code Duplication gone, and you got what you want…

Another way is to re-think your class hierarchy. You might want to consider to do this:

 

Solution : Change in hierarchy, extend C from A, B from C
Solution : Change in hierarchy, extend C from A, B from C

 

 

Yes, you’ve seen it right. Perhaps you can swap Class B and C.  So C extends now from A, and B from C.

But, if you do that, be careful. You need to know exactly what kind of behavior you wanted in Class B. Most likely you have changed that now by extending from C. Take a good look at what methods B was originally calling from Class A, and if it now calls an overridden method by Class C.

These 2 solutions came up today. For my particular problem I’ve used the first. (no, not copy paste smart ass)

I think the first solution is the easiest, I would not recommend anyone to do the second solution unless you really know what you are doing. If you encounter more of these problems like above, swapping might be better for you.  

Do you got another (better?) solution? Let me know!

bookmark_borderGame Programming (Experience)

Recently I saw at Youtube someone telling about his view on game programming. His targetted audience was for those who want to create games, but have no clue how to start. He began with something along the line of “I’ve been programming games for 5 years, so I guess I could say I’m experienced“.

That got me thinking. How long have I been programming (for) games?

I’ve been working on Arrakis since I was around 14/15 years old. I’ve finished it when I was 18. Arrakis is written in Basic (using Quick Basic 4.5). I realized in order to get further I had to learn a new programming language. One that did not had the limitations as Basic. Thats when I had decided to learn C. My first attempt was writing a new Dune II clone, this time really using the Dune II graphics.

After a short period of learning C I began playing around with the Half-Life SDK; I’ve been toying around with a bot framework (Botman’s bot) and before I knew it, RealBot was born. RealBot has been developed for around 4 years.

And, to complete the circle; while RealBot was fading away I picked up again my Dune mania and recreated Dune II – The Maker from scratch. 

Today, I am 26 years old. I haven’t worked on Dune II – The Maker seriously since 2006. This would mean I would have rougly around 7 to 8 years “experience”.  So considering that, I might also say that I have experience in game programming.

When you look at my LinkedIn, you see none of those projects counted as experience. Sure, they are mentioned
under “websites”. But I would prefer to put them in a more suited spot. Perhaps somebody has a suggestion for that.

Although ~ 8 years might sound like a lot. I do think it has to taken with a grain of salt.

First, I did not work 8 hours a day, 5 days a week.
Secondly, I did not have any sparring partners,  so quality wise I did not write superb code.

However, there are concepts that I’ve learned; and you only learn those when you write games. Concepts like:

  • sprite/bitmap management
  • drawing maps / tile engine
  • A*
  • parsing files (from original game)
  • little AI, 
  • state machines, 
  • double buffering
  • blending techniques
  • team colouring (in RTS)
  • using colorkey’s
  • transparancy
  • palette manipulation

And probably a few other things I can’t recall at this moment.

So yeah, perhaps I could say I do have experience in Game programming…

bookmark_borderEclipse IDE for C++

I’ve been working with Eclipse for at least 2 years. When I just learned of Eclipse I was mostly programming C / C++ using Visual Studio 6, which I bought zillion years ago.

Eclipse was a whole new world to me, offering all the convenient tools (like finding references, go to class, etc) a Java programmer could need. 

All my hobby projects are mostly written in C / C++ using the old Visual Studio version. It worked well, but after getting used to Eclipse, going back to Visual Studio 6 was like going back to the stone age. Of course, at this time Visual Studi0 7 is already out-dated; If I’m not mistaken, version 9 or 10 is around the corner.

Good news though, there is an Eclipse version where you can develop in C / C++ using the Eclipse IDE. It is available here. You have to use it in combination with a compiler. To get started, I have used MinGW which is easy to set up. Grab that from here

The good thing is, once MinGW was set up correctly, Eclipse automatigically found it (I bet it used the PATH variable) and you could try to compile your first “Hello world” program. 

My first impressions are good, the C++ IDE is actually like the Java variant with all the handy tools.  The performance is good, it works good, It has Doxygen built in for you. All in all I am quite satisfied. Also, I am quite happy that it exists, since Dev-Cpp and Code::Blocks is not being updated. I thought Dev-Cpp had a lot of potential, especially with the built-in packages.

If you ever consider to use an IDE (you don’t need it!) for writing C / C++ programs, you should definately give the Eclipse IDE for C++ a try.