How does a FOR loop work?

During a recent round of interviews we asked candidates to point out the problem with the following code snippet

for($i = 0; $i < 10; ++$i)
  if ($i = 0)
    print "yes";

This was intended as a simple, spot-the-typo, exercise. The problem is if ($i = 0), should read if ($i == 0)! As is, it's an infinite loop, and the follow up question is to ask if it's infinitely printing "yes" or not.

Relatively easy and straightforward. What happened in the actual interviews was anything but straightforward. Without fail, candidates kept pointing out the pre-increment ++$i. My first thought was to put them back on track, because "no, that has nothing to do with it." Instead I decided to explore.

"Wait, tell me why the pre-increment would be a problem?" I asked curiously.

Every candidate seemed to have a different explanation for why a pre-increment wouldn't work. I would then ask them to walk through the execution of the loop. And this was the mistake that each candidate shared, they didn't know how a standard three-expression for loop worked.

Stubbornly, many would insist that the pre-increment would be run first, before the assignment statement. I would ask, "so the execution order of the three expressions is dependent on the third expression?", hoping they would see the absurdity in such a claim. "Yes," was the confident reply. sigh

"No," I would explain (feeling a little defeated), "the first expression is always executed first and only once, the third expression is only executed after a loop iteration."

I would go on to explain that the pre-increment works perfectly fine. In fact, there is some argument that it is more efficient since it is not creating temporary variables (a post-increment will create a temporary variable to hold the original value that is returned from the statement). Either way, in the context of a three-expression for loop it makes no difference; whichever method lends itself to better readability in your code should be used.

None of the candidates who missed this question were considered for a position.

And what did I learn? That we should use foreach with Iterator objects, and I'm switching to Python!

Synesthesia

I was reading about Dual-coding Theory, which states that visual and verbal information is processed on distinct and different cognitive channels. The verbal cues refer to symbolic codes; in other words, arbitrary representations of something (such as reading or listening to a speech). The visual cues are perceptual, that is, a direct representation of what you are seeing.

Dual-coding refers to utilizing both visual and verbal, that is, symbolic as well as perceptual codes. Memory seems to favor the visual channel, and this is known as the picture superiority effect.

Think of watching the presidential debates. The information is symbolic (through words) as well as visual. What we remember tends to favor the visual, which can then act as a voluntary trigger to retrieve the words.

This is an interesting concept, and taken further leads to synesthesia– a rare phenomenon where one cognitive pathway triggers another involuntarily. For example, someone with synesthesia may see numbers in different colors; or see musical notes. And not a voluntary association, but a completely involuntary reaction. The advantages should be obvious, such as encoding symbolic information into long-term memory as easily as a visual information.

I wonder, if with the right training, we could develop in ourselves something like synesthesia; associating visual/perceptual codes to symbolic codes — allowing perceptual representations of abstract concepts.

We tend to do this naturally with metaphoric associations, such as an emotional reaction to an image or sound. I suspect we can take this much further, where we directly leverage perceptual codes to involuntarily trigger any number of symbolic/abstract information (whether emotional, or intellectual, or artistic)

Bad Hair Day

“you’re getting a haircut,” my friend announced

My friends, wiser than I am, have been conspiring against my dreams of long luxurious hair. In my head it looked awesome. The reality, I suspect, was a disheveled mop that was starting to make me look homeless

Always trust your friends in these situations. We all live in these wonderful dream worlds of our own design; mine is made possible by pragmatic friends.

Choose friends wisely. They are the gatekeepers between your delusional fantasies and reality

Racism

This is the second time I’ve taken the Implicit Association Test about racial equality. [1] And again, I’m one of the 4% with an implicit racism against white people. From the test:

Your data suggest a moderate automatic preference for African American compared to European American

I retook the test a few times. I was trying to game towards neutrality. The results were the same differing only between moderate and slight automatic preference for African Americans compared to European Americans. I guess I’m still racist against white people.

I’m never sure what to make of this. I grew up in suburban white America. I love Friends and Seinfeld. And while I consider myself neutral to race and culture I’m not that surprised I carry some implicit racism. Most people carry these unconscious thoughts. Our claims to neutrality are usually covering up our own bias.

This kind of bias is always troubling, although I can’t help but to wonder why I have a bias against people who look like me. I was happy to see that there were 12% that actually were truly neutral.

Any theories? Please share them! The test is extremely interesting and I would highly recommend taking it

References

1. http://www.understandingprejudice.org/iat/

Anchoring and Affect

