Thursday, November 18, 2010

First time programming?

My first programming language is C. During my first year in university, I cannot understand a thing about programming. Yes, I know what are functions, datatypes, etc. But whenever I am asked to write a program, I just cant write a thing. The one thing I do not know is: HOW TO USE WHAT I HAVE LEARNT TO MAKE A PROGRAM!?

The stuff which are taught in lectures are just the basic tools for a program. One good analogy is: In order for you to build your house (program), you use a hammer to hammer the nails onto the wall, a spanner to fix the pipes, etc. In order to PROGRAM (build your house), you need to know how to use all these basic tools such as functions, datatypes (int, char ).

The best way to do a program is to really use the divide and conquer method. Firstly, you look at a house and plan out what are the components that you need.

Next, build one component at a time and test the component THOROUGHLY to make sure it is fully workable and returns the result that you want!

For example, a toilet bowl's function is to be able to flush away all the dirty stuff when we press the handle. We will not wish to have the dirty stuff spray onto us when we press the handle. Thus, it is very important to test the component to make sure the functionality and result is what you really want.

Normally, I will put in outputs (in C: printf, C++: cout, PHP: echo) which can show the values of the variables that we use. In some cases, the values in the variables may not be what we expect it to be.
For example:
function testcompare(){
    int i = 5;
    int j = 6;
    if (i == j)  // We want to compare instead of assigning j to i.
   {
       cout >> "true";
   }
   else
  {
       cout >> "false";
  }
}

We want to test if i is equal to j, we will perform these actions. However, in most programming languages,  '=' is an assignment and not a 'is equal to'. Thus, it should be '==' instead of '='.

For beginners, on first look, we may think that this function will function in what we want it to be. However, it does not.

If we output the value of i and j within the if, we can detect that our comparison is faulty as both i and j will give the value 6.

2 comments:

  1. One thing I do is create an output class / global function that takes two items: the caller and whatever variable one wants to output.

    The function then outputs the classname of the caller, determines the type of the second variable and outputs it accordingly ( e.g. in php use print_r, int_val( BOOL ) etc ).

    This means that you won't run into the issue of 'losing' outputs and having to do a search through your whole project to find old output that you forgot to remove after they'd fulfilled their function. Also helpful as it can show the sequence of events in your program.

    ReplyDelete
  2. Wow, I didn't know that print_r acts like a variable tracker. That is one more tool in the debugging and checking amory.

    Guess I will need to go and spend some time to look through some good functions =) Thanks for sharing this!

    ReplyDelete