My blog has been moved to ariya.ofilabs.com.

Monday, November 28, 2005

Tenerife

N24 aired an interesting documentary film on Tenerife accident between two 747s, considered one of the biggest pre-9/11 aircraft disaster. Detailed information, including released reports and reconstructed illustrations are also available from Project Tenerife website.

This accident happened long time ago (when I was few days old), the lessons learned from it proved be to useful for improvements in the regulations. Even for people outside airline industry, it shows how good communication is indeed very very important.

The Danger of Spamming

Rick Downes is against spamming. Yet he was arrested because the police suspected that he spams people with advertisement of pharmaceutical products.

Friday, November 25, 2005

Seven-Segment Problem

After too long working on a prototype board, I had an idea to invent this question.

.

You have a 7-segment display. A byte is sent to the display, 7 bits are used to switch on (if the bit is set) or switch off (if the bit is reset) the segments. One bit in this byte is not used and always zero.

Values in the byte which correspond to decimal number (0 to 9) shown in the display are summarized as follows:

  "0"    0xE7 
  "1"    0x22 
  "2"    0xAD 
  "3"    0xAB 
  "4"    0x6A 
  "5"    0xCB 
  "6"    0xCF 
  "7"    0xA2 
  "8"    0xEF 
  "9"    0xEB 

Find out what value you must send so that the display shows "E".

In an interview, see how fast your candidate can get the answer (of course, you can replace "E" with another letter). Also, if he or she spends too much time studying number "8", time to try another candidate...

Thursday, November 24, 2005

Pronounciation of 'char'

From Bjarne Stroustrup's C++ FAQ:

How do you pronounce "char"?
"char" is usually pronounced "tchar", not "kar". This may seem illogical because "character" is pronounced "ka-rak-ter", but nobody ever accused English pronounciation and spelling of being logical.

All these years, I've pronounced it incorrectly...

Monday, November 21, 2005

RealTime vs Live

WordPerfect Office, since version 9, has a very nice timesaving feature called RealTime Preview. When choosing specific font from the dropdown list, the selected text is already reformatted and displayed using that font. This applies also with other types of formatting. And because the update is instant, you can scroll throughout the font list and see the text "morphs" into different look and finally choose what suits you best. This is not like in other office suite, where you need that many repetitive clicks because every time you change the font, the text just looks ugly and you have to repeat and repeat it again.

Half a decade later, Office 12 has Live Preview, which is basically the same thing.

Unzipping in C

If your C program needs to extract files from a ZIP-compressed package, using minizip (and zlib) is a fast and easy solution. Not to pollute planetkde (no pun intended here :-), a simple function to extract one file from a ZIP and write it to the specified output file is shown in this simple unzip example instead.

Friday, November 18, 2005

rand(): font, Qt 4.1, Superman, beauty

Some random goodies, welcoming the weekend.

Amusing history of Verdana and Georgia fonts. Got this link when reading Office UI blog.

Changelog for Qt 4.1 is quietly available.

Superman Returns will be out in Summer 2006. The released teaser so far contains almost nothing. Do not waste your time downloading it.

Beauty? Per definition, beauty is in the eye of the beer holder.

Unoptimize OpenOffice.org's content.xml

This is probably not of much interest for normal users.

If you work with OpenDocument and OpenOffice.org, you know that XML file produced by OpenOffice.org is not quite human-friendly as it contains no CR/LF and no indentation because it is (so to speak) "optimized" for size. The XML file of course looks ugly in normal text editor. To change this, open menu Tools, Option to bring the Options dialog. Choose Load/Save, General from the list of the left and uncheck the option Size optimization for XML format.

Thursday, November 17, 2005

No Test? It's Broken!

I'd like to add one important point to Aaron's list to good practices in open source software: extensive tests. Sure, manually testing the program is good, but since open-source software is done mostly by volunteers, tests that run by themselves are of great help. In the past, I have witnessed so many bugs in KOffice, including mine of course, that surely would be earlier exposed, had we an automated test suite for that.

This taught me first hand the importance of testing (unit test is the obvious choice, but even a bunch of plain assert()s is better than nothing), until I finally adopt the "if no test exists for this module, assume it's broken" philosophy. For my personal projects, now I am very careful to make the corresponding tester for all modules/classes. That will save my days and reduce the headache later. It is funny to observe that the code which does the testing is sometimes longer than the code under test (though this fact definitely plays little role here).

Test suite is also very useful on stuff being developed by many people. Some day in the future, maybe the maintenanceship of your code is transferred to another person and when s/he wants to further develop it (implement feature, fix the bug, add workaround, you name it), the test suite will give early hints whether the new code breaks the regression or not. "But the code looks good (and I am a great hacker!)" just does not guarantee anything and completely antipodal to that broken philosophy I've mentioned.

