Friday, October 9, 2009

What You Have You Keep

The second law of thermodynamics states that in an isolated system, entropy is an increasing function. In his book, Do Androids Dream of Electric Sheep, Philip K. Dick talks about fighting entropy, or Kipple, as he calls it:
Kipple is useless objects, like junk mail or match folders after you use the last match or gum wrappers or yesterday's homeopape. When nobody's around, kipple reproduces itself. For instance, if you to go bed leaving any kipple around your apartment, when you wake up there is twice as much of it. It always gets more and more.

No one can win against kipple, except temporarily and maybe in one spot.

I've found that there is much truth in the Kipple Theorem. Just think of your house, which contiguously gets more cluttered over time. The same also happens to source code, if left to its own devices. It is also true of memory, which gets disordered over time. What was once clear and orderly, becomes obscure and messy.

Over the past couple of weeks, I've tried to always put something back in its place whenever I go from one room to another. When I do this with disciple, my house starts to look much better. A couple of days of slacking off, and the house looks once more like a total mess. And I don't ever remember making it a mess. It's just that kipple does multiple while you're asleep. Unless we take active, continuous action to combat it, kipple will always win.

Source code is no different. It grows smelly over time. No matter what your source control's log tells you, changes are constantly taking place. They make the code more unreadable, make methods more incomprehensible, and occasionally even introduced some bugs in unimportant locations.

The same is true when learning things, such as Japanese. I've studies nearly 400 kanji characters over the last couple of weeks. Unless I take active steps to maintain what I've learned, my neurons start getting all bogged down with kipple. That's why I am very fanatical about reviewing the items in my SRS software every day. Left alone, for even a little bit, the kipple raises its ugly head and starts to take over.

Kipple Fighting 101

When you learn chess, you basically have to study three different stages - opening, middle game and end game. For the opening stage, you basically memorize the known openings, which have already been studied thoroughly. During the middle game you need to understand basic principles and how to implement them. The end game part is usually kind of a mixture of the previous stages: memorize principles for different scenarios.

One of the most basic scenarios in the end-game is the one where you have a king and a rook and the opponent has only a king. It is very easy for you to win now. But, to make things explicit, the guiding principle is to always prevent the enemy from returning to territories from which he retreated. When the enemy king moves, you make sure he cannot move back to where he once was. You corner him into closer and closer areas.

In the endless chess game against kipple, the opposite is true:

What I have, I keep!

This means an endless game of maintenance. You've learned a new word - make sure you review it periodically. You finished cleaning the kitchen - make sure it stays clean. Constant vigilance is the key to winning this battle.

Whenever you go over source code, you will encounter kipple. It is your responsibility to engage it immediately. No matter what your current objective is, you must spend some of your time chasing and destroying the kipple. Otherwise, the kipple will take over the code base, and before you know it, no one on your team has a clue what does what.

Monday, October 5, 2009

Kill the CAPS LOCK!

The Caps Lock key can do wonders to wreak havoc in VI. You're in command mode, minding your own business, when all of a sudden all hell breaks loose. All because you pressed the caps lock key instead of the shift key.

For a long while I was reluctant to follow the plethora of tutorials on how to kill the caps lock key. The reason is that there are several cases where I thought it was very beneficial to actually use it to, well, lock the caps. For example, by tradition, definitions in C/C++ are written in all capital letters. I finally figured out how to get along without the evil key, as I will explain below:

g~w - capitalize one word
g~~ - capitalize the entire sentence
gU{motion} - capitalize motion

I still have some open issues. For example, to capitalize the word I just wrote, I have to go backwards ('b') and then capitalize it ('g~w') and then go back to insert mode ('A'). I'll be happy to learn of better options, options that take into account that I despite having to configure my setup. Nonetheless, I'm now much better off than I was with the occasional caps lock accident.

You can now make the caps lock key do something else, such as behave as a shift key or an esc key.


Further Reading:

Wednesday, September 30, 2009

Exercise

I've written before that I have the great privilege of working with people who are much smarter than myself. After more than a year of working with some ridiculously smart people, I've come to the following conclusiong:
Smart people exercise more than average people.
This doesn't mean that every smart person I know exercises regularly. I know quite a few ridiculously intelligent people who don't. But, from what I can see, the average amount of smart people who exercise is much higher than in the average population.

I've been exercising more regularly lately, and my coworkers contribute much to it. As a matter of fact, I just went for an 8km run with half of my group. It turns out that some guys with a PhD in Computer Science can be pretty competitive and this forced me to push myself.

It does feels great after wards, tough.

Promise!

Tuesday, September 29, 2009

Learning Japanese - Spaced Repetition Software

Note: this article is part of a series on how to memorize
material. You can start at:
The classical method for reviewing items you have already memorized is using flashcards. The basic technique is to create a flashcard with a question on one side of the card and the answer on the other side. Each day, you'd review several cards. For each card, you'd read the question side, try to recall the answer, and then compare your answer with the one written on the backside of the flashcard. However, this method has a major limitation:
After some time, you'll have a large "database" of several hundred and even thousand cards. It is not practical to review all of them at once. How do you know which cards to review?
 Several better techniques have been developed over time. For example, in the 1970s, Sebastien Leitner developed the system now named after him. Using this method, you'd have several numbered decks. Each time you succeed in recalling a flashcard, that card will travel to a deck with a lower priority. On the other hand, if you fail to successfully recall the card, then that card would move to a higher priority deck.

You would now schedule your reviews according to the priorities of the decks. For example. each day you would review cards from the highest priority deck, every other day you'd review cards from the second highest priority deck and so forth. This better optimizes your time - you would spend less time reviewing cards you can easily recall, and more time reviewing cards that are hard for you.

This was no doubt a major improvement. In the computer age, however, we can optimize this method much further. Computer programs which do this are called Spaced Repetition Software (SRS). The first program, of which I'm aware, that pursued this task is SuperMemo.

