Archive

Archive for May, 2009

This physically pains me

May 21st, 2009

Here’s a real excerpt (slightly modified to protect the stupid, and now fixed) from a web app I inherited. GRAAAAAAAARGH!

def encrypt_password
  ...
  self.salt = md5("#{Time.now.to_s}-#{login}")
  ...
end
 
def generate_confirm_hash
  ...
  self.confirm_hash = md5("#{Time.now.to_s}-#{email}")
  ..
end

Pro-tip for hiring managers: ask candidates to identify problems in the above code, and smack any who fail to do so.

Uncategorized , , ,

Pop quiz

May 4th, 2009

All right, class. Who can tell me what the following program will output? Bear with me, I promise this gets interesting.

1
2
3
4
5
6
int main ()
{
  int x = 0, y = 0;
  y = ++x + 10;
  printf("%d, %d\n", x, y);
}

That’s easy, it’s exactly what you expect: “1, 11″.

What about if you replace line 4 with the following?

y = x++ + 10;

Again, easy! This time, the expression x++ evaluates to the value of x before it is incremented, so the answer is “1, 10″.

Now, what about this?

1
2
3
4
5
6
int main ()
{
  int x = 0;
  x = ++x + 10;
  printf("%d\n", x);
}

This is no different than the first example, except that we’ve chucked the variable y, so the answer is “11″.

Last question: (and I’m sure you can see where this is going) what if you replace line 4 with the following?

x = x++ + 10;

 

I’m obviously trying to trick you, so you must realize that the answer is surprisingly also “11″, but can anyone tell me why it is so?

 

Extra credit: explain what happens if you replace line 4 with this seemingly-equivalent statement:

*(&x) = (x++) + 10;

Uncategorized ,