Speaking about test, I can as well mentioned that it is a good idea to valgrindify the program from time to time. Ideally, after a feature is correctly implemented and does not break any tests, Valgrind should be used to point any possibilities of memory leaks and other similar mistakes.

Programming is 1% coding, and 99% debugging. You surely would appreciate any extra help for that 99% part, wouldn't you?

So, happy testing.

Wednesday, November 16, 2005

Browser's Blue Screen

I wonder whether novice Windows users would get panic if you give them this link: http://www.acme.com/notexist. Can possibly some Ajax guru make it more scary?

"Related" HAL 9000 Technorati link: notexist :-))

Compiler Talk

When for the first time I learned C++, I used to practise guessing what goes wrong when the compiler issues errors. Basically, I jumped to the line where the error is spotted and, without reading further error message, tried to deduce the mistake just by looking at the code. I found that this is quite effective (but not efficient, do not do this for in real-world because you will waste lots of your time) to understand many advanced programming caveats. I even always recommend it to C/C++ newbie who has the guts to do so. Up to certain points, you can even start to understand the distinct behaviors of different type of compilers.

Often, time does not permit me to do this kind of exercises anymore. So instead of "talking" to the compiler directly, now from time to time I just drop Gimpel Software's bug of the month a visit. Some of them can be quite inspiring, and it is always fascinating to try to get the bug in matter of seconds. Try it yourself.

Monday, November 14, 2005

Who Let The Bugs Out?

Whoever behind it, this kdebugs.blogspot.com is cool.

Unleashed

Normally, amazing fighting scenes are what you expect from Jet Li's movies. That also what I had mind when I inserted Unleashed into my DVD player. But I was wrong. This European-produced movie offered a good and strong (and believable) story, and most important is that it was not packed with unnecessary fights (except perhaps one in the pool, but that's acceptable considering this is Jet Li's). The story was a bit slow and probably disappoint those who favor thriller, but all in all it is very much enjoyable. The performance of Morgan Freeman playing a fatherly figure was fairly decent as well.

Excluding his Chinese movies, in my opinion until now this is the best from Jet Li.

Sunday, November 13, 2005

Afraid or Not?

This is another joke.

At a company picnic, the CEO wanted to hold a little game. After asked for attention, he started his speech, "People, it is often said that we, the men in the family, are afraid of our spouses. Is that really true? Or is it only a myth? Well, let us now experience the moment of truth."

"For all the women, please let go your husbands for a while. Come on guys, come forward! For those who are afraid of your wives, just stand up next to me, on my left side. For those who aren't, on my right side."

It took a while until all the married men lined themselves. After some chaotic movements, in the end only one cool-looking guy stood to the right of the CEO. This stunned everybody.

The CEO approached him. "So, you are the best among us. You are the only who dare to stand here, while others had chosen another path. How come?"

"No idea", the guy gave a calm answer, "my wife told me I must stand here".

Thursday, November 10, 2005

Property in C++

Almost ten years ago a friend of mine showed that he was happy to use Visual Basic's (and also later on Delphi's, with Borland's extensions to Pascal) feature of object property, which allows one to set a property of an object and have the object respond to the change. For example, this code:

Button1.Left = 43

will automagically also move that button to a new position, not only changing Left to a new value. In C++, this is not achievable because no function call is involved. Instead, the code must be modified to something like:

Button1.SetLeft(43);

And we need to have another (getter) method to obtain the property's value:

int posx = Button1.Left();

while in VB and Delphi's Pascal, one Left is enough.

Personally, I believe this is only syntax stuff. It doesn't matter so much, it even improves nothing. But just to make a rebuttal, I crafted a simple example to show that it is also possible to implement such feature in C++.

The key here is that each property is an object. To cover all basic data type, obviously template-based is a good choice:

template<class T>
class Property
{
public:
 Property(){ owner = 0; };
 operator T(){ return data; }
 Property( T dat ){ data = dat; }
 void setup( Object* obj, std::string n ){ owner = obj; name = n; }
 Property& operator=( T dat ){ 
  bool changed = dat!=data; data = dat; 
  if(owner && changed) owner->propertyChanged(name);return *this; }
private:
 T data;
 Object* owner;
 std::string name;
};

Later on, to wrap that setup() method, a simple macro magic is employed:

#define INIT_PROPERTY(x)  (x).setup( this, #x )

The basic object system needs to have method to be called when its properties are modified:

class Object
{
public:
  virtual void propertyChanged( std::string name ) = 0;
};

As you can guess already, propertyChanged is invoked when a new value is assigned to the property. So the secret is here is the overloaded assignment operator.

A hypotetical widget named Slider can be implemented as follows:

class Slider: public Object
{
public:
  Slider();
  virtual void propertyChanged( std::string name );
  Property<int> min;
  Property<int> max;
  Property<double> value;
};

Properties of this Slider must be initialized in the constructor. This is so that each will get a unique name and assigned to an object (simply this) to which it will report when its value is changed. With the INIT_PROPERTY macro, this is as convenient as:

Slider::Slider()
{
  std::cout << "Creating slider" << std::endl;
  INIT_PROPERTY( min );
  INIT_PROPERTY( max );
  INIT_PROPERTY( value );
}

The job of propertyChanged is then to handle the situation when one of the property has been changed:

void Slider::propertyChanged( std::string name )
{
  if( name == "min" )
     {
     std::cout << "Slider.min has been changed" << std::endl;
     // do something
     }

  if( name == "max" )
     {
     std::cout << "Slider.max has been changed" << std::endl;
     // do something
     }

  if( name == "value" )
     {
     std::cout << "Slider.value has been changed" << std::endl;
     // do something
     }
}

Almost nothing else is needed. Then, the code snippet shown below is already comparable to how it is done in VB:

  Slider slider;
  slider.min = 1;
  slider.max = 42;
  slider.value = 8.3;

Althought is a "fake implementation", at least I have convinced my friend that C++ can have property.

Of course, this trick has some disadvantages. Properties needs to be initialized in the class constructor, so more boilerplate code compared to the case where this kind of feature is supported in the language itself. No checking when setting a value means corner cases must be well taken care of. Also, properties are object instances which are not so cheap. Comparing the property using string is also not fast. Infinite loop is even possible when it is not handled well.

I believe that some functors in combination with more template and macro magic will even allow the redirection of reading and writing property to the corresponding getter and setter methods. Left as an exercise for the reader :-P

Reading the Memoirs

Looking forward to seeing Zhang Ziyi's performance in Memoirs of a Geisha, I started to read the book from which the film is adapted. So the new few days will be hopefully quite exciting.

Wednesday, November 09, 2005

Qt 4.2: one tenth of...

As I write before, Qt 4.1 will be cool.

BUT, the next Qt 4.2 would be much more awesome because it is one tenth of the ultimate answer to life, the universe, and everything !

Tuesday, November 08, 2005

Reaper 3-D: Flight Combat

This Reaper 3-D, an open-source spaceship combat game seems to be underrated. It is of the same style as Terminal Velocity, but with better goodies such as improved 3-D ground objects (instead of flat, sprite-based ones like in TV). The OpenGL-based graphics is amazing, though it lacks the fast-paced background music which - in TV - pumps the adrenalin faster and faster. And granted, it has not been updated anymore since few years ago although the last release was still fun enough to play.

Image Hosted by ImageShack.us

Give it a try!

Monday, November 07, 2005

Engineers (Again)

Following Aaron Krill, here is another engineer joke:

It was decided to build a bridge between heaven and hell. Half of the bridge should be made from hell and another half from heaven and these two shall meet in the middle. People from hell worked hard for this, as if the bridge is finished they could also once a while visit the heaven (who wouldn't?).

Up to a point where the two parts of the bridge should be connected, there wasn't any structure or whatsoever from the heaven's side. It was just empty space. Even after waiting for some time, there seems no activity at all from heaven regarding this bridge thing.

A call was made from hell to heaven, asking for explanation on what had happenned. "Well", expressed a representative from heaven, "we don't have engineers."

Friday, November 04, 2005

DOOM and KPresenter

DOOM is an FPS game. KPresenter is our lovely presentation tool. What do they have in common?

Image Hosted by ImageShack.us

Well, long time ago I implemented lots of slide transition effects to KPresenter. Later on, just for fun I have added one effect called melting that IIRC has no equivalent in other presentation program. If you play the classic DOOM many times, you know how this effect looks like...

Thursday, November 03, 2005

Pocket Dragon

In Germany, a youngster might bump to you in and ask "Hey Alter, hast du Taschendrachen?" which is translated literally as "Hi dude, have you got pocket dragon?". Quite easily, one can guess that pocket dragon in this context means the lighter.

And there are still more phrases like this, such as Lungenbr├Âtchen (bread for the lung, means cigarette), F├╝nf-Finger-Rabatt (five-finger discount or thief) and Tretferrari (Ferrari with pedal, definitely a bike).What a wonderful imagination, isnt it? IIRC there is even a special slang dictionary for that!

Wednesday, November 02, 2005

I-D-D-Q-D

As I expected before, DOOM: The Movie was quite awesome. OK, as in the game, the story was rather weak. Often, it was quite predictable as well, e.g. that additional extra chromosome pair would apparently lead to something (although thumbs-up for mentioning Lucy).

The film is definitely more enjoyable for those who had experienced the game. The appearance of typical-scientist Dr. Carmack was a nod to John Carmack, this trivia would be hardly known if you are never exposed to iD Software. And Dr. Willits - for the famous DOOM levels designer Tim Willits - was also there (one may wonder though, where is Dr. Romero? :-P). Few minutes scene which was entirely in first-person perspective surely would be appreciated by the DOOM lovers. And of course, everybody has been waiting for that BFG show.

So, are we going to see Quake: The Movie soon?