The basic idea of all the SRS programs are to schedule cards for review at a time in which the review will have an optimal benefit in entrenching the fact in long term memory. When you first learn a card, it is scheduled for review quite often. After each review, the program asks you to rate how easy it was for you to recall the fact. Using this data, the program can optimize the time for the next review.

The theory is that the best time to review a card is just when you're starting to forget it. If you have to sweat a little to recall the card, but you do manage to remember it, then you'll remember the card for a long time. The idea is to always live on the edge. Never have a feeling that life is easy. As in most things, this gives you great benefits - namely, long term recollection.

I've used a couple of SRS programs over the past few months. At first, I used mnemosyne, which is a cross platform, open source program. Overall the program is pretty easy to use and very streamlined. You aren't presented with too many options, and you can begin using it immediately. It also has many plugins and existing decks which you can download. The downside is that it is not very configurable, which I for one actually like. It was also a pain to install on an old PowerPC mac, but for Intel machines the installation is very easy. Finally, adding audio and graphics to the flash cards is pretty annoying.

After spending around a month away from computers, I found it was very difficult to return to using mnemosyne. This is actually a problem with all SRS programs. The algorithm requires you to use it at constant time intervals for best performance. After using it daily, a month of absence will take many weeks for the algorithm to overcome. That's why I decide to start fresh and switch to a competitor - Anki.

Anki has very similar features to mnemosyne. It also has many plugins and existing decks. However, it looks much better visually, everything about it can be configured, sound and graphics are a snap to insert and it has mobile and online versions. It can even let you synchronize your decks across multiple computers. As a final bonus, it is updated much more frequently than mnemosyne.

I've now been using Anki for a couple of weeks. This is definitely not long enough to get a feel of its scheduling algorithm. It takes many weeks of daily usage before you can start to experience the benefits of any SRS algorithm. From what I can tell, it looks like a really solid program so far, and I'm happy with it.

Further Reading
  1. Learning Japanese - Memorization: the openning article in this series.
  2. Learning Japanese - Vocabulary: shows a nifty plugin for Anki that can really help you learn your vocabulary.
  3. Configurability: what I think of configurability.

Monday, September 28, 2009

iPod Touch

I ordered myself an ipod touch the other day.

I already have an old ipod shuffle, which I take with me on runs. I also use it to listen to pod casts on long car drives. I can say I'm very pleased with it.

The question is now:

Why?

Why did I order the ipod touch. The answer is that not so much for the music. It is certainly a big step up from the shuffle (which will continue to serve me when excercising), especially having my entire music library and all the podcasts available at whim. Nonetheless, I didn't order it for the music.

I ordered it because it looks like a good portable computing device. No, I don't intend to program on it. I intend to read ebooks on it like I did with my ancient Palm Tungsten E until it passed away. I intend to finally organize my life in some sort of calendar (maybe I'll finally know what happens with me in the weekends). I'll definitely look into some GTD organizer. Some Japanese learning app will definitely find its way in there. I'll probably think of some other useful uses.

And that's the reason I'm buying the touch - because it is extensible. I'm sure a year from now I'll find a dozen more good uses for it.

Too bad it doesn't have a camera...

Sunday, September 27, 2009

Japanese - Vocabulary

I'm currently studying Japanese words with the "Core 2000" list of words on smart.fm (free registration).

Each new word is introduced on smart.fm's iKnow system, which provides some nice features. Each word includes:
  1. The meaning, in english.
  2. The writing in kanji with stroke order.
  3. An example sentence showing the word used in context.
  4. An audio recording of a native speaker reading the sentence.
After showing you some words, the website goes on to drill you about it. So far, I've found that it is best to spend some time alone with each word, trying to memorize it, and only then proceed to the drill stage. Another problem with the drilling is that it appears geared mostly towards short term memory. This is where a nifty tool comes in.

In order to remember the words long-term, I study these new words using an SRS program, namely, Anki. The really great thing is that Anki has a plugin which can automatically import data from smart.fm. This means that in a matter of minutes I can have all the words, example sentences and recordings integrated into Anki.

Here's how it's done:
  1. Install Anki.
  2. Install anki-iknow-importer.
  3. In Anki, choose Tools/Smart.fm Importer.
  4. Fill the two dialog boxes. The most important is the address of the Smart.FM list.






That's pretty much about it. I can now review the words and sentences over time, and Anki will make sure my learning process is optimized.

Further Reading
  1. Learning Japanese - Resources
  2. Learning Japanese - Memorization

Saturday, September 26, 2009

Python, Subprocess and Multiple Arguments

Calling external programs from within a python script is a pretty common task. However, most of the times when I want to pass some arguments to the external program, I usually get stuck. The official Python documentation for the subprocess module is a bit lacking, in my humble opinion, in its treatment of argument passing.

This is not to say that all the information isn't available in the official documentation. In fact, it is. It's just not where I always look for it. The first thing I look for are examples. I simply do not have time to read the entire documentation page for something which should not be too difficult.

The basic idea is this:
  1. Either provide a string of the entire command and argument, but use Shell=True
  2. Provide a string for only the command
  3. Provide a list of strings, where the first item is the command and the other items are the arguments
It's the third option that keeps on giving me trouble, so here is a simple working example for reference:
 1 #!/bin/env python
 2 import subprocess
 3 
 4 program_name = "ls"
 5 arguments = ["-l", "-a"]
 6 
 7 command = [program_name]
 8 command.extend(arguments)
 9 
10 output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
11 print output


Passing Arguments with Quotes
If you want to pass arguments that are passed with quotes in the shell, then just pass them as a single list item, without the quotes.

Links
  1. James Gardner wrote the most comprehensive introduction to the python subprocess module I've encountered.
  2. The official documentation.

Further Reading
  1. Bash: Passing Arguments with Quotes
  2. Why Not to Write Shell Scripts

Friday, September 25, 2009

