Tokens — Page 10

Variable Hoisting in JavaScript

javascriptprogramming

If you are programming in JavaScript, the odds are that you come from C/C++/Java background and have been bitten by the web development bug. This might cause a couple of problems however, mostly because JavaScript is “Lisp in C’s Clothing”. So you might find yourself a little disoriented with the familiar syntax and sometimes very unintuitive execution. One thing that is not quite so easy to understand is the fact that functions are first class in JavaScript and that they do create closures. Couple that with lexical scoping and you can get some really unexpected results.

Connected to the concept of functions being first class is the notion of variable hoisting. Dictionary definition of “hoisting” is lifting or raising something up. And this exactly what JavaScript engines do. Any variable that is declared (be it initialized to a primitive data type, an object or even a method) anywhere in a function will actually be run as if they are declared in the beginning of that function. That is, they are raised to the top of the scope they are in. Initializations for that variable will not run however, since this is not what you normally expect, and because it would have caused rather unexpected run time peculiarities.

Consider the following JavaScript snippet:
<pre>
function foo() {
// Some other stuff.
// blah blah blah

if (false) {
var myVar = "bar";
}

alert(myVar);
}
foo();
</pre>

When you run above code, you will get an alert box with “undefined” in it. Even though what’s inside the “if” conditional is not executed, the variable is nevertheless declared in the beginning of the function. So, in effect, the above code is equivalent to:
<pre>
function foo() {
var myVar;
// Some other stuff.
// blah blah blah

if (false) {
myVar = "bar";
}

alert(myVar);
}
foo();
</pre>

Note how the variable is taken and declared at the top while the initialization is left where it was.

But wait, there’s more. Since the condition for the “if” is never true, execution does not enter inside the block, and thus it doesn’t matter where it is placed in our function. Thus, below is equivalent to the two code fragments above:
<pre>
function foo() {
// Some other stuff.
// blah blah blah

alert(myVar);

if (false) {
var myVar = "bar";
}
}
foo();
</pre>


Contrast above executions with the below where you’ll get a ReferenceError exception because myVar has not been declared.
<pre>
function foo() {
// Some other stuff.
// blah blah blah
/*
if (false) {
var myVar = "bar";
}
*/
alert(myVar);
}

foo();
</pre>

Variable hoisting is actually quite logical when you think about it, but you have to get into the mindset of functional programming. And because of such properties, variable hoisting is asked a lot on JavaScript quizzes found on the web.

I have to admit that the above example does not seem very likely in any code, but believe me you will one day come across it if you start coding complex JavaScript applications. When you do find yourself with bugs caused by hoisting, you might also understand the reason behind the coding convention where all variables are declared at the top of the scope, even if they are used at the very bottom. Don’t be afraid though, JavaScript is still a beautiful language, if you try to understand it and use “the good parts”.

PHP Sessions

phpprogramming

I recently found out that the directory we kept PHP session files had more than 2 million files. Now that’s a lot of files and even though it is less than ~55 million maximum allowed on a typical linux system, we had to act fast. On Linux boxes number of files allowed is determined by the number of inodes and this can be viewed with the following command:

$ df -i

Which will give you number of inodes you are using and how much more you can use.

I went through PHP documents on sessions and checked our PHP configuration file php.ini. It turns out that Debian systems does not want PHP to do its garbage collection itself and tries to remove old sessions using a simple cron job. The problem was we were using another directory for storing sessions and not the default “/var/lib/php5”. So PHP wasn’t removing old sessions because “session.gc_probability” was set to 0 by Debian and Debian cron job wasn’t removing these sessions because the sessions were not in their default location. We enabled PHP’s garbage collector and modified the cron job so that it uses the correct folder now. Now we have two checks in place for removing old sessions.

This is something to keep in mind if you find yourself with a lot of session files. Hope this helps others.

Web Teknolojileri Nereye?

teknoloji

</embed>



Bugün dışarıdaki harika havaya rağmen ODTÜ Teknokent’teki sunumuma gelen herkese teşekkürler. Sunumumu yukarıya koyuyorum, ancak konuşma odaklı bir sunum olduğu için pek işinize yaramayabilir. Sunumumu yazılarla doldurmak istemedim :) insanları konuşmaya odaklanmaya zorlamak daha iyi sanırım.

