Monday, November 23, 2009

Curiosity of code

Just some random curiosities, I've recently seen in PHP code.
if (TRUE) {
...
}

or maybe:
if (...) {
...
}
else {
// see Code below
}
An evergreen:

function _copy (...) {
...
}

/* Workaround function */
function copy (...) {
...
}

Sunday, November 22, 2009

Rest in Peace, grandma.

A few weeks ago I've visited my grandma, because she and my grandpa had birthday.
We also did it, because we figured that it might be the last chance to see her alive.
It was a hard time for us. Because she wasn't the person I knew, the person with whom I argued so often when I was a young boy, the person who showed me the beauty of cooking. She were lying in her bed, barely realizing us, not really able to do conversation. As soon as we went away from her bed she was calling for one of us, repeating the name of the person she was calling for every few seconds. Most of the time she called for my grandpa, but another time it was me she was calling. I then stood there, next to her bed, not really able to talk, because she wasn't and I didn't know what to say.
And then I had a talk to my grandpa. Although he looked visibly aged, he still seemed to be in a good constitution. But while talking to him I realized that he was tired. He told me that his woman was asking him eight times a day which day it was and such things. He told me and my cousin that he fears the day when she is gone. It was hard. I mean.. I knew it, I knew that it would break his heart when she's gone, but in my memorization he has never been a guy who would have spoken it out. I know that both had good and bad times together. Their was always that joke about them, that they can't do with each other but not without each other either. But now, the very imagination that this might be true, caused frighten in me.
Yesterday I got a call from my mother, telling me, that it seems that she decided that she will go, so she drove to my grandparents. A few hours later she called me again and said to me that it is over. Slept in, in silence. No pulse anymore. But the first thing I replied was: Can you take care for grandpa? Its not that I don't sorrow, but I knew it were better for her. She had a life with ups and downs, she had a man who loved her more then anything else. But she wasn't able to live or enjoy her last days I think. And now I'm afraid about the living, the one whose heart supposedly just broke. I hope he will be able to enjoy his last years anyway. Because he is a good man, has always been and I think he deserves it.

Grandma, we will miss you. Rest in Peace.

Tuesday, October 27, 2009

Things that make you a good programmer

If you ever wondered if you are a good programmer (not), you might think about the following points:

1. Repeat yourself. How else would you keep yourself busy, if your customer has new requirements?

2. Re-Using code is for people who take the other side of the street if a big dog walks along. No risk, no fun. How else would you find out that the common idiom you use is really the way the job has to be done?

3. If you have a coding convention (e.g. how code has to be indented): Just ignore it. Its a good thing to have editors go crazy, when trying to automatically detect the indenting of a source file. By always confusing the editor you keep up the fun of the people, who try to change your code. It would be too boring for them, to simply edit the file, without the quiz which quoting applies to your code. Extra points for those who additionaly (to mixing tabs, spaces, 4 and 8 space, expandtab and no expandtab) write a vim modeline into their file that - by guarantee - does not match the indenting of the file.

4. If you can complicate things: Do it. Numeric indices in arrays can be exchanged by arbitrary strings, this makes your code more interesting. Especially if you have to move elements in that array. And complicated codes makes people, who don't know it, think that you are a good programmer. One step nearer to your goal, isn't it?

5. When working with different classes invent a system that auto-loads the classes you need. Don't document it, that would be a risk for your job. Its not neccessary anyway, because good programmers like the riddle and finding out what gets called, where and why, is a simple but entertaining riddle. After all documenting is a bad idea as well. Especially if you make your system open source, because with a good documentation, it might be to easy for competitors to use your code to make money.

6. If you find ways to do extra function calls: Do it. It gives you the chance to refactor your code, if the customer notices that it is too slow. Great opportunity, hu?

(But, seriously: Don't hear on me. Its just a cynical way to express my feelings.)

Monday, October 26, 2009

Building a 15W Debian GNU/Linux system

When the Intel Atom was revealed to the public I didn't came around to say: "Wow!", because that piece of hardware promises to be a generic-x86 1.6 GHZ CPU with a total power consumption of 2 Watt, which is amazing considering that x86 hardware generally wasn't an option if you wanted to build a low-power system. But then the first chipsets were presented to the public and the Atom became a farce, because you don't want to have a chipset that eats over 25W for a CPU which consumes 2W. That was basically laughable.

Recently I managed to find out that there is a new chipset out there, the Intel i945GSE, which runs at about 11W TDP, including the soldered-on-board N270 atom cpu. And I convinced myself that this could get my new homeserver. So together with a 2.5" drive I could get a system which runs with about 15W maximum power consumption, which is amazing, given that the Arcor Easybox my provider gave to me seems to have similar maximum power consumptions. And it isn't able to provide me with the great flexibility, the new Atom system is.

So I bought the following components:

  • Intel Essential Series D945GSEJT
  • A Mini-ITX M350 case, which is amazing, because its about the size of a Linksys router and should still provide a good thermal environment.
  • 2GB Kingston HyperX DDR2 533 MHZ S0 DIMM
  • a Western Digital Scorpio 320 GB hard-drive
It took a while to get those components together, especially because I previously decided for an Antec case which I ordered from K&M Elektronik, but as they didn't keep their delivery promise I came to the M350. What a luck.

Running Debian on this machine is the easy part, you would think. This is true, for some exceptions. First: Lenny runs fine. I've installed the notebook hd in my desktop and then put it in the atom, when I got the first hardware and it worked right away. Except of a grub message, which is disturbing and which I didn't manage to fix right now (grub says "Error: No such disk" just to get the menu a seconds later anyway and boot the system flawless).

What didn' t work exactly reliable was the included network chip. Its quiet a shame to say that, but if you buy an Intel board, wouldn't you expect that it would run Intel components? Unfortunately this is not true for the atom board. It has a Realtek RTL8111 network chip, which isn't properly supported by the 2.6.26 kernel (that means the kernel think it is and loads a rtl8169 module, which isn't able to properly detect a link).
The workaround for this is to use a 8168 module from Realtek and compile it for your kernel, but as I equipped this system with an Atheros 2424 PCIe chipset for playing WLAN AP, too, I had to upgrade to 2.6.31 anyway and there the chip is fully supported by 8169.

Making the system an access point has been surprisinly easy as well. The greatest pain was to find a Mini PCIe WLAN card, because after all this isn't very common. However I found one based on an Atheros 2424 chipset and bought it. I additionaly bought an SMA-antenna connector that I could mount into the case (the M350 has a preparation hole for it) and an SMA antenna.
Setting this up, has been fairly easy. You need to know, that running master mode with newer mac-subsystem-drivers in Linux doesn't allow setting master mode directly. Instead you need to use an application to manage everything, which is capable of running cards over netlink. Thats hostapd. The unfortune is, that the lenny version is too old and so I built myself a (hacky) backport of the sid version, which isn't that hard anyway, because rebuilding against Lenny is enough. Additional you need a kernel 2.6.30 with compat-wireless extensions, or an 2.6.31, because previously the ath5k driver didn't support the master mode. After that getting hostapd up is a matter of a 4 - 15 lines configuration file. For me its now running in 802.11g with WPA and a short rekeying interval with 14 lines of configuration.

After all I'm satisfied with the system. Without any fan the CPU constantly runs at 55°C, which is okay, given that it must operate within 0 and 90 °C according to the tech specs. The system and the disk are somewhat lower (47 and 39°C). The power of this system is more then enough. Its booting quick and working with it works without latencies, even when the system is doing something. What I haven't yet tested is weither the power consumption actually fulfills the expactations. I will do so, once I got a wattmeter.

Friday, October 23, 2009

What an "Intel atom inside" sticker could make of you

So I've got this cool Intel Atom-Board Intel Essential Series D945GSEJT, which is the first Atom-ITX-board that doesn't feature a 25W chipset for a 2W CPU. Its pretty cool, but one thing made me laugh.
Today I saw that there came a "Intel atom inside" sticker with the board. I thought: Well, nice, now I could - if I wanted - put this on my Mini-ITX-system. But then I saw whats been written next to this: "Use of the enclosed Intel Atom logo label is unauthorized and constitutes infringement of Intel's exclusive trademark rights unless you have signed the Intel Atom logo trademark license".

Isn't it nice of Intel to supply me with a sticker which would make me a criminal if I'd use it?

Tuesday, October 20, 2009

Cool PHP-Code.

Did you know that in PHP you can write something like that:

$test = "foobar";
$test = sTr_RePlace("bar", "baz", $test);
$x = sPrinTf("%s is strange.", $test);

pRint $x . "\n";
eCho "foo";
What makes me frighten is that this actually used, e.g. by this code snippet which exists (in a similar form) in an unnamed PHP-project:

echo sPrintF(_("Bla bla bla: %s"), $bla))

And yes, they do use echo to output the result of sprintf.

Update: So i got this great comment. The commentator wants to point out that the senseless use of "echo sprintf" is because of gettext. He says "That's simply the way you use gettext." But this is simply not true. The difference between printf and sprintf is that the first one outputs the string, while the second one returns it. That means that in the above example printf could be used (instead of sprintf) without a useless echo call in front of it. The reason for using sprintf (and eventually the reason because you find it in a lot of applications using gettext) is that you can use it to fill a variable with the translated string or to use the string in-place. A common use-case for this is to handle a translated string to a template engine, for example.

Wednesday, October 14, 2009

PHP and the great "===" operator

Lets suppose you've got an array with numeric indices:
[0] => 'bla'
[1] => 'blub'
Now you want to do something if the element 'bla' is found in that array.
Well, you know that PHP has a function array_search, which returns the key(s) of the found values. Lets say, you write something like this:
if(array_search($array, 'bla')) { do_something }

Would you expect that do_something would actually do something?
If yes: You are wrong.
If no: Great, you've understand some parts of PHP - insanitygoodness..

Actually, if 'bla' wouldn't have the index 0 it would work, because 1, 2, 3, 4, etc. is TRUE. But unfortunately PHP has some sort of implicit casting, which makes 0 behave like a FALSE, depending on the context. So following this, the if() works for all elements except 0.

You might be tempted to write
if(array_search... != FALSE)
But this wouldn't help you, because 0 would still evaluate to FALSE, leading to if(FALSE != FALSE) which is (hopefully obviously) never true.

A PHP beginner (or even an intermediate, if he never stumbled across this case) might ask:
Whats the solution for this dilemma?

Luckily the PHP documentation is great. It tells you about this. And additionally PHP has got this great operator (===), which causes people to ask "WTF?" when they hear about it for the first time. Additionaly to comparing the values to each other, they also check the type of the variable. This leads to the wanted result because 0 is an integer, while FALSE is Boolean. So the solution for our problem looks like this:
if (array_search($array, 'bar') !== FALSE) {
Isn't this great?