Learning Japanese - Resources

Selected Articles

  1. Memorization - how to memorize kanji, words and grammar.
Guides & Blogs
  1. All Japanese All the Time - this website has tons of articles about how to learn Japanese by having fun and immersing yourself in the language 24/7. The author claims to have learned Japanese in 18 months while studying for a computer science degree in an American college.
  2. Nihongo Pera Pera - includes many articles on how to study Japanese by yourself. Includes many tips, especially on how to use the computer to its fullest potential.
Grammer

  1. Tae Kim's guide to Japanese grammar contains an excellent guide with plenty of examples.
Computer Software

  1. Smart.FM - for a free registration, you get access to tons of free online material. I'm currently studying a list of 2000 core Japanese sentences. The online learning system, called iKnow, has example sentences and recordings by native speakers. See my article on how to use it to learn vocabulary.
  2. Reviewing the Kanji - this website is a great companion to Remembering the Kanji. You can select how many lessons you've already learned, and then quiz yourself. The real bonus is that for each character, you have access to user created stories. These stories can be voted on, and this means that you can easily find some really great stories!
  3. Anki or Mnemosyne - both of these are completely free space repetition software (SRS). Anki is very polished and overflowing with options, whereas Mnemosyne is much simpler. Both offer a wide collection of preexisting cards. Anki also has version for some portable devices and a free online account which can synchronize between several computers.
  4. Rikaichan - an excellent firefox plugin that gives you information on kanji and words whenever you hover your mouse over them.
Stories in Japanese
  1. Free stories can be found at this website. The stories are short and simple, and there are plenty of them to choose from.
  2. White Rabbit Press offers a series of booklets graded according to the JLPT vocabulary. The kanji characters have furigana, and there's a bundled CD with a native speaker reading each story. Each set includes 5 booklets.

Thursday, September 24, 2009

Learning Japanese - Memorization

I've been studying the Japanese language for nearly a year now. I didn't learn as much Japanese as I'd liked to have, but I did learn a great deal about learning.

In a previous article, I listed my early attempt at learning how to memorize. I've learned a lot since then, and I'd like to share it with you. It boils down to three main tips:
  1. Use associative memory
  2. Learn in context
  3. Use SRS software
Associative Memory


The Japanese writing system consists of several "alphabets" used simultaneously. The most famous is the kanji writing system, which is made up of thousands of characters. In order to read a newspaper, you need to be familiar with about 2000 different characters. That's quite a lot of letters to remember.

My initial approach was to learn by repetition. I'd write each character dozens of times and then quiz myself. This method not only takes a lot of work, but it's very easy to forget characters after some time without review. I can say that after about 200 characters, it was getting too time intensive to continue.

I recently started using Remembering the Kanji by James Heisig. The book promotes using our imagination to better memorize.

Each character has a keyword attached to it, and the complex characters are made up of combinations of simpler characters. Therefore, in order to remember the complex "letters", you make up some story that connects the keywords of the simpler letters and the keyword of the complex letter.

The keyword of the simpler characters are remembered using the keywords of even simpler characters. This all goes down until the axioms, sorry, the primitive elements.

It is now very easy to remember how to write a new character. For example, to remember how to write "bull's eye" I just image myself throwing a white ladle at a target and hitting the bull's eye. To commit this to memory, I spend several minutes picturing myself standing all tense, with a large audience watching, the fate of the free world resting on my ability to hit the bull's eye with a shining, white ladle. Now, I know that to the letter with the keyword "bull's eye" is made up of the letters for white and lade.

The serious beginning in the story is in direct contrast to the silly image of throwing a ladle, which only helps burn the story into memory. I try to spend a couple of minutes really imagining the story. I also attempt to make the story as interesting and bizarre as possible, preferably involving as many senses as possible.

So far this technique has been working miracles for me. I intend to finish learning all of the basic 2000 characters in half a year, and I really believe its possible!


Free Resources:
  1. The first few lessons of Remembering the Kanji are available for free.
  2. There is a website where you can read the top rated stories people invented.
Further Reading:
  1. Learning in context - to be written
  2. Using SRS software

Monday, July 20, 2009

Configurability

A while back I stumbled upon the following vim command, which opens a file explorer view. Now, this is truly scary stuff. I was as shocked and disappointed as you probably are. Assuming you're me, that is, and you're most likely not me. You just might be that other person who reads this blog, in which case you probably have no idea what I'm whining about. Alas, to explain, we have to go a little bit back in history.

The best way to travel back in time is the "Once Upon A Time - Man" way. We'll use Peter, which Wikipedia tells us is the very embodiment of the Good Man, as our guide.

In ancient times Peter lived with his entire extended family in the same house. Maestro was the leader of this establishment, and everyone, from Jumbo to Pierrette, went to sleep at night under the same roof. It was one big happy family.

As time went on, and social orders changed, the atomic unit shrank to include only the immediate family. Peter had to leave his parent's house and make his own way in life. After finding a lovely life (people who are Good have beautiful wives), he would get his own place and raise his children there. Until they got kicked out because they were lousy bums who had to get a job.

Nowadays, Peter and his present day wife would probably get divorced, so the family unit is now as small as it gets, unless the universe gets very creative.

Okay, we finished our time travel, and you probably have no idea why a little file explorer freaked me out. I guess we'll just have to make the same journey again until you get it.

A couple of thousand years ago, after the petty city states finished their squabbling, it was pretty nifty to be a king. As a king you were a member of a very small club. There just weren't many kings back then. Every once in a while someone would go around conquering half the world, limiting the number of nations and thus the number of kings.

This continued until just after the world wars and the rise of nationalism, which resulted in everyone wanting their own state. Wolfram Alpha says there are 203 sovereign states right now. Unfortunately, Wolfram Alpha can't answer the simple query: "Number of countries as function of time", but neither can google. You can imagine that over the past centuries this number is an increasing function.

