Tuesday, January 27, 2009

A blunder ... can compiler help?

Yesterday, as i was writing another code, i made this simple mistake, illustrated by the following example (example only, don't do this!):

int foo(int i)
{
int j = 2;
if(i > 0)
return i; /* here should be j*/
else
return 0;
}

ok, this is not a good example (it is rubbish), yet clarify my point. It is not simply a typo (which can possibly be another blunder, duh!). I wrote 'return i' instead of 'return j' because i thought it should be 'i'. This is called semantical error. It is not syntactical error, nor grammatical error. It is simply a blunder, and worse, it can't be detected by a compiler.

Thursday, January 22, 2009

Is it only about logic?

What programming is all about? I mean the philosophy beneath. Is it only about logical thinking, or pure imagination (creative) or something that combined both? Hmmm... i'm still curious ...

However, i believe that both thinking are involved (other than the fact both are the functions of our brain). Since at certain levels, we have to use our abstract, creative imagination as to think about a any possible solutions. Whereas during the level where we are going to form the solution, we must use our logical thinking.

... ?

Monday, January 19, 2009

Snippet - a release from wholeday coding

Two friends, Ritchie and Ben (not real names), are heading back after losing their football match that they had just played. As this lost was so embarrassing that they lose to underdog team, they decided to hid their identities from locals.

The next morning, Ritchie went out in disguise. On his path, he met with an old woman. Suddenly as he's passing her by, she said to him, 'hi Ritchie!'. What a shocking it was to Ritchie, as he thought no one would recognize him. So frustrated and worried, then hurriedly he went back, thinking about other disguise.

The next morning, he went out again, but now with a different disguised. And there, again, he met with the same old woman that he met yesterday. 'We'll see if you can catch me again!' as he said to himself. Then, another shock readied upon him as he is came so close to the old women, she said 'Hi Ritchie!'. Ritchie was shocked. 'How on earth did she knows it is me?!!!' Ritchie never met with this woman before.

'How come did you know it's me?' he asked.

'Shh... it's me, Ben!'

Friday, January 16, 2009

Visualizing using Visgraph

I've learnt that we can visualize (preview) a text-based 'directed graph' using visgraph. I just learn to use them, so all i can say (after a sneak preview and simple test), it is magnificent!

What i found so far, it is easy to draw directed graph e.g. FSA using visgraph. It suits my need. All the drawings are done automatically and nicely organized. We just specify it using a special specification language in text format. That's all i can say (so far).

I'll write more later.

Wednesday, January 14, 2009

Structure or no structure

I was in the advance algorithm class this morning, and there was something came across my mind.
I was shown a basic solution of using dynamic programming in searching for Longest Common Subsequence (LCS) between two sequences / strings. (well this out of context). What's make me think about struct/no struct, is because this solution provides two matrices in the solution, which contains two different types of values, but structure is identical, e.g. 2 mxn/ 2-dimensional arrays. We may assume this subject intentionally use naive approach to explain about more complex thing (about advance algorithm), so it may use simple representation (2 matrices) instead of array of structures.

But in reality which is the best?
For the above example, we may use array of structure (which contains both values) instead of using 2 separate but identical arrays to represent 2 different types of values. I originally advocates that (structure instead of different arrays). But, after seeing the example, i changed my mind. Why? because this approach can also provide several advantages:
  1. it is faster
  2. it is simpler, (that's why it is fast).
Array is simpler than structure, because underlying beneath, compiler construct the housekeeping information for arrays simpler than for structure. Where as for arrays, only 2 info stored ; (1) the types, (2) the length/size. But more information needed to maintain structures i.e. structure description (complex). This also means increased access time (ignorantly the compile time as well, maybe!).

Basically, what i think best is both approaches (structure or multiple arrays) should be considered, regardless what type of information we need to store. E.g. personal information (name, age, address), we may have to considered both approaches - either structure or 3 arrays, instead bluntly approaching towards structure only.