Bir de şahsi fikrim, onca saçmalığa rağmen bu ülkede duruyorsak bir katkıda bulunmamızın gerekliliği. Bilgiyi paylaşmalı, biz de eğitimli kesim olarak katkıda bulunmalıyız. Web sektörünün gelişmesi herkesin işine gelecektir.

2010 Türkiye'de Japon Yılı

sanatphotography

Büyük olasılıkla haberiniz yok ama 2010 Türkiye’de Japon Yılı aslında. Bu yüzden birçok şehirde çeşitli etkinlikler düzenleniyor. Perşembe günü Neo-Japonesque gösterisine Japon arkadaşımın -sonunda!- daveti ile gittik. Dört kişilik çok güzel bir gösteriydi ve fotoğraf makinemi unutmadığım için güzel fotoğraflar çekebildim.

Ankara’daki gösteri Oran’daki Türk-Japon Kültür Vakfı’nda gerçekleşti. Arkadaşla salona girdiğimizde oturacak yer yoktu, neyseki elçilik görevlilerinden birisi bizi (daha doğrusu sadece Kohei’i) gördü ve bizi loca odalarının bulunduğu alana götürdü. Onlar da olmasa ayakta izleyecektik galiba.

İlk gösteri kaligrafi gösterisiydi. İzlerken aklıma Hero filmi geldi, orada da kaligrafi okulundan sahneler vardı. Kullandıkları fırçalar gerçekten büyük :)

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://2.bp.blogspot.com/_AZvuJ9kmERM/S5Ity_DOksI/AAAAAAAABcc/bjgLBNv0cuc/s320/P3045216_2.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445465253386031810” />

Kullandıkları renk kırmızı olsa daha dikkat çekici olurdu diye düşünüyorum, ve sanki Hero’da da kırmızı renk kullanılmıştı (tabi Hero’nun Çin filmi olduğunu söylemeliyim). Kohei’ye sorduğumda genellikle siyah kullanıldığını söyledi. Çizimler, bittikten sonra yavaş yavaş yerlerine çekildi.

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://3.bp.blogspot.com/_AZvuJ9kmERM/S5Itza7VWKI/AAAAAAAABck/EEgyMwicrIw/s320/P3045219_1.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445465260869114018” />

Karakterlerin ne anlama geldiğini hatırlamıyorum doğrusu, sanki en sağdaki “rüya” demekti ama emin değilim. Kohei’ye sordum, o da bilmiyormuş, çok eski karakterler olduğu için anlamıyor. Çin’den gelen karakterlerin bir kısmı Japonya’da halen kullanılıyor, buna Kanji alfabesi deniyor. Kohei’nin dediğine göre bir Japon çocuk orta okula kadar filan gazete okuyamıyor, zor alfabe yüzünden. Anladığım kadarıyla zor karakterler kullanmak onlar için “erdem” ifade ediyor.

Kaligrafi karakterleri sahnenin arkasını süsledikten sonra müzik gösterilerine geçildi. İlk konser, sıkı durun, marimba gösterisiydi. “Marimba da neyin nesi” diyebilirsiniz, çünkü ben de ilk öyle dedim. Marimba ksaylafona benzeyen vurmalı bir çalgı, ama ksaylafonun çok daha büyüğünü düşünün. Çok dinlendirici bir sesi var. Çalan sanatçı Nanae Mimura ve anladığım kadarıyla çok ünlü birisi. Konserden önce bizim oturduğumuz locayı gezdi ve bizimle oturan elçilik görevlileriyle sohbet etti. Hem o hem de elçilik görevlileri Türkçe bilmiyordu, ben de Japonca bilmeyince yurt dışındaymış gibi hissettim :) Sanırım konser salonunu yukarıdan incelemek istedi. Ne yazık ki ışık miktarı çok azdı, yoksa hazır elimde de 50mm f2.0 lens varken bir portre fotoğrafı için izin isteyecektim. Başka zamana artık :)

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://4.bp.blogspot.com/_AZvuJ9kmERM/S5Itzxfg9zI/AAAAAAAABc0/UkZ3lS6ffMo/s320/P3045229_1.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445465266926450482” />

Nanae Mimura’nın YouTube videolarından birini izlerseniz neler çaldığını görebilirsiniz:

</param></param></param></embed>

Mimura-san en sonda iki tane Türkçe parça çaldı ve seyirciler çoştu gerçekten :) Çok duygulu çalıyor, işini gerçekten iyi yapan birisi. Zaten genel olarak herhangi bir işi çoşkuyla, severek yapıyorsa bir insan, yanlış yapıyor olamaz.

Bundan sonra soprano Satomi Ogawa çıktı sahneye. Opera ile aram çok iyi sayılmaz ama bazen ilginç şeyler çıkıyor gerçekten (opera ile biraz bile ilgileniyorsanız şu RadioLab podcast’ını dinleyin). Bu konserde de piyano ile birlikte söylenen şarkılar çok güzeldi. Satomi Ogawa 1999’da Kainat Güzellik Yarışmasında Japonya’yı temsil etmiş, ve aynı zamanda blog tutuyor :)

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://3.bp.blogspot.com/_AZvuJ9kmERM/S5It0IRuAvI/AAAAAAAABc8/4QVMCHnwyRc/s320/P3045250_1.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445465273042600690” />

Satomi Ogawa’yı dinlemek isterseniz YouTube’daki parçalarından birine yönlendiriyorum sizi.

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://2.bp.blogspot.com/_AZvuJ9kmERM/S5I9_RKQIYI/AAAAAAAABdE/3nYia7OcZC0/s320/P3045258_1.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445483056591806850” />

Zoe Keating’den çello dinlemeden önce favori enstrümanım kemandı :) Ve bence keman bu konserin en güzel aletiydi. Sanırım bunda İkuko Kawai’nin dünyaca ünlü bir keman sanatçısı olmasının da payı var :)

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://4.bp.blogspot.com/_AZvuJ9kmERM/S5I9_hZVkHI/AAAAAAAABdM/hBx6LfZAUcY/s320/P3045269_1.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445483060950044786” />

Kawai-san’ın kullandığı keman, 1715 yapımı bir Stradivari.

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 300px;” src=”http://3.bp.blogspot.com/_AZvuJ9kmERM/S5I-AYFJLII/AAAAAAAABdc/EhGy7Xshseg/s320/P3045297_2.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445483075629296770” />

Keman bence gerçekten büyüleyici bir çalgı :) Kawai-san’dan bir parça koymazsam yazı tam olmaz sanırım:

</param></param></param></embed>

Ve konser bitimi.

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://3.bp.blogspot.com/_AZvuJ9kmERM/S5I-AKwu0oI/AAAAAAAABdU/7BfEmQw2v_s/s320/P3045285.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445483072054022786” />

<img style=”display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 240px;” src=”http://3.bp.blogspot.com/_AZvuJ9kmERM/S5I-AlM4TRI/AAAAAAAABdk/zxTiGnxIZV4/s320/P3045304_1.JPG” border=”0” alt=”“id=”BLOGGER_PHOTO_ID_5445483079151406354” />

Eğer dinledikleriniz ve gördükleriniz hoşunuza gittiyse 6 Mart Cumartesi günü Bursa’da ve 8 Mart Pazartesi İzmir’de bu konserleri izleyebilirsiniz. Mekan ve detaylı bilgiler japonya2010.org sitesinde.

Not: Yazıdaki videoların hiçbiri benim değil, yazıdaki tüm fotoğrafları ise ben çektim.

Great Speeches are Inspiring

politics

[...]
Rarely in any time does an issue lay bare the secret heart of America itself. Rarely are we met with a challenge, not to our growth or abundance, or our welfare or our security, but rather to the values and the purposes and the meaning of our beloved nation. The issue of equal right for the American Negroes is such an issue. And should we defeat every enemy, and should we double our wealth and conquer the stars, and still be unequal to this issue, then we will have failed as a people and as a nation...


Lyndon Johnson’s speech on human rights issues of the blacks. From the book “Obama’s Challenge” by Robert Kuttner.

It’s a great speech winning the hearts and minds of ordinary people. As a foreigner, it is quite dazzling to me that the blacks could not vote until well into the 1960s, that they were deprived of many basic human rights that we take as granted today.

I’m also excited to see that Turkey has a long path to go and I hope my generation will lead her, when the time comes.