Now, not only does the number of states increase, their power decreases as well. When once a totalitarian state was a pretty simple matter, the internet has made it much more difficult. Not only because Iranians protesters can twit, but also because Hezbollah backed Palestinian terrorists can coordinate attacks and learn how to make bombs. The state is no longer omnipotent.

One more diversion before I get to the point, which should be quite anticlimactic. When Henry Ford made the Model T, all the cars were black. Now they come in a wide range of colors. In addition, everyone wants his very own custom T shirt. I'll leave Apple out of it for the moment, because I'm going to get back to them soon. Everyone besides Apple wants you to have a unique user experience, complete with a customizable theme.

The Point. When I saw that Vim has a file explorer, I was terrified that it would go the way of the emacs. I hate it when a software not only allows, but actually expects you to customize everything in it. This is why I was afraid that Vim was starting down the road that would make it a good OS (albeit lacking a good editor). Emacs tries to do everything, and it tries to let you do everything. That just sucks, no matter what estimmed blogger Steve Yegge says.

For example, I mapped my Win key to the escape key, so that I can quickly switch between normal and insert mode. The annoying thing is that this only works on my machines. When I use a different machine, things suck. This is why I hate Configurability. I prefer uniformity. And I much prefer stuff That Just Works. This is the Apple part I promised two paragraphs back.

I could go on for much longer on this issue, and I probably should. Alas, I've written too much stuff already, without even explaining how everything relates, and the wife deserved some attention. More later.

p.s.
The file explorer in Vim is ok. It's just a file open dialog box. So Vim is still good.

Thursday, July 9, 2009

One Year Summary

A bit over a year ago I started this blog. I didn't know much about blogs and twitter and such at the time. After a year of blogging, I'm afraid I still don't. I think my main pain is that I still write these posts in the damn web UI. I'd much rather write in vim, but I haven't found the time yet to figure out the best way to do it.

During the past year I made many public promises to myself, on this blog and the others. I haven't really lived up to all of them. I have a consistent problem of trying to do too many things. Working full time and studying Japanese have taken up nearly all of my time.

While I didn't get down to writing a game in Python like I wanted to, I did manage to learn the language. Luckily, it was a perfect fit for many of the tasks I had to do at work, and it was a joy to learn it. I have my gripes with the language, but since I work in C++ most of the time, its a very welcome change. Needless to say, Python code is much more readable. C++0x has the auto keyword which will help, but a static language will never be able to match a dynamic one in this criteria.

I intend to continue studying Japanese, though maybe not in a university framework. I have to figure out if I want to start studying for a Master's in Physics or not. None of the courses I can take will be easy in conjunction with a full time job. I may end up preferring not to take any official obligation and spend time studying the things I like by myself.

We'll see how the next year will come along...

Tuesday, June 9, 2009

New Computer

I'm writing this on a new computer I got yesterday. It's called Fit PC2, and it's really really small, and pretty green as well.

Not as cool as a new mac, but nice none-the-less. It's ridiculous how small these things have gotten.

Sunday, June 7, 2009

Better World Books

For the last couple of months I've been ordering my books from BetterWorldBooks.com. It's an online book store with a soul. All the books I order are shiped in carbon free packages (if you exclude the air plane that delivers the package). In addition, the company diverts funds to help fund literacy around the world.

The nice part is that it isn't expensive either. Their prices usually compete with Amazon, especially considering that shipping is $3.97 around the world (free for the US). That alone would probably make it worthwhile for people outside of the states (yes, there are a couple of us out here).

They're also really nice. I occasionally get free treats, such a chocolate bars or tea. It's just fun. Alas, they give me nothing for writing this post :-).

Thursday, May 7, 2009

Book updates

I've updates my current reading list page, and even added short reviews for books I finished reading. Right now this includes everything I read, including novels and history books. Most are computer related in some way, though.

The reading list is accessible from the menu to right, at the top.

First Donation

Today I donated money to an open-source project for the first time. And it felt good.

I've decided that paying for free open-source projects you use and find helpful is a good way to go. Money was more of a problem during my university days, and I think it's a nice model to use good software when you need it, and pay for it when you can.

If you're wondering, the project is Review Board, a code-review web 2.0 application. I've been playing with it at work for a couple of days, and it's really nice. A few glitches during the installation, but it's at the RC stage, which means that it's stable, but glitches are still expected. It also means it's a nice time to give the developers a kindly push in the right direction.

Wednesday, April 29, 2009

Maintenance Mode

After a vacation, you really need some time off.

So I'm not going to talk about computer programming today.

In computer programming, we have two modes of operation - maintenance of what we have so far and creating new features. Maintenance is about fixing bugs in the code, updating the documentation and every other task that we programmer love to hate. Creating new stuff is cool. That's why we started programming - to create stuff. We like to create stuff, we hate to support it. Kind of like parenthood, I would guess.

Too bad for us. We can't avoid maintenance mode (unless you happen to be a consultant or something). You really shouldn't add new features until you get rid of the major existing bugs. And the same thing is true in life - the caveman, sorry, cave-person, didn't have time to invent the wheel until he had food in his belly. Hungry men don't create things - they look for food.

Just as in programming you need to constantly fix bugs to maintain the quality of an evolving product, so too must you constantly maintain your body and mind before you start to improve yourself. There are the usual things:
  1. Brush your teeth at least twice a day
  2. Eat & Drink
  3. Sleep
We all do the above to maintain our body at the most basic level. After that, we have to maintain our house:
  1. Wash the dishes
  2. Do the laundry
  3. Clean the house
  4. ...
At a higher level still, there are more maintenance jobs:
  1. Exercise at least three times a week
  2. Spend time with our loved ones
  3. Do something unrelated to anything - see movies, read novels, go hiking in nature, ...
These maintenance jobs are the most important so far, but they can't be done very well unless we finished our other tasks. In particular, everyone acknowledges that exercising regularly is very important, but too many people don't actually do it. And spending time with our significant other is so important that I'm actually willing to use the stupid term "significant other".

