Tuesday, December 13, 2011

C++: I hate you, I hate you, I hate you. Leave me alone! Yet, I find you strangely attractive.

Had a really bad day with C++. Two strange bugs ruined it for me.

The first one had to do with the use of the ternary operator without proper parenthesis around it, while working with the shift operator on streams:

ostringstream ostr;
ostr << "bla bla" << x?y:z;
// This becomes
(ostr << "bla bla" << x) ? y : z;
// Which was simply becoming

ostr << "bla bla" << x;
A stupid bug like this inside a distributed app caused me to spend several hours. The bug was manifesting itself in a barrier operation, where different sets of distributed processes that were supposed to barrier independently, were barriering all at once, and the resulting incorrect counter was causing a deadlock!

Once I fixed the bug, another one followed. This time operator overloading and function overloading together were causing trouble:

void foo(X x) {}

template<class Y>
void foo(Y y) {}

And there is a class Z that implements an evil cast operator to class X.

In this setup foo(z) resolves to foo<Z>(z) but the coder has intended to call foo((X)z). The code used to work before the templated overload was added.

This was causing a rather strange segfault in a distributed app, and for some reason not showing up in standalone mode. After some more hours, this bug got cleared as well.

Given that I also spent a good deal of debugging and refactoring time yesterday with the evil problem of static object construction order (which is best eliminated via lazy initialization), as well as destruction order, I started to have some questions about C++. Not that I don't know all these problems. I do know them very well. But when there is so much depth and complexity in the application logic (multiple threads, database connections, forked processes, incremental checkpointing, cross-process synchronization, etc), having to deal with the warts of C++ becomes really annoying.

However, despite all its shortcomings, there is something about C++ that still attracts me. It is somewhat like enjoying the smell of your own armpit.

Friday, December 2, 2011

Democracy: Apply Directly Where It Hurts

The rhetoric about spreading democracy sounds so phony in light of recent history. While I believe that there are many people in this world who are kind enough to expend their resources for the benefit of others, such behavior is hard to come by in the realm of international relations. Spreading democracy often takes a backseat in the presence of other priorities. There are plenty of cases where oppressive regimes have been supported by the West for the purpose of advancing their agendas, at the cost of the people living under these regimes.

There is this saying, which I heard from my father, that goes something like the following in English: "What goes around will come around for those who are trying too hard to outsmart others." (Çok uyanık geçinenlere döner dolaşıp girermiş.) When we look at examples of foreign intervention in politics and internal affairs of countries, we repeatedly see that in the long term there are unexpected consequences of such efforts.

Let's start with Egypt and the so called Arab Spring. An Arab nationalist, Soviet-leaning Egypt under the rule of Abdel Nasser has been transformed into a pro-American Egypt with normalized relations to Israel under Enwar Sadat. I don't see any problem with this personally, as I have no sympathy for the Soviets. Also, Enwar made a good deal: getting back the critical Sinai Peninsula that was lost during the 6-day wars, in return for recognizing Israel. He paid it with his life. What I don't like though, is the continued US support of the Mubarak regime that followed, in the name of maintaining 'stability' in the region. While I understand the concern for the rise of Islamic extremism, which may impact the so called 'stability' (especially with respect to Palestinian-Israeli conflict), I don't see how supporting an oppressive regime with billions of dollars of aid each year helps make the extreme elements in opposition any less extreme. Martial law for 30 years! So much for spreading democracy. It is not yet clear whether the US support of Mubarak in the past will come back to bite them or not. But if Pakistan and Iran are any indication, it may very well do.

Now let's look at Pakistan and see how Islamic extremism was fueled by US and was not considered harmful, well, until it was. There is this idiom in my mother tongue, that goes like: "This is not a diet, nor a cabbage pickle." (Bu ne diyet, bu ne lahana turşusu.) You can't have a low-sodium diet and have a salt-packed pickle at the same time! Back to Pakistan. Zulfiqar Bhutto who had socialist economic policies was ousted with the help of US, and General Zia took control in a military coup. With the Soviet invasion of Afghanistan, US and Saudi Arabia have provided financial support for the Zia's regime to train large number of Mujaheddin against the Soviets. Under General Zia, the number of Madrassas's (religious schools) in Pakistan has skyrocketed. Students graduating from these schools had no skills to find a job in the market, and have been channeled to join the Mujaheddin to fight against the Soviet invasion in Afghanistan. They later formed the roots of the Taliban. And we all know how that turned out.

In Iran, the democratically elected Iranian government of Mosaddegh was overthrown in a coup. British and the US were behind the move, which was a consequence of Mosaddegh's nationalization of the oil industry that was being run by a British-owned company until that time. Consequently, the US became a major backer of Shah M. Reza Pahlavi, whose monarchy has become increasingly corrupt, extravagant, and out of touch with the general public in terms of Iranian culture and (Islamic) traditions. This gave rise to the Iranian revolution, and today theocratic Iran is one of the most oppressive regimes around the world. Not to mention the whole Iraq/Iran war and the rise of Saddam as a balance against the Islamic Republic of Iran. The Gulf War and the Iraq war that followed have further complicated the whole situation. Unfortunately, two wrongs do not make a right, and in this case many many wrongs did not make a right, at least not yet. Today the sanctions against Iran further isolate the country. It won't help with nuclear proliferation either, as North Korea is a proof to that. Furthermore, the opposition groups within Iran that are already being deprived of their social movement rights may interpret these sanctions as abandonment by the West.

May be Ron Paul, who is seeking to be the Republican Party candidate in the upcoming elections, is right. He says that "America [should] not interfere militarily, financially, or covertly in the internal affairs of other nations", while advocating "open trade, travel, communication, and diplomacy with other nations". Food for thought.