Monday, November 24, 2008

Bash: Passing Arguments with Quotes

Today I needed to write a bash script that eventually calls another program, passing all the arguments to it. It seemed like a simple enough task: just use $@. However, the problem was that the program accepts arguments surrounded with quotes, and the $@ stripped the quotes away. I was really surprised that google didn't immediately find an answer.

Finally I reached this forum:
http://www.linuxforums.org/forum/linux-programming-scripting/62564-bash-script-problem-preserving-quotes-arguments.html

And to save you the trouble, here's a script that preserves the quotes:
#!/bin/bash

# blah blah blah

./foo "$@"


For more on posts, see here.

Thursday, November 20, 2008

Brandon Sanderson, The Hero of Ages and Marketing

The story of how I ended up reading The Hero of Ages is an interesting example of successful marketing in the 22'nd century.

I first heard about Brandon Sanderson after hearing that Robert Jordan passed away. I've read most of the Wheel of Time books several years ago, and occasionally checked up on the status of the final book in the series. That's how I learned that Robert Jordan died before being able to complete the last book, and that an author called Brandon Sanderson had been selected to write the book in his place.

A quick google search led me to the surprisingly good website of Brandon Sanderson. There I found a free ebook of the work-in-progress version of the upcoming Warbreaker book. I read this book and liked it, which made me want to read more books by Sanderson. On his website, he also posted the first couple of chapters of The Final Empire, which I also read and liked. I searched for the book on various ebook sites (since shipping to Israel from amazon is expensive and takes a lot of time), and couldn't find it, so I left it alone.

Fast forward several months later. A random check in fictionwise showed me that the Final Empire was finally released in ebook format, so I immediately bought it and downloaded it to my palm. I read the book in a couple of frantic-your-priorities-are-up-side-down days. As soon as I finished reading the book, I ordered the second volume, The Well of Ascension. I just couldn't wait for it to appear in ebook format. Needless to say, I finished reading this book in no time as well.

I was now hooked, and couldn't wait for the third book to come out. I felt like I was 8 again and couldn't wait a whole week for the next episode of The Transformers. That's why I escalated things up, and ordered the hardcover (!) edition of the third and final book as soon as it came out.

The fact that I kept coming back for more, even to the point of ordering hardcover edition, (which is not something I generally do), just shows how good the books are. But the point is that none of this would have happened without plenty of free material on the author's website. At least on my part, this payed of.

Friday, October 31, 2008

Languages

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration."
Edsger W. Dijkstra (SIGPLAN Notices, Volume 17, Number 5)

If you think about it, and please do just that right now, then you'll probably notice that you use your native language to facilitate the thinking, at least to some extent. Taking this observation to the next step, how much does our native tongue influence our thought patterns, and how we perceive the world around us?

This is not a new question. There's even a theory in linguistics which studies this question, called the Sapir Wohrf hypothesis. In fact, George Orwell, in his classic book "1984", has a whole new dialect, "Newspeak", introduced by Big-Brother to aid in preventing "thoughtcrime". This imaginary language is unique in that it attempts to reduce its vocabulary, thus intending to limit thinking patterns.

A couple of years ago I heard a lecture (in hebrew) by Physicist turned Computer-Scientist, Prof. Naftali Tishbi, on some of the research he's conducted on languages and their affect on thought (and vice-versa). Since then, the subject has been sitting in some corner of my brain.

A couple of examples:
  1. When a procedure that I already know is coined, I can understand and use this procedure much more easily. For example, in Origami, the term "Double Rabbit Ear" allows my brain to much more easily choose an appropriate course of action, rather than just remembering some sequence of folds I've done before. In programming, that's exactly what Design Patterns do. I've used the Factory pattern before, but coining it made it more accessible cognitively, and it's now much more easy to notice when this pattern is appropriate.
  2. When attempting to solve a programming challenge, the choice of language affects the solution. This is taken to a very interesting extreme in LISP, where the first step to solving a problem is to create a new language tailored specifically for it.
There is no doubt that what programming languages a developer knows critically affect the quality of the solutions he'll be able to come up with. Until I learned Perl, I wrote simple text processing tools in C. The results were buggy programs which took too much time and thought to write.

But it is much deeper than this. A programming language affects how we even think about problems. Knowing several programming languages is important in developing our own minds and allows us to better think about problems. This is not new. Edsger W. Dijkstra already talked about this in his excellent 1972 lecture, "The Humble Programmer".

So, my resolution for the following year is to learn two new languages. While I think I know enough of Perl to get what I want done, I'm no expert in it, mainly because I don't like too many $ and too few letters. That's why I'm going to spend the next year studying Python. And I intend to really study it, inside-out.

I have a full-time job, a wife, some non-programming hobbies, an obligation to write a computer game, but I intend to follow through.

Funny comic from www.userfriendly.org, August 15, 1999

The other language I'll be learning is Japanese, by the way.

Saturday, October 11, 2008

Active Learning

"Reading, after a certain age, diverts the mind too much from its creative pursuits. Any man who reads too much and uses his own brain too little falls into lazy habits of thinking."
Albert Einstein

It's been several years since I realized I don't know anything. Occasionally, I make the mistake of thinking I know something pretty well. Reality always reminds me that I don't, usually using a sledgehammer. So I keep trying to learn. I read blogs and even (gasp) books. In fact, I even spent four years in what some people say is a pretty decent university. I even managed to surround myself with people who are way smarter than myself at work.

The thing is, you can only learn so much from other people. Consider Tom for example. He wants to learn a bit about thermodynamics and the heat conductivity of various substances, say, a human hand. It doesn't matter how much he reads Fermi's book, or even how much he hears lectures by distinguished professor Mom. In fact, said professor told Tom just last week that touching fire is dangerous, because fire is very hot. Now, Tom is a very intelligent boy for his age. His mother told him so, as a matter of fact. Yet, all of the theoretical knowledge he's learned won't stop him from trying to touch the flame with his hands. Only after such an experiment will Tom really understand that the heat conductivity of the human hand is, well, ehm. It hurts. That's what he'll finally learn.

My point being is that the only real way of learning something is by doing. That's why work is so much more tiring than a university lecture. In a lecture, you sit passively and listen. You can cover more material faster this way, but you don't really learn anything this way. Work has that annoying aspect wherein you actually have to work.

I'm not against reading books. I really love books. I am, after all, a nerd. Reading books is important. But it's not enough. You have to learn things the hard way.

So I'm going to write a game. I don't know too much about it yet, but that's what I'm going to write in the spare time that I don't have. A game is a good subject, because it covers nearly every imaginable aspect of Computer-Science and software development. I've already finished the dreaded Requirement gathering process. Here are the games requirements:

  1. It should be a game.
  2. I should finish it.
That's it. Now to start codi, em, I mean, designing. Yes, that's what I mean. Designing.

Sunday, September 21, 2008

If You Can't Test It, You Can't Program It

A well known axiom of business management is: "if you can't measure it, you can't manage it". I believe there is a software development corollary:

If You Can't Test It, You Can't Program It

At a first glance, this may seem stupid. You can program what ever you want to. But programming is about making the computer do what you want it to do. And computer have been scientifically proven to be bizarre, unpredictable creatures. They do what you tell them, and not what you think you told them. Unless you test, you can never know if you got your message to the damn computer.

And because source code is a dynamic, ever-changing being, you have to test and re-test and double re-test. That's why you need Unit Tests and Regression Tests. But that's for another post...