I think I covered most of the maintenance tasks so far. Now, before I finish, I'd like to point out that I'm not a native English speaker, and I remember hearing that it's bad to use the same word over and over again. While I didn't bother looking for a substitute for "maintenance", mainly because I didn't get the spelling correct on my first try, I did intentionally avoid a synonym for "job" - the word "chore". This isn't because my wife would be offended if I said spending time with her is a chore, but because I really don't think of these tasks as chores. They are a minimal set of things we need to do to keep what we have so far. The higher up we go, the more fun these tasks are.

Finally, the last form of maintenance is:
  1. Create something new. Challenge yourself each day.
This fights stagnation. We have to move forward. And it isn't really about maintenance, but it's good to force yourself.

Note also that I didn't put work anywhere up there. If your job goes up somewhere on your maintenance list, find another job.

Now, where did I leave those extra 10 hours a day...

Tuesday, March 24, 2009

Vacation

I'll be on vacation with little or no internet access until April the 18'th.

No internet and no cellphone is really the only way to have a real vacation these days...

Monday, March 16, 2009

Running a program on a server after logging off

At work we have a pretty powerful server on which we run computationally intensive programs. Many times, I want the programs I execute to continue running after I log off. This is how to do it:
screen -S blah
run_my_command
Ctrl+a d

I can now log off and go home. The program will continue executing safely.

The following day, I log back to the server and type:
screen -r blah

I'm now back where I was.

Sunday, March 15, 2009

Language Verbosity

I recently took a look at the upcoming C++0x specification, and I was annoyed that yet more keywords were introduced into the language. C++ is already a very complicated language, and it takes months and years to truly master. I just didn't think that introducing more keyword would advance the language. In some sense, the size of a language's vocabulary is a measure of the language's complexity.

My concern is that as the technology advances, it will become increasingly difficult to be proficient even in one single language. The learning curve just becomes steeper and steeper with time, which is fine if you're there when they make the curve steeper, but not if you're at point zero. And I know that specialization happens for all sciences as they advance, but programming isn't a science and a programming language is a tool, not a science (from the user's point of view).

Natural languages (such as the one you talk to your mom with, I hope) are not like that, however. Their vocabulary size increases with time, regardless of the language. Unless your language of choice is Newspeak, of course. Another curious fact about human languages is that they allow one to learn the language, using the language. This might not be too clear, so I'll illustrate:

John Robert Jr IV is 6 years old. His parents speak English (not American, mind you). Little Robert can't understand everything his intellectual parents say, but, by the time he starts his studies in a snooty British college, he'll have already mastered the language. How did this happen? Little Robert doesn't know any other languages but English!

The answer is that one can use English to learn English. This happens because you can understand snippets of code, er, words, and understand the rest from the context and the non-verbal information (intonation, body language, ...). In addition, there are always several ways of saying anything, at different levels of complexity. There are synonyms for nearly all words, some down-to-earth, others only for people with a PhD. in Literature. This is how Robbert will slowly learn new words which better articulate what he wants to say.

Sorry, but we really need to get back to programming languages.

Programming languages are like that as well. C++0x may introduce many new constructs, but you don't really need to use them. When I started programming in C++, I simply changed the file extension to .cpp and continued programming in C. I eventually started using the object-oriented facilities, and after that started learning what the STL really has to offer. So I think there's room for hope. Having different ways of saying the same thing, some concise, some verbose, is actually a good thing.

This is one of the clashing points of two of my favorite languages - Perl and Python. The Perl philosophy is that there should be many ways to perform the same task. Python believes there should be one (see The Zen of Python). And the thing is that Larry Wall studied linguistics. The language he devised may be a write only language, but the basic idea of having many different ways of expressing yourself doesn't just give you the opportunity to make yourself clearer, it also makes learning the language easier. You can learn new tricks as you go along, but it doesn't block you from getting what you want done.

Of course, the above isn't really true for Perl, unless you can remember what $x is for any x, but the potential is there.

Sunday, March 1, 2009

And Then There Were Two

I have decided to split this blog in two. All the programming related posts will remain here. The Origami posts will move to their new home at: http://origamitips.giladnaor.com

I hope this will allow me to better concentrate on each subject. Good week!

Thursday, February 26, 2009

Productivity Tip #2

I recently realized that I'm actually two very different people. I'm one person at night, and another when the alarm clock goes off. My night-self, let us call him Mr. Gogo, is tired, but his brain is functioning pretty well. My night self makes all kinds of plans for the next day. And then, my morning-self, Mr. Gozen, screws everything up. So, my second productivity tip is:

Tip #2 - Kill Mr. Gozen

I don't mean to promote violence or anything, it's just that Gozen is an asshole. He has only one thing on his mind - going back to sleep. That just plain sucks. I want to do things, and he ruins everything. So I've decided to terminate him.

For the past week, my alarm clock has been going off at the ungodly hour of 5:58 am. I get up and start looking for the damn clock, usually thinking of nothing else but going back to sleep. By the time I find it, I'm pretty much awake, and Gozen is dying. You see, much like the Wicked Witch of the West (WWW) melts in water, my morning self melts in wakefulness. And I must say that life is much better without him.

Every morning, I do the dishes while I finish waking up, ignoring the last feeble screams of Gozen, then stretch and exercise, drink something warm, and head off to the computer to work for an hour on one of my hobby projects (I promise to reveal what they are soon). Then I wake the wife with a kiss, and on I go to work. It's great.

Now, most of you are probably worried that getting up in the morning will be bad because you'll lose sleep. Well, nothing can be further from the truth. While I do sleep less, I don't lose any sleep. If the left hand side of the equation is waking up early, than the right hand side must be to go to sleep early. Well, no, No, NO! That's were the beauty of this whole habit is - my evening self has some ability for cognitive action. And when he gets tired, he says - off to sleep, because being tired after a long day means that that's what you need to do. On the other hand, being tired after 6-8 hours of sleep means you are not thinking properly, Gozen has taken control.

