Wednesday, February 11, 2009

Extacts from Pet Diary

Excerpts from a Dog's Diary

8:00 am - Dog food! My favourite thing!
9:30 am - A car ride! My favourite thing!
9:40 am - A walk in the park! My favourite thing!
10:30 am - Got rubbed and petted! My favourite thing!
12:00 pm - Lunch! My favourite thing!
1:00 pm - Played in the yard! My favourite thing!
3:00 pm - Wagged my tail! My favourite thing!
5:00 pm - Milk bones! My favourite thing!
7:00 pm - Got to play ball! My favourite thing!
8:00 pm - Wow! Watched TV with the people! My favourite thing!
11:00 pm - Sleeping on the bed! My favourite thing!

Excerpts from a Cat's Diary

Day 983 of my captivity.
My captors continue to taunt me with bizarre little dangling objects.

They dine lavishly on fresh meat, while the other inmates and I are fed
hash or some sort of dry nuggets. Although I make my contempt for the
rations perfectly clear, I nevertheless must eat something in order to
keep up my strength.

The only thing that keeps me going is my dream of escape. In an attempt
to disgust them, I once again vomit on the carpet.

Today I decapitated a mouse and dropped its headless body at their feet.
I had hoped this would strike fear into their hearts, since it clearly
demonstrates what I am capable of. However, they merely made
condescending comments about what a "good little hunter" I am.

There was some sort of assembly of their accomplices tonight. I was
placed in solitary confinement for the duration of the event.
However, I could hear the noises and smell the food. I overheard that
my confinement was due to the power of "allergies." I must learn what
this means and how to use it to my advantage.

Today I was almost successful in an attempt to assassinate one of my
tormentors by weaving around his feet as he was walking. I must try
this again tomorrow -- but at the top of the stairs.

I am convinced that the other prisoners here are flunkies and snitches.
The dog receives special privileges. He is regularly released - and
seems to be more than willing to return. He is obviously retarded.

The bird has got to be an informant. I observe him communicating with
the guards regularly. I am certain that he reports my every move. My
captors have arranged protective custody for him in an elevated cell, so
he is safe. For now....

Friday, January 23, 2009

Simple Object-oriented programming (OOP) concepts explanation

I don't include code samples because this should be used to understand the concepts rather than teach you to program. Please feel free to use it but please give me the credit if you use!

We start with 2 concepts
  • A Class
  • An Object also known as An Instance
So in object orientated programming, the structure has been built around the idea of the real world. So lets take it back to the real world for a second.
If I want to build a house what do I need? Okay before you say bricks, windows lets just make it a little less detailed and just say you need rooms.
OK so how do we make a room, erm... Well that's our first problem, we need a plan. So we draw up a blueprint of what our room is going to look like. For now we don't much care about what kind of room it is, we just need a room. So our blueprint defines 4 walls and a roof and a floor.
Great now we know how to build the room, how do we build it? Well get a builder so lets not worry to much about that part, let assume we can give someone the plans and they'll give us a room. Its good to note at this point that you can give the same plan to that builder or another builder over and over again and each time they'll give us back a similiar room.
So if our house is a collection of rooms we can make a whole house using the same plan a couple of times over to create a couple of rooms, put them together and whoala, we have a house. Albeit a very boring bare house.

So bringing the example above back to object orientation. We can say the blueprint is the class and the room is the object or instance of the room's blueprint.
The collection of rooms are just that, they can be grouped in either a collection (a instance of a collection blueprint or class) or an array which is just basically putting them side by side in your computers memory.

Now lets EXTEND on that!

OK so lets make out house a bit more interesting, shall we? We need a bathroom, with a toilet and a bath and a basin. Other than that it should be a room. So rather than start from scratch lets just staple some additional instructions to our builder onto the room blueprint.
So what do we want?
  1. We want a room
  2. We want a bathroom in the room
  3. We want a sink in the room
  4. We want a basin in the room
So what we are basically doing is EXTENDING our idea of a room. Which is the next concept in object orientation. You have something that's almost what you need, you tweak it, and whoala you have the new thing. Then we just give it to the builder with the extra notes and he makes us a bathroom room or kitchen, depending on the extended blueprint.

Lovely! So why not just copy and paste the class and give it another name, like make a bathroom blueprint from scratch. Well 4 reasons:
  1. Cause you actually doing just that anyway, because you are going to make a new class, its just going to be extending the other class. Basically just steal all its functionality.
  2. Because then its not a room anymore and we cannot refer to it as a room anymore. The nice thing about extending from a basic concept is that in code you can actually refer to the more complex items as their basic counterparts. For example I can still have a collection of rooms to my house, but now some of those rooms will just happen to be bath rooms. If we made a whole new bathroom blueprint it would be a different concept all together and we'd need to have 2 collections, one for all our normal rooms and one for our bathrooms, and that is just horribly inefficient.
  3. Because sometimes, you don't have access to the code of the class you want to extend. There are heaps of libraries of classes scattered over the Internet which might all do almost what you want but not quite. But you won't always have access to their code. So you'd have to resort to EXTENDING them to get what you want.
  4. And most importantly! Why do you want to take the risk of making a mistake recoding something that already works! Debugging takes up more time than coding in many scenarios so why go there?
In its simplest form most of the above benefits can be obtained by using an INTERFACE. Basically all an INTERFACE is, is a tick list of things to include. So if our room had an interface it would say something like, make sure its got walls a ceiling and a floor. It won't define any detail about the walls or the floor or the ceiling but it will just make sure its there. If our room had been using an interface we would have said it IMPLEMENTED the INTERFACE. Hey I'm a poet!
When the builder then took the room to his boss, the boss would get out his checklist and tick off
Walls. check!
Ceiling. check!
Floor???? Where is the floor?
And the builder would have to come back to you and say listen about your room, you forgot to add the floor and according to you tick list, or interface, it should have one. Could you please revise the blueprint and give it back to me?

We can cheat a little with the above scenario. Lets say our initial INTERFACE for a room stated that the room should always have a window. Sometimes however this isn't important to us, but its important enough in most cases that you want it in your check list or interface. So what we then do is we
first:
  • Implement our interface and build a blueprint of a "standard" room, but instead of specifying all the details we just specify some defaults. So for the windows section we just say, "no windows", for the walls we say "no walls" for the floor we say "no floors" etc.
second
  • We EXTEND our standard room, and make it into everything we want. But because the standard room blueprint already says "no windows". If we neglect to fill in anything about the windows on our new rooms blueprint. The builder won't be bothered because he can see the default is "no windows" so he'll assume that is what he needs to do.
Nice thing about this is we can refer to all our rooms by the name of the interface, lets say its called iRooms. So now we can build our house out of a collection of iRooms. Which can be anything as long as it ticks all the boxes. Cool huh?
Its good to mention here that Implementing an Interface, or Extending a Class does not limit you to their functionality. You can add whatever you want on top of whatever is there.

OK minor problem emerging here!

Lets say I take a room out of my house collection...., what kind of a room is it?

I don't know!

OK well I'm just going to the loo in it..... NOOOOOOOOOOO! Crash. (wanted to use another word but it would be nasty)

So what happened?

Because we only know its a room, we don't know if its got any special characteristics. We don't know if its just a room or if its a bathroom or a kitchen or... you get the idea.
So if we don't know what it is how do we use its, unfortunatly for us the computer is even dumber than we are. So it won't know either. Luckily there is a solution. A couple actually.
  1. All classes has a getType method. Even if you don't put it there. That is because without you knowing it any class you create in either java or c# and even now a days in vb EXTENDS the "base class". If you haven't caught that, that does mean you can refer to any object as the base object. This class has nothing much in it except one or two little useful methods. One of which is getType(). Yippeee we're saved. Well kinda. The problem is that even though the objects base knows exactly what it is, if we have it defined as a room and not a bathroom we will only be able to tell you things about the bathroom that it can tell you about the room. If the wall colors were different it could tell us that or if the floor was tiled we'd be okay. But it won't be able to tell us anything about the bath, so we need to change what we have it defined as a room to a bathroom. In object orientation this is known as CASTING, and you only need to CAST if you are going from a simpler to a more complicated class. Like from a room to a bathroom. So first we cast our room to a bathroom and then we ask things about the bath, but at least we could call the GetType method to figure out what to cast it to. I know, computers are dumb! The sooner you learn this the better.
  2. Instead of using the GetType() method, you could always add a room name to your checklist in your interface. That way you know you would always have to define it and you'll always know where it is.
  3. Strangely enough, mostly it either won't matter or you'll know exactly what kind of room or object you are dealing with. The most common CAST you will ever do is from a base object to your object of choice (see point one on for note on the base object).
Now lets talk a bit about SCOPE...

I gave a builder the same set of room blueprints 20 times. He made 20 rooms. I don't like the color of my room I'm going to use as my kitchen so I paint it blue (never said I had fashion sense). Does that imply all my walls in my house are now blue because I changed one? Of course not! The rooms have been built, they now have very little to do with class or blueprint that made them except that you can probably get a good idea of what its supposed to do from the class. Once the room has been built its an individual. It can have its own fashion sense it can be used for different things. What happens to the one does not happen to the other. Same is true for classes and objects.

And that brings us to REFERENCES. OK now after a couple of years we have plenty of houses with plenty of rooms. We now need to refer to Daniel room. His room already exist, its been long built, probably customized. So we don't need to give it to a builder, we don't need the class. We already have it. So we just can just REFER to it.
So Daniel just won a room makeover. but there are many different people involved. So they each need Daniel's room to do their thing. We don't need to give them each a copy of Daniel's room we just need to let them know where it is. Anyways a copy won't work in the object orientated world because changing the colour of the walls in one copy won't change it in all the other copies of Daniel's room like magic.
So we jot down Daniel's address and room number on piece of paper and give it to all involved. We can write as many pieces of paper as we want REFERRING to Daniels room, it'll still be the same room. Even if one of the specialists write the address for someone else and they go find it and work on it it will still be the same room so at the end of the day I'll have my home entertainment system, my PS3 and my slightly scary new bedding all in the same room.
98% of objects gets passed via references, so be careful of passing an object to something, changing it and expect it to come back the same way. If you pass someone a reference to your object and they change it they will be changing your object! Its actually quite a mission to copy an object so always assume you are passing a reference not a copy.

OK enough for now, I've already given away that I watch way to many home decor programs! What can you do, BBC loves the things.

Hope this helps someone. I'll try extend on it at some point in the near future.

Thursday, November 27, 2008

Arm, Armder, Moer-arm

Nou ons was moer-arm. Agt kinders. Daai tyd was ek in Std 2 en moes ons van daai glas bottelkies Gom met die rooi proppie en rooi kwassie hê vir skool. 
 
My Ma het amper flou geval toe ek haar vra vir gom. "Waar dink hulle moet ons die geld vandaan kry?" 
 
Maar natuurlik - Boere maak 'n plan. En Ma was nie stupid nie. Sy kon tot gom maak. Ruk daar flour uit, gooi suiker by en kook die spul op die stoof. 
Ek staan eenkant, baie impressed met my slim Ma. 
 
Laat dit toe effens afkoel en sit dit in 'n ou NCR Yeast Blikkie wat so bietjie "verblyk" was. 
 
Ek was die enigste kind met 'n Yeast Blikkie op my tafel. Al die ander kinders het die regte bottelkies Gom gehad. Ek het nie baie omgegee nie, want my gom was baie beter as hulle sin en het geplak soos Superglue nie kan plak nie! 
 
So drie weke later, terwyl die biesies buite bewe van die hitte, sit ons klas in doodse stilte, besig met eksamen. 
 
Fokkit mense, een moerse ontploffing ruk die hele skool tot aandag. Daar trek my donnerse blikkie se deksel en hy agterna tot teenaan die sielieng. 
Dis net gom wat neerkom op ons soos 'n donnerstorm! Alles is wit. En dit stink ongelooflik - suurhol se moses! Ek't nog nooit iets in my hele lewe geruik wat so gestink het nie. 
 
Die Juffrou se brille is vol gom. Haar hare spierwit. Dit drup van haar af asof sy in 'n shower staan. Tot in haar kliewitch by haar tieties. Ek kon nie glo dat sy soos 'n beeld lyk nie. 
 
Oral waar ek kyk in die klas is almal faaktap van die gom. Almal is wit en gil van die skrik. Ons boeke is faaktap. Ons klere is faaktap. Ons stink faaktap. 
 
Juffrou wip toe haar gat en jaag ons almal uit en sê ons kan maar huis toe gaan. Van die kinders wat bus gery het se gom het goed drooggeword van al die gewag. 
 
Ek was woedend toe ek by die huis kom. My Ma het gedink dis 'n moerse grap en het haar dinges amper geskeur van die lag toe sy my sien EN ruik. 
 
Die klaskamer moes uitgeverf word om van die suurholreuk ontslae te raak, maar ons kon dit nog vir weke dwarsdeur daai PVA ruik. 
 
Ek was glad nie gewild nie. Niemand wou eers meer met my speel nie...
 
Dis nou wat gebeur as jy Moer-arm is!