One of the most interesting and oft-cited cognitive heuristics is the Anchoring and Adjustment heuristic. A fascinating example is the mock auction biased by the last two digits of the participants Social Security number:

Dan Ariely, professor of management science at MIT Sloan School of Management, conducted a mock auction with his MBA students. He asked students to write down the last two digits of their Social Security numbers, and then submit bids on such items as bottles of wine and chocolate. The half of the group with higher two-digit numbers bid “between 60 percent and 120 percent more” on the items, says Ariely. [1]

A similar heuristic is the Affect heuristic. An “affect” in this case is a feeling of “good” or “bad”, a simple emotional response to any stimulus. An interesting examples of an Affect heuristic is the positive correlation between New York City weather and major stock indexes. [2] In other words, the affect of the weather on investors effects asset prices. A not so subtle reminder of how irrational our rational minds behave.

Much has been written about overcoming and compensating these types of cognitive biases. I would like to propose something radically different. Embrace these biases, use them to your advantage! The beauty of these mental heuristics is that even when we are aware of them, we are still victims to these biases. I’ll write more on our bias blindspot in a future essay.

Everything you do, whether you like it or not, is already anchored. Various inputs, including the weather, create an anchored affect and biases every judgment throughout your day. It’s self-centered and lazy to go through life at the whims of the weather, and everyone else’s mood.

Before you go into an important meeting or negotiation, be prepared with an anchor. And not just to anchor other people you are talking to, but anchor yourself! And it’s easy– the anchor need not have any relevance. A positive anchor is anything that makes you happy and provides a “good” affect, and a negative anchor is anything that provides a “bad” effect. This could be a picture you carry, a song in your head, a vivid memory, anything. It doesn’t even need to be in your conscious awareness, so perhaps carefully selected background music might be appropriate while you are working.

The key is to recognize when to employ a positive affect and when to employ a negative. A positive affect is likely not a wise choice if you are negotiating for a low price or trying to assess risk in a project. Likewise a negative affect could be devastating if you are trying to sell an idea or discover something new.

References

1. http://www.cfo.com/article.cfm/3014027
2. http://heuristics.behaviouralfinance.net/affect/Saun93.pdf

Beard Experiment: Fail

A beard experiment is a wonderful project. A project that requires curiosity and a strong resolve of laziness.

It is a daunting challenge. “I really want to shave, I look horrible,” I told myself repeatedly.

“This itches, I hate it!” Yet my willpower for laziness prevailed. Dreams of full beards and creative displays of mutton chops with handlebar mustaches danced in my mind. “It’ll be worth it,” I reminded myself.

Two weeks in– a startling revelation: I can’t grow a beard. I’m 32 years old and I can’t grow a beard. I’ll never be able to grow a beard. I’ve been drifting through life in denial. Had I lived in a Taliban controlled Afghanistan I would have been imprisoned for life or possibly put to death.

I know there are others like me; and I know some of you are brave enough to persist with these cursed boy-beards attempting to make it a fashion statement. Stop. It’s not cool. It’s like a short-guy puffing his chest out trying to intimidate. Sure, some celebrities have gone this route, like Johnny Depp, Keanu Reeves, or John Mayor. And in all cases, while we can say “that doesn’t look too bad”, no, in fact it does look bad– or at least worse than without the beard.

It’s times like these that we need to listen to the women in our lives. Now if you’ll excuse me I’m going to go home and shave.

Aspergers and LEGOs

The Autism Research Centre at the University of Cambridge recently released a fascinating paper on LEGO therapy. You read that correctly: LEGO therapy! Awesome! The study involved 6-11 year olds with either high functioning autism and/or Asperger Syndrome. [1]

I am suddenly reminded of my own awkward social skills as a child and how amazing it would have been to actually receive therapy involving LEGOs rather than punishment for not paying attention to anything except LEGOs.

LEGOs are like crack if you have Aspergers. On one hand it can be an amazing tool to motivate social interactions and develop spatial reasoning. On the other hand, it’s kind of a cruel joke taking an obsessive detail-oriented ‘perger and putting them in front of LEGOs– a narrow area of interest to focus on without any required speaking or physical dexterity. To this day I continually resist the urge to quit my job and focus full-time on LEGOs.

I never considered the social implications of LEGOs; 90% of my LEGO experience was solitary and became a coping mechanism to deal with the depression from not having any friends. There were however several friends I made who shared my passion for LEGOs — it’s possible that those were crucial moments helping me to overcome my social anxieties.

References

1. http://www.ncbi.nlm.nih.gov/pubmed/18566882