Picture of a roadThe beauty of the system is that everything balances up. If you don't get enough sleep, you'll go to sleep earlier. But you will never oversleep anymore - you'll get exactly the amount of sleep that your body needs.

I find I'm more productive in the morning, even though I work pretty well at 2-3 am as well. It's just that I have plenty of peace and quite to do what I want.

Wednesday, February 25, 2009

Why Not to Write Shell Scripts

Don't write shell script. Not in bash, and not in any other shell. There are two very simple reasons for this:
  1. They are not portable.
  2. They are hard to read.
Nowadays, we have a much better option - write those same shell-scripts in a cross-platform scripting language, such as Python or Perl. These languages are much easier to read than some devilish combination of awk, sed and a cryptic syntax. Well, Python is. But Perl can be readable too, contrary to common wisdom, if a bit of care is taken when writing the script. Sure, most shell scripts use some really neat tricks & hacks, but let me quote Brian Kernighan:

"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
Brian Kernighan

(If you follow the link, you'd see Mr. Kernighan has a hand or two in the scripting pot himself. Oh well...)

In my experience, even the most temporary script ends up expanding beyond its original intention, and thus needs to be modified and maintained. Shell scripts are just not the right tools anymore.


Further Reading
  1. Python, Subprocess and Multiple Arguments
  2. Programming with VI

Tuesday, February 24, 2009

Productivity Tip #1

I lead a pretty busy life. Besides working full-time, I'm taking a very intensive university course in Japanese, I try to blog weekly, exercise, read lots of books, work on hobby programming projects, do the dishes, spend quality time with my wife each and every day, go hiking every month and occasionally have some semblance of a social life. It's not much, and I won't give up any of these things, at least until we get a dishwasher. I do, however, need to better utilize my time so that I can get more things done.

This post will hopefully kick off a series on what I learn as I try and be more productive, or, more precisely, more efficient. I don't intend for it to be anything like those "self help" blogs, I actually kind of hate those. I want these posts to be about concrete, down-to-earth tips that I find useful. So, without further ado:

Tip #1 - Listen to Podcasts

Most people know this already. For a programmer, I'm pretty caveman like in my knowledge of the latest technological hype. I'm usually several years late when in comes to the latest thing. But although I've known about podcasts for months, I could never find the time to listen to them.

This where this tip gets productivity related. Podcasts are great for doing something useful in those "dead" moments we have each day. Most people listen to podcasts on their way to work. For me, however, it takes less than 5 minutes to drive to work each morning, so it's not really an option. The solution is obvious, although it took me ages to find it out - I listen to podcasts in the gym.

I think exercising regularly is very important, but that will have to wait for its own blog post. Listening to podcasts while exercises has been really great for me, and I can finally catch up on all those StackOverflow podcasts.

Saturday, February 21, 2009

Programming Editors

I've used a wide range of programming editors throughout my life. From the glorious copy con and QBasic of the DOS days, through the Turbo C of my teenage years, including the emacs of my college days and the Source Insight and Eclipse of my professional career, one thing is clear:

I was a terrible IDE user

This means that I rarely used, or even knew of, more than 5% of the features of each IDE - excluding (maybe) the glorious copy con editor, of course. I can still remember the shock at watching someone else debug code by using a debugger (!!!). Turns out there was one right inside Turbo C all along.

And then came along The Pragmatic Programmer, which is a phenomenal book. One of the things they recommend is to choose a text editor and learn how to use it. Learn how to use it real well. Which is why, I think, having a learning curve that's a bit steep is a good thing.

In college, after the first year the emacs police got off of our backs, and we could use eclipse. Many people did just that. I didn't - I had been forced to endure emacs for too long, and had to pay too many hours to learn the likes of "Ctrl+X 2", that I just didn't want to learn how to do it all over again (I still don't know how to do it in eclipse). This was, of course, a stupid idea. But to rationalize why everyone had red squiggly lines below their errors as they typed and I didn't, I had to learn emacs just bit more in depth. Or at least spend hours talking to the emacs psychiatrist, which eclipse doesn't have. Take that eclipse!

The problem was that I never really liked emacs. It can do horrible thing to your keyboard's CTRL key. And I never liked it's MS-Word philosophy of putting together one huge application (or OS) instead of many small programs which can be bundled together as you see fit. And getting a decent copy outside of unix-land is always an annoyance, even on my Mac, which is a citizen of unix-land, kind-of. I still hate programs that are several mega-bytes overweight. That's why I'm switching to vi.

So far, I really, really like vi. The whole normal mode is great for touch-typing - no more reaching out for the arrow keys (or the CTRL key). It's a nice programming editor, though I still have a lot to learn. My main IDE is still Eclipse, and although its C++ support is sketchy, it will probably take a while before I'm proficient enough in vi to give eclipse up completely.

Above all, I still miss Source Insight, now that I'm on a linux machine. It's probably the best editor for a large code base. It's one of the most ugly editors, but when most of the code is already written, it can't be beat. But now I mostly write new code, so it's not too bad. I'm good with vi.

But I still don't know how you say Ctrl+X 2 in vi...

edit: it's ":split filename"

Thursday, February 19, 2009

Source Code Highlighting - In Blogger!

There are a great many posts on how to post syntax highlighted code in blogger. I can't really understand why google doesn't have a ready made solution in place.

I think I've found a solution I'm comfortable with:
  1. Convert the code to html (":TOhtml" in vi)
  2. In the Compose tab, type: <pre> </pre>
  3. Paste you code between the tags
  4. Publish
That's it - no installation, not too much hassle.

For extra credit, you can modify the css for <pre> sections.

If you want line numbers:
  1. ":set nu" - show line numbers in vi
  2. ":TOhtml" - convert to html
  3. ":set nonu" - remove line numbers for easy copy & paste (or, use Ctrl-V)
1  #include <stdio.h>
2
3 int main( int argc, char *argv[] ) {
4 printf("Hello World!\n");
5 return 0;
6 }
All that's left is to find a good color scheme for vi.

edit: Ars Pythonica has added that in your .vimrc file, you can add: "let html_use_css = 1" to include css in the output.

Thanks to:
  1. Eli Bendersky - for the pre trick
  2. FluidBlog - For the vi TOHtml trick

Project Euler

I've just stumbled upon Project Euler. It's a nice little site with mathematical puzzles to be solved using a computer. For example, this first problem is to find the sum of all the natural numbers who are multiples of either 3 or 5, and are below 1000.

There are hundreds of such puzzles, most of them more challenging, so I'll allow myself to post the answer to this first problem:
#!/usr/local/bin/python
sum = 0
for number in range(1, 1000):
if number % 3 == 0 or number % 5 == 0:
sum += number
print sum

And what is great is that once you solve a problem, you get access to a pdf file with an overview of the problem and efficient solutions. The above solution I wrote, which is quite naive, can be computed in O(1), had I first used a pencil and paper as my IDE, and not Vi. Can you see how?

Tuesday, February 17, 2009

Programming with Vi

This page will serve as a place to store my notes on using vi as a programming editor. And when I mean vi, I really mean vim, but these days I think it's pretty safe to assume vi=vim. I'm placing these notes on the web in the hopes that it may help other programmers new to vi, as I currently am. I will update this page with information I find useful.

The Two Modes
The most important concept to understand about vi is that it has two separate modes - command mode and insertion mode. This originally struck me as rather cumbersome, and I found my self working nearly completely in insertion mode. You can get along like this. You can also write C++ code in Microsoft Word. There's a really good reason for this separation - because command mode is separate, the commands are much simpler. For example, navigating around the document is really easy:

Navigation
j - Move one row up
k - Move one row down
h - Move one column left
l - Move one column right
w - Move to the start of the next word
b - Move to the start of the previous word
e - Move to the end of the next word
0 - Move to the beginning of the line
$ - Move to the end of the line
1G - Move to the beginning of the file
G - Move to the end of the file
% - Move cursor to matching bracket

The jkhl commands may seem quite stupid. There are arrow keys after all. But if you know how to touch type, you'll find that this really improves your productivity. It takes a bit getting used to, but it really pays off, in my opinion.

Like many commands, you can add a number before a navigation, so that 4j moves you four rows down.

Undo & Redo

u - undo last change
Ctrl+R - redo

Copy & Paste

v - enter visual selection mode. Moving the cursor will select text for later actions, such as:
y - yanks the selected text. aka, copy
yy - yanks the current line
p - paste text

Multiple Windows
:split filename - split a window and open filename
:vsplit filename - split a window and open filename

Running Shell Commands
:!make - executes a shell command, such as make
:r !ls - executes a shell command and pastes the output into the file

Search & Replace
/expression - Searches for expression
:%s/expression/newtext/g - Searches for expression and replaces it with newtext

Configuring The Environment
:set nu - shows the line numbers
:set nonu - don't show the line numbers
:colorscheme - changes the color scheme
:set sw=4 - sets the tab size to be 4 spaces

All these commands can be placed in ~/.vimrc

References
  1. Yolinux is always a good source
  2. Run vimtutor for a good hands-on tutorial

Saturday, February 14, 2009

Current Reading List

This page contains books I'm currently reading and the books I recommend, though I will probably not update it too diligently (last update-30'th of December, 2011). I have also stopped buying the dead-tree variety of books, and read on my iPad. Finally, note that reading is not enough, you also have to do things.

Current Reading List
Release It / Michael T. Nygard


Books I Recommend

I still can't relay say that I finished this book. I still need to find a quite afternoon to finish it. But, it is an excellent book, there is much Zen in it.

I think it's a good book. I haven't read many self-help book, and I generally dislike the entire genre. The 7 habits, however, is a serious book that doesn't promise any shortcuts - it doesn't promise easy solutions to anything, and I like that. I disagree with a couple of things, but it's a good read.

This is book is recommended by many people, and now by myself as well. It's a great book about managing people who get payed to think. It's a great read even for regular programmers like myself. It's a small book with plenty of fun stories. Highly recommended.

Don't Make Me Think: A Common Sense Approach to Web Usability / Steve Krug
This is a book about designing web sites to be usable. It's a very quick read - large fonts, plenty of pictures. Nonetheless, I think it's worth the money. A day after finishing the book I happened to show my mom the blog of our honeymoon, and I immediately noticed the many flaws I made when designing the blog.

Kafka on the Shore / Haruki Murakami
This is the second book by Murakami-san I've read. Like the first one (The Wind-Up Bird Chronicle), it is quite surrealistic. I can say that I really liked it, even though I disagreed with many of the overly-simplistic pacifistic messages Murakami tried to convey. Next I need to read Norwegian Wood (done).

Tuesday, February 3, 2009

Learning Japanese

I started studying Japanese a couple of months ago. I decided to register for an intensive course at the local university. There were other places to go to, such as the Japanese embassy, private tutors, specialized companies or just learn it by myself. I'm not too sure why I chose to study in a university.

Ages ago (2003), when I was contemplating what to study, I narrowed it down to three options - Physics, Computer-Science and Japanese (as part of a degree in Far-East Studies). And then I decided the choice was too hard, so I'd take two of the three. The final choice was to study Physics and CS, with the hope of squeezing a course in Japanese somewhere in between. I can't say I'm sorry, even though I never did manage to take that Japanese course.

After finishing my degree, it was only logical that I finally study Japanese. And I guess I decided to do that in the university because that was my frame of mind back then. I did consider the other options, but either they were too expensive, or they were not rigorous enough for my taste. In any case, now that the semester is nearly over, I can say I'm quite please with my choice.

I must say that the learning Japanese is quite different from learning Physics. The entire atmosphere is different. For example, classes are much more interactive, but that may be due to the fact that few people want to study Japanese, but a great many want to study Computer Science (for $ome $trange rea$on). In any case, the biggest difference is that there's a lot of memorizing required when learning a new language. Except some courses in mathematics, I really didn't have to do much memorizing during my studies in the exact sciences.

I've always had a love-hate relationship with memorization. My teachers in high-school kept on saying that it's important to understand concepts, and not to memorize them, and I've spent most of my life agreeing with them. I no longer do. I think that memorization is an important step in the path to understanding. It is also important in itself. For example, any physicist should remember what Planck's constant equals, as well as the value of Boltzmann's constant. Remembering important equations is also very important, because it allows you to solve various problems without breaking your chain of thought by opening a book (or Wikipedia).

My father knows pretty much the entire Hebrew bible by heart. He's not religious, far from it. It's just that his teachers didn't have any problems with forcing people to memorize passages. And I think his teachers had a good point. My teachers also had a good point, but that's how life is.

So, back to Japanese. I've spent the last couple of months having to memorize stuff for the first time in my life (at least to such an extent). I wasn't prepared for it. I tried various techniques. At first, I wrote each new word in a tiny notebook which I intended to take with me everywhere. That didn't workout, mostly because I didn't take it with me everywhere I went (it had a spiral binding, quite uncomfortable in the pocket). I still think it's a good enough idea in most cases, and it certainly works out quite well for my brother, who gave me the idea. My current invention goes something like this:
  1. Buy a bunch of cards
  2. Write the English/Hebrew term on one side.
  3. Write the Japanese translation on the other side.
  4. Add some useful information (for verbs, for example, if it's いちだん or ごだん)
Yes, it may very well be the breakthrough in language study that millions of people have been waiting for. Oh, wait. Someone thought of it before me. Damn.

Both of the methods I tried are okay, but that's pretty much all I can say about them. Of course, I don't rely solely on them. I still think that the best way to learn is by doing, and, as a corollary, the best way to learn to read is by writing. So I write a lot, which is important in Japanese because their alphabet is a mess. A beautiful mess, but a mess none-the-less. The bottom line, however, is that all these methods fall under the category of "memorize by repetition". It's a good, proven method. It just takes up a lot of time, time which I simply don't have (when am I supposed to write that game in Python?).

There has to be a better way. I've started looking into the various tricks of memorization. In high-school, we once got a visit from some sales-person for a private course in improving learning skills, or something like it. She taught us how to remember all the different offices in the government in a couple of minutes. She did this by telling us a story, and I still remember it more than 10 years later. It's based on a simple trick, that is the basis of most memorization techniques.

The basic concept is that we have an associative memory, and we remember the extraordinary. So, to remember a list of things, you just make up some bizarre story that connects the various items. The more extraordinary, the better, especially if it involves as many senses as possible. There are many methods that take this basic idea much further, but I don't know enough about them right now. It's one of the things I've decided I want to learn (yes, yet another one).

And yet, I'm not sure these techniques are good enough for Japanese. There are thousands of "letters", each with several different pronunciations, depending on the context. Most letters have a dozen or more strokes to remember. I just don't know of a better way to remember them than by endless repetition.

So, although it's been a fun post, it's one of the longer ones I've written, and I have to go memorize some kanjis (料利 is especially annoying).

I also have to improve my English writing skills sometime.

Further Reading
  1. Learning Japanese - Memorization: includes many new lessons I've learned since writing this article.

Friday, January 23, 2009

Some People Are Idiots

I've been sick for the past week and a half. The whole stay in bed, drink lots of tea, use entire rain-forest's worth of tissue paper deal. It sucks. I've even lost the ability to speak. At least I can still write.

I've tried every remedy conceived by those old hags that live alone in the middle of some dark forest. You know the ones. It turns out that lemon really is a good anti-bacterial substance, and so is garlic. That's science. I happen to have a wife in the chemistry business, you see. The undisputed winner in the "your grandmother says its good" category of medicine is without a doubt a bit of whiskey. Well maybe most grandmothers don't recommend it, but there's nothing like 40% alcohol to burn all the nasty germs. Sure, there might be some collateral damage, but at least the germs will die. As the song goes, "Just a tea-spoon of whiskey helps the cough go away, helps the cough go away, in a most delightful way!".

So I've been thinking about all the idiots who say pearls of wisdom like:

"All You Need is LOVE"

or perhaps:

"Live Every Day As If It Were Your LAST"

You see, people who live everyday as if it were their last would never bother going to school, have children or advance mankind. It's like one of those governments in those democracies which have elections every two years. They never accomplish anything, because they are only thinking about today. Life is great because we don't live each day as it were our last.

Oh, and finally, "The Wind-Up Bird Chronicle" is a great book, right up there with such classics as "Ubik" or "The Three Stigmata of Palmer Eldritch".

Thursday, January 8, 2009

TBB and Non-Integer Ranges

I've been using the Intel Threading Building Blocks (TBB) library for a while now, and I finally figured out how to overcome one of the annoyances I had with it.

All of the examples I've been able to find show how to iterate over a range of integers. I couldn't find how to directly iterate over a range of doubles. I worked around this by iterating over integers and translating back to doubles in the inner operator() function. I finally figured out the answer, after reading the Reference Manual (as they say, rtfm).

The trick is that the blocked_range construct can accept, in addition to integers, random access iterators. So, with a range:


tbb::blocked_range< std::vector<double>::iterator > ( bunch_of_doubles.start(), bunch_of_doubles.end() );

I can now parallelize a group of doubles.

Of course, this assumes that you have a container ready. Luckily, I happened to have one ready. In other cases, it's probably best to continue and shuffle between ints and doubles.