Tudor Barbu's blog

Ramblings about software development

16 May

Open Hack EU

Some asshole flying a plane in front of my house at 7 in the morning just woke me up, interrupting my well deserved rest after Yahoo’s 24 hours hackathon. But this post is not about Cessnas spraying chemicals, it’s about one of the best programer oriented events I ever attended. Thank you Yahoo! for making it possible.

I teamed up with my colleague from Vendo Adolfo Abegg and Alex Brausewetter, a former colleague that decided to start his own company called Helpdesk, aimed at delivering the best customer support and ticketing software on the market. Some friendly SEO can’t hurt, right ;) ?

The hacking event was great, a lot of cool hacks, both software and hardware. Yahoo’s Pipes service was hacked by members of the Romanian Security Team on the stage, while Yahoo’s CTO was watching – awkward. It seems that they got pulled into a “private chat” by Yahoo!’s officials shortly after. Read the whole story here.

Another hack that got my attention was evA-Ziune – a site where you can report small fraud, like not getting receipts, using Foursquare’s API. This will quickly overload their servers :) !

The hardware hacks were also impressive, especially the one called Yahoo! Farm, a table size farm which can tell you how many friends you have online on YMessenger using robotic sheep. Pretty nifty! It won the Hacker’s choice prize! Anyway, all the projects are available here and the winners here. Take a look! There are a lot of cool projects out there!

Our project was mooooody.com, a site which detects your location, queries Yahoo! Weather for that location and plays music accordingly. Although it has no market value – or any other value what so ever – we had fun building it and we got a lot of positive feedback from other attendees. We mashed up a lot of clever hacks and technologies but a project that doesn’t address a real problem and is built around illegally playing copyrighted music from Youtube – a service belonging to Yahoo!’s arch-rival Google – couldn’t possibly impress the jury so we didn’t win anything. Nevertheless, we’re proud of it!

Congratulations to the winners, congratulations to Yahoo! for organizing such an event and congratulations to all attendees for creating such a cool atmosphere.

I’ve found a great way to have fun with your linux server. First of all, install cowsay. If you use Ubuntu server or Debian, you can use apt-get install it:

sudo apt-get install cowsay

Now, you have to wait for another user to connect to the server via ssh. You can use the who command to see who is logged in. With two open terminals on my Ubuntu box, who‘s output looks something like this.

tudor@thor:~$ who
tudor    tty7         2009-06-17 20:31 (:0)
tudor    pts/0        2009-06-17 20:36 (:0.0)
tudor    pts/2        2009-06-17 22:21 (:0.0)

Let’s assume that the victim is the one using the first terminal, pts/0. Now, all you have to do is punch the following like in your terminal:

cowsay -f tux "I see you" > /dev/pts/0

…and his terminal will look like this:

tux

Doesn’t this make you feel better about yourself?

Later edit: the party continues here!

An incredibly cool movie.

It’s funny because is true.

14 Apr

Coding music

Posted by Tudor. Tags: ,

What do you do while programming? And more important, what kind of music do you listen while programming? My habits are:

  • Starting a new project: Rammstein – Sonne
  • Taking over a project originally started by a junior programmer: Nightwish – Wish I had an angel
  • I’m assigned to 5 years old project that uses really old technologies and frameworks: Weird Al Yankovich – Amish paradise
  • Looking over a Perl script written by somebody else: Prodigy – Voodoo people
  • After a long meeting with the project manager: Zebda – Hasta siempre
  • Debugging (first 30 minutes): Rammstein – Ich will
  • Debugging (after 30 minutes): Prodigy – Fire starter
  • Looking for security flaws: MC Hammer – Can’t touch this
  • When I see a QA guy heading for my desk: Nana – He’s coming
  • When the project goes online: Britney Spears – Ops, I did it again (just kidding :P ) I’m having a brew and listen to Irish Rovers – Star of the county down

What do you listen to while coding?

25 Feb

The six stages of debugging

Posted by Tudor. Tags: ,
  1. That can’t happen
  2. That doesn’t happen on my machine
  3. That shouldn’t happen
  4. Why does that happen?
  5. Oh, I see
  6. How did that ever work?

via Mihai Brehar…and if debugging means taking the bugs out, then programming means putting the bugs in :P

Variable variables (or their official name – pointed out by Ionut Stan in the comments) or “dynamic variables” (how I like to call them) is the name of a feature of the PHP programming language which allows a programmer to define a variable that has its name given by the content of another variable. Like such:

$foo = 'bar';
/*
 * declare a variable called bar into the
 * current scope and initialise it with Hello world
 */
$$foo = 'Hello world';

echo $bar; // will echo Hello world

If find this really useless and very annoying, because a typo such as a double dollar sign in front of a variable can result in some very hard to track bugs. But this isn’t just another strange feature that made its way into the trunk. This is very, very well implemented and some programmers strived a lot to make it work under all possible circumstances. It works so well that you can use strings that cannot be normally used to represent variable names, such as !@#$%^&*()_+-=/*. Really, it works!

$foo = '!@#$%^&*()_+-=/*';
$$foo = 'Hello world';

$variables = get_defined_vars();
echo $variables['!@#$%^&*()_+-=/*'];
/**
 * will echo Hello world - the content of a variable called !@#$%^&*()_+-=/*
 */

Further more, it even works with objects:

class Example {
     /**
      * sample attribute
      *
      * @var string
      */
     private $value;

     /**
      * default constructor
      *
      * @param $_value
      */
     public function __construct( $_value ) {
          $this->value = (string) $_value;
     }

     /**
      * returns a string representation
      * of this object
      *
      * @return string
      */
     public function __toString() {
          return $this->value;
     }
}

$foo = new Example( 'bar' );
$$foo = 'Hello world';

echo $bar; // will echo Hello world

I think this one of those features reserved for the most gurus of the gurus, because after 5 years experience with PHP and a Zend Certification, I haven’t yet grasped the logic behind this…

At one time in our careers, we all worked as freelancers. It’s fun, you get to pick your work, no annoying project managers, no accounts managers, no managers at all. You’re your own boss. Just you and the project. And yeah, almost forgot, the client.

Because most freelancers are cowboy programmers that work from home, sooner or later the project will fall behind schedule. Reasons include proximity to the fridge (also has a devastating effect on the waist), games that are installed on that computer, movies and so on. What do you think most people will choose between eating a huge sandwich and drinking some beer while watching the new episode of Heroes and working? Yeah, my point exactly.

And when you’re behind schedule and the client asks you for a status update, you’ll do what every other moral person would. Lie about it! But try to do it with style and in a way that won’t get you caught. Most common lies are:

  • I’ve worked on the database” since nobody can actually check that
  • I’ve solved some security issues” again, really hard for somebody to actually check that
  • We have a small problem with the server’s configuration, I’m working on it now” somehow implies that is not your fault
  • I haven’t done the layout part yet, but everything else is ready” but I can’t show it to you without the layout now, can I?
  • My (retarded) neighbour flooded me, but don’t worry, your files are safe” should work, as long as he’s not your neighbour…
  • We’re almost there, I’ll set up a demo environment for you by tomorrow noon” like coping some files on a FTP host takes a day and a half
  • I’ve worked on optimization…riiiiiiiiiiiiight

And in the end, a meaningful quote: I did not have sexual relations with that woman – Bill Clinton.

Few weeks ago I was in the middle of a debate with my girlfriend – which is also a programmer – about the increment unary operator. The main topic was what will the following code display:

$i = 1;
$i = $i++;
echo $i;

I said 1, she said 2. Of course, I was right in the end. But we were having this conversation while we were heading out to meet some friends, so, inevitably, others were drawn into the debate. One of which is Costin, who posted on his blog an entry called The trials and tribulations of the C incrementer on this subject. Long story short, if you post increment a variable, this operation will take place last and its result will be poped first from the stack and in some cases will be lost (our case – read Costin’s post for more details).

Other annoying examples using the increment operator:

$i = 1;
function someFunction( $_value ) {
    echo $_value;
}

someFunction( $i++ ); // will display 1
someFunction( ++$i ); // will display 2

$j = 1;
$j = ++$j;
echo $j; // will display 2

And again, the ZCE exam features this kind of questions.

19 Jan

Variable interchange

Posted by Tudor. Tags: , , ,

How to interchange 2 variables? A simple question with a simple answer. Most people will do it like this:

// interchanging using a temporary variable
$a = 2;
$b = 3;

$temp = $a;
$a = $b;
$b = $temp;

echo $a . ' ' . $b . PHP_EOL; // will display 3 2

Quite elementary. But it can be done in a much more elegant manner, using arithmetical (addition & subtraction) or logical (xor) operators. Read the rest of this entry »

16 Jan

Language constructs fun

Posted by Tudor. Tags: , ,

What will the following code produce?

echo '1' . print( 2 ) + 3;

Tough question? Not quite. Just run the code and find out the answer. If you’re too lazy or don’t have a PHP server at hand, I’ll tell you the result. It will print 511 to the screen. Now comes the tough question. Why is that? We – me and my colleagues at Zitec – sat quite a while before figuring it out. It goes like this (special thanks go the Alex “the badger” Novac – see his pic on Zitec’s Team page): print is a language construct. It doesn’t need brackets to work. The following lines do the same thing:

print( 5 ); // will print 5
print 5; // again, will print 5

Thus, the line print(2) + 3 will translate as print 5, because the brackets are used “arithmetically” here, stating that their content should be evaluated first. Basically it happens like this: it evaluates (2) to 2, adds 3 and prints the result. This will give us the 5 on the first position. As stated in the php manual, print always returns 1. So after displaying the first 5, the code becomes echo '1' . 1. Pretty self explanatory.

If you want to take the Zend Certified Engineer exam, expect this type of questions.