Coding Guide Lines

25 Jun

Recently I have been working in a big mathematical simulation desktop application project that contained lots of entities that were interacting with each other with some degree of complexity and required the GUI to update in real time in response to user inputs that triggered certain calculations.

Somethings I have learned the hard way and included some rework to fix, below are the lessons I have learned during this project.

  1. Wrap all primitives and Strings in classes
  2. Wrap any collection in a separate class
  3. use Getters/Setters
  4. Use parent reference in your child objects
  5. Use data aware controls as much as possible.
  6. Put all string literals in a single class as static variables
  7. Use MVVM or MVC to separate between business logic and presentation.
  8. Concentrate business logic in pure classes that doesn’t contain anything else.
  9. Never ever duplicate code, instead encapsulate and reuse. even if its time consuming.
  10. Try using design patterns whenever possible (creational patterns was a life saver in my case.)

I know my rule about using Getters/Setters contradicts with the Object Calisthenics. but I actually had to use them to get/set the value based on the selected global unit system Object, and to fire events that refreshed the GUI element associated with the data model variables.


Physics tutorial

3 Jun

The Tutorial covers basic physics topics using informative graphics and an easy-to-understand language. Each unit is broken up into lessons and sub-lessons. A lesson resembles the type and extent of coverage given to that physics topic in class. The sub-lessons are accompanied by Check Your Understanding sections, providing an opportunity to assess one’s understanding of the lesson material.

C++ reference

29 May

Here is a free C++ reference for those who want to learn. If you already know c++, and want to make a very quick revision this PDF is an excellent source. It is just 300 pages, so it is very suitable for refreshing your information quickly. If you have never programmed before then this book is not for you.

Blender 3D modeling complete Video tutorial

26 May

While surfing the Internet looking for 3D tutorials I found a guy called Jason Walsh has created a complete blender 3D video tutorial and has put it on his you tube channel. You can reah his channel by navigating to this URL

Here is the first video in a more than 100 videos series

Real draw pro tutorial [part 2]

24 May

I have created new tutorials for real draw, I hope you like it.

Lesson 4

Lesson 5


Lesson 6

XML explained in Arabic

22 May

I have created two video tutorials to explain what XML is and how the world has benefited from it. The videos are in Arabic, I hope you like them both.

XML explained (Arabic)

XML applications (Arabic)

Creating nice looking game textures using GIMP

21 May

While I was surfing the Internet looking for some graphics tutorials for a game I’m currently building, I found those three simple nice tutorials on how to create some realistic textures using GIMP

How to create a rock texture

How to create a wooden texture

How to make a colorful background

Real draw pro tutorial

20 May

I used to use real draw pro to create some of the 2D graphics for games, or to quickly prototype any art work ideas. I have created these introductory videos long time ago, but I thought they might still be useful to you. Hope you like them.

Lesson 1

Lesson 2

Lesson 3

Learn to think like a computer, to code like a guru [Part 3]

19 May

The last time I ended my post with two simple questions, and some of you have provided perfect answers, and I was really happy about that. But I still need to make my promise and answer them for you. For the first question which was “What Jazari was going to do if he can’t find resident evil 5 disk on the table?” The answer is that “Jazari was going to hang, since Jazari was not going to be able to execute the command as the disk is not on the table, and as we said before if a computer or a robot failed to execute a command it will hang”

Now for the second question which was “what can we do in this case?” The answer is that we are going to add an extra if command to check if “resident evil 5” is on the table or not, and accordingly Jazari will pick up the disk or not. We will also add an if command at the end of the sequence to check if Jazari is holding a disk in his hand or not and accordingly un-grasp the disk or just say in a metallic voice “Sorry no games found on the table”.

To make it easier to understand I have included the below two figures. The first figure is the command sequence (which we will refer to from now on as pseudo-code, and the second figure illustrates the pseudo-code (this type of diagrams is called flow-chart, so from now on we will use the term flow chart).

Click to enlarge

Let’s explain the pseudo-code, as you can see it looks a bit similar to the pseudo-code we have written in the previous post, As Jazari will move 10 meters, turn left, and then will move 5 meters to reach the table has the games on its top. By this point Jazari will take a look at the table to see if killzone 3 disk is on the table or not, and since it’s not on the table Jazari will execute the commands between “begin else” and “end else”. Which contains another if command that asks Jazari to check if the “resident evil 5” disk is on the table or not. Since the “resident evil 5” disk is not on the table “Jazari” will execute the commands between the “Begin else” and “End else” (line 4.b.1.b.1) which is simply nothing. Finally Jazari will return back to me by moving 5 meters back, turning right, and then moving 10 meters back. At this time Jazari will check if he is holding a disk in his right hand or not. In our case he is not holding any disks so it will execute the commands between “Begin else” and “End else” which is saying using his robotic metal voice “Sorry sir, No games found on the table”.

Robots and computers can execute nothing or simply the “empty command sequence”, the execution of nothing is simply moving on to the next command.

Pretty simple isn’t it? I know that things are becoming a bit more complex now, but the key to understand this is to remember the two rules we have explained so far “Sequence” and “Branching”. Till now we are using them like Mechano pieces, we gather them together to build something useful. So whenever you think its complex look at each mechano piece separately and make sure you understand it very well and move on to the next piece. So take your time to understand each and single command separately, the move to the next command, and don’t forget to use the flow chart to relate commands to each others. keep trying until your mind accepts the way computers and robots think.

Click to enlarge

I think in this example all the pseudo-code is simple and clear except command number four, it looks a little complex. We will need to break it apart to make it easier to explain and understand. Take a look at it in the flow chart you can see that it’s a normal if command with the normal left and right branches (the Begin if, and Begin else). Now look down left branch, as you can see it hold a single “if” command which has its own left and right branches. This was possible because as we said before the “if” command is treated by Jazari as a single command, and since we can put any command we want between “Begin If”, “End if” , “Begin else” and “End else” we can normally put an “if” command without any problems.

When an if command is put between “Begin if”, “end if”, “begin else”, or “end else” of another “if” command, it is called a nested if. Nesting can be up to any level which means that you can have an “if” inside an “if”, inside another “if” which is inside another “if”, etc…

Now as you understand the nested if, let’s agree on a numbering system we will use in our pseudo-code. We will start the count with one, and for each following command we will increase the count by one. This means that if we have four commands we will start by one and end at four. Let’s apply this on our pseudo-code. Please take a look at the diagram and notice that we have eight commands. For the “If” command left and right branches, For the right branch (“begin if” and “end if”) we will add an “a” after the number given to the “if” command and start counting from one for each following command between “begin if” and “end if”. Please take your time to look at the below diagrams to keep following up with me.

Now for any command written between “Begin else” and “End else” (left branch) we will add a “b” after the number give to the “if” command and start counting from one for each following command between “begin else” and “end else”. Again take a look at the diagram to understand what I mean.

Now take a look at the nested if, and make sure you understand its number properly, try to apply the numbering rules mentioned and the common sense. I hope it is simple enough.

What have learned during this post

  1.  A new computer vocabulary: pseudo-code, and flow-chart
  2.  The fact that computers can execute empty commands by doing nothing and moving on to the next command
  3. Nesting concept for the “if” command
  4. How to number the commands in a pseudo-code

 Your assignment
Now as an assignment I want you to write Pseudo-code and draw the flow chart for the following case. I want Jazari to bring me a cold beverage from the fridge, if there was no cold beverage in the fridge Jazari can bring me my favorite “Baklava” which it is in the fridge too, and if was no “Baklava” in my fridge I want Jazari to return back to me and say “Sorry sir, You don’t have any food in your fridge. You might consider to ask for a pizza delivery”.

I can’t stress how important it is to make this assignment, as it will help your mind think like a computer which is needed if you want to be an excellent coder one day. Keep trying an don’t give up easily, even if you think you can’t do it correct, continuous trials will challenge your mind which is really desirable in any learning process. If you faced any trouble with this assignment feel free to contact me.

Learn to think like a computer, to code like a guru [Part 2]

17 May

So I’m back from work and ready to have some fun, I guess I will enjoy some play-station 3 games. Since I have already mastered how to give orders to my robot “Jazari, using the “Sequence” rule, I will ask him to get me “Kill zone 3” disk.

  1.  Move 10 meters forward
  2. Turn 90 degrees left
  3. Move 5 meters forward
  4. Grasp killzone 3 disk from the table using right hand
  5. Move 5 meters backward
  6. Turn 90 degrees right
  7. Move 10 meters backward
  8. Un grasp killzone 3 from right hand

Okay, I will load this  “sequence” of orders into Jazari, and wait for him to bring me the game. Until he is back please read the above steps carefully and make sure you don’t have any problems with them.


Time is ticking

It seems that “Jazari” is taking a bit longer than it should to get me the disk. I will go to check what’s wrong with him. I can see that Jazari is standing right infront of my disks not doing anything at all. what could have gone wrong? I’m pretty sure that the sequence is correct. Anyways I’m standing right infront of my discs, I will grab the disc myself.

Jazari has hanged

I can’t find my “Killzone 3” disc, ohh I have just remembered that I gave it to a friend last week, that’s okay I can play “resident evil 5” for today. Now I understand why Jazari has stopped working. He couldn’t continue the execution of the sequence as he couldn’t execute one of the commands in the sequence.

If a computer or a robot failed to execute a command in a sequence, it will not continue to execute the next commands and it will keep waiting in it’s place forever without doing anything which is known an a hang.

Okay, now we need to think what to do to avoid such a hang. We need something more than just a sequence of commands that are executed from top to bottom. Something that enable us to make Jazari execute a specific set of  commands only under certain conditions. Here comes the second rule we found in Jazari’s manual, “Branching”.

Branching can be defined as the ability to execute a specific set of commands when certain conditions are met, if these certain conditions are not met then this specific  set of commands will never be executed.

Let’s rewrite our order to Jazari  using both rules, sequence and branching. But first let me push the reset button to restart Jazari, and pull it back to my living room.

1- Move 10 meters forward
2- Turn 90 degrees to left
3- Move 5 meters forward
4- If killzone 3 disk is on the table
Begin if
Grasp killzone 3 disc from the table using right hand
End if
Begin else
Grasp resident evil 5 disc from the table using right hand
End else
6- Move 5 meters backward
7- Turn 90 degrees to right
8- Move 10 meters backward
9- Un grasp the disk from right hand

Let’s see what Jazari will do this time. Let me push that button and wait. That has worked, in less than 20 seconds Jazari is back with resident evil 5 disk and has dropped it on my living room carpet.

here we have asked Jazari to move to the table where I put my games, then take a look and see if Killzone 3 disk is on the table or not. then execute the commands between “begin If” and “end if” when killzone 3 disk is on the table otherwise it will execute the commands between “begin else” and “end else”. after that it will return back to my living room and leave the disk from his hand (which could be killzone 3 or resident evil 5 according to the availability of killzone 3 on the table). In our case Jazari has returned with resident evil 5 as killzone 3 is not available on the table.

You might have noticed that branching has changed the order of execution as the command “Grasp killzone 3 disc from the table using right hand” was not executed although it is located before “Grasp resident evil 5 disc from the table using right hand”. 

To better understand branching we will draw an illustrative diagram to show why its called branching and to show the execution direction as it is no longer from top to bottom.

Click the image for a high resolution picture

As you can see the execution is moving from top to bottom, until it reaches the “if” command (The area highlighted in green) by then there are two branches for Jazari to choose which one of them to execute, he checks if the killzone 3 disk is on the table or not. Since I have lent the killzone 3 disk to a friend the execution direction will change to the left branch where Jazari will pick up resident evil 5 disk (which I’m 100% sure its on the table). after then Jazari  will execute the next commands (under the green area) normally in order.

Now what If the killzone 3 was on the table? The execution direction was going to change to the right branch where Jazari was going to pick up killzone 3 disk, then Jazari  will execute the next commands (under the green area) normally in order.

As you can see from the diagram, I have highlighted the branching area with green, also I given the “if” command a single number in the sequence. I did that because the “if” command is treated as one block, which means that it will always be executed. The “if” command execution is simply the execution of either the commands between the “begin if” and “end if” when the condition is correct or execution of the commands between the “begin else” and “end else”, when the condition is not correct.

The if command takes this form, and it must be always written that way, anything written in bold must always be written. as I have just said the “if” command is a single command although it is written on multiple lines.

If certain condition is correct
Begin if
Put here the list of commands you want to execute if the condition is correct
End if
Begin Else
put here the list of commands you want to execute if the condition is not correct
End Else

how can an “if” be a single command while it’s written on multiple lines?
To answer this question I will ask you this question, is the below a single command or multiple commands?

10 meters

What if I wrote it that way on a single line

Move 10 meters forward

Is it a single command or multiple commands?

Well, computers and robots don’t see things the way we humans see, for example computers and robots don’t know what lines are. They don’t even care if a command is written on a single line or multiple lines. In fact computers and robots come from the factory with a limited set of commands that the factory engineers have put into them, for example, we have seen the “move” command that make Jazari moves forward or backward. Where do you think I got move from? I got it from Jazari’s catalog.

Now I’m telling you that Jazari’s engineers have indicated that the “if” is a single command, so from Jazari’s point of view it is a single command. Train your mind to accept this fact as you are here to learn how to think like a computer.

To help your mind accept the fact that “if” is a single command always imagine it as drawn below, As you can see it looks like a single block now surrounded with dotted line. so when you want to use an if command you will just put that block, then write down the commands you want between begin if, end if , begin else, and end else. I hope that helps.

If command

Again remember that when you include the “if” command in a sequence you either include it all or nothing at all. There is no such a thing as an “if” without “begin if”, “end if”, “else”, “begin else”, and “end else”. The max you can do, is to write or not write any commands between “begin if”, “end if”,  “begin else” and “end else”.

Why I’m stressing so hard on that “if” is a single command?

First let’s assume that  “resident evil 5” disk was not on the table.

1- what Jazari was going to do? Why?

2- what can we do in this case?

Think about these questions and i will provide the answer in the next post. But If you were able to answer both questions then congratulations, your mind is starting to think like a computer.