Articles from March 2010



Why Lua truly rocks

For the uninformed, I am of course talking about the programming language Lua.

Recently I have been working on my Lua-based framework, for creating games and applications for iPhone. Yesterday I submitted my first application which uses the framework to App store. The whole application is written in Lua. You can check the Lua source code for the application here.

I have been working on the framework for weeks, but I wrote the Lua code for the application in just one day. The application is not very complicated, but Lua shows its potential compared to Objective-C, when you need to get iPhone apps done quickly. The more I improve the framework, the easier it is for me to write those application in Lua, because writing the application logic and adding a user interface is quite simple compared to writing everything from scratch with Objective-C.

The reasons for choosing and using Lua for iPhone applications were pretty obvious for me. It is written in ANSI-C, which means that I can just take the Lua source code and compile it with rest of my framework. Lua’s licensing model, MIT license, allows me to do anything with Lua. Lua is also quite compact. The binary size will only grow by a couple of hundred kilobytes, which makes it really light. It is also quite fast compared to other scripting languages and the memory usage is not too bad. I haven’t done any heavy calculations in my applications yet, but if I ever need to that, I can move the heavy processing to the Objective-C side.

Where Lua really shines, and what is was actually designed for, is that it is probably the best language for embedding and extending other parts of the software written in C, C++ or Objective-C. Once you understand the Lua stack model, adding functionality to Lua is very easy. It is almost as easy as calling functions directly. Only thing that you need to do is to get the arguments from the stack, which is very easy.

Lua is actually a very powerful and flexible language, because it has metatables and support for closures. You can do a lot with these features. I am currently working on two-player Tetris clone, and I can forward function calls automatically to the other player with this piece of code:

function create_peer_opponent(page_name)
    local opponent = {}
    local mt = {__index =
                function(table, key)
                    function remote_call(...)
                        local str = page_name .. "." .. key .. "("
                        for k, v in pairs({...}) do
                            str = str .. tostring(v) .. ", "
                        end
                        str = str .. "nil)"
                        ui.rpc_call(head_to_head, str)
                    end
                    return remote_call
                end}
    setmetatable(opponent, mt)
    return opponent
end

After creating the peer opponent, the rest of the code does not need to know if the opponent is a local AI opponent or a remote human opponent. The function calls will end up in the correct place automatically.

Of course, everything I want to do from Lua needs to be wrapped in the framework. Fortunately Objective-C is actually quite dynamic language, and the reflection support is pretty good. I might be able to leverage the reflection mechanisms to automatically create and use Objective-C objects from Lua, but I don’t know yet if it actually is possible or does it make any sense.

Another nice thing about writing the game logic in a scripted language is that I can implement the framework on other platforms, e.g., on Android, and use the same Lua code for the game. The platforms are not limited to mobile platforms; basically anything that has support C supports Lua.

If anyone is interested in using the framework, just let me know, and I might actually open source it.

  • Share/Bookmark

My short visit to video game industry

Ever since I got my first computer, a Commodore 64, I decided that I want a job as a guy who writes computer games. Well, I got a job in video game industry when I was 28 years old. By that time, I had a Master’s degree in computer science and seven years of experience in traditional software industry.

It was like dream coming true. Finally I could live my dream and program cool games for the rest of my life. I was so excited about the opportunity that I did not even care about the open office. There were no doors. Not even cubicles. Programmers in one area, graphic designers in one, and game designers had their own area. You could easily hear the game designers talk from the programmers’ corner. Obviously the managers of that company had never heard of the book Peopleware, which should be mandatory reading for every single manager and programmer in every company that does software.

My title was Generalist Programmer, because I did not have any special game programming skills like graphics or AI. I thought I would write code for the game the company was developing, and pick up graphics and AI skills a long the way. What I did not know that a really big part of Generalist Programmer’s duties were related to version management. I would spend a week every month merging our changes to the latest version of the 3D engine. I probably should have asked what my tasks were going to be, but I was so excited about the job that I did not do that.

Actually my only interesting task during my time there was creating a plugin for 3D Max, but I could not really focus on the task, because of the environment; People were having loud design sessions 20 inches from my desk, which made concentrating a bit difficult. Of course, I could have used headphones and listen to music to block out the noise, but listening to music actually prevents people from coming up with creative solutions, because the brain is already occupied with the music. I read this in Peopleware, and I think it might actually be true for me.

The other, more experienced, game programmers were not doing that much coding either. They were mostly using the editor that came with 3D engine and putting pieces of the game together. No-one actually did heavy programming full-time in that company. I knew that if the other guys were not programming that much, I would never be able to just program all day long, which was the only thing I wanted to do.

The situation was not as bad as it might sound, but it was such a big shock for me, because I had created these illusions in my head that game programming would be the coolest job ever. I came crashing down from my dreams. It was not at all what I imagined it to be. The pay was also pretty bad compared to regular programming jobs. After three weeks I felt so bad that I had to leave. One evening I came back to work after everybody had left and left my key on my desk. My boss and his boss were out of town, so I just sent an email explaining the situation. My boss called me the next day and I explained the reasons why I left.

Looking back at situation, I probably should have talked about these things immediately with my boss, but I didn’t. I just wanted out. My dream was crushed. I just wanted to write software. I am really happy that I tried to pursuit my dream. It did not work out for me, but at least I gave it a shot, albeit a short one.

  • Share/Bookmark

Dieting is not rocket science

Seriously. It is not that difficult. You eat less and exercise more. Let me say it again: You eat less and exercise more. That is it. No need for fancy and expensive methods. You do not need a personal trainer to lose weight. You do not need to understand everything about nutrition to lose weight. It might help, but is not required.

There is huge amounts of money in dieting. A lot of Top 50 books at amazon.com are about losing weight. All the home exercise machine infomercials fool people paying a lot of money for stuff that is not actually needed.

You do not need to know how much calories are in the food you eat. If you are putting on weight, you know that you eat too many calories. If you are losing weight, you consume more that you eat. Common sense also helps a lot. If the food is covered with fat, it probably contains a lot of calories. Buy a scale and eat same type of food the same amount every day for a week. You should know after the week, if you are eating too much.

However, people might know what they need to do to lose weight, but they are incapable of doing that and they try to find easier ways of losing weight without the hard work.

It is all about mental toughness. If you want to lose weight and decide to lose weight, just do it. After Christmas I decided that I will lose weight, because of all the chocolate, Christmas food, hamburgers, etc. January 1st I weighed 85 kg, two months later I weighed 79 kg.

All I did to lose weight was stop eating chocolate, French fries and dinner at Mickey D. I still eat four times a day, including a lot protein on every meal so that my muscles do not completely vaporise. I also do 20 minutes on the treadmill when I am at the gym. That’s it. No fancy tricks, no books, no personal trainers, no pills, nothing special. I even eat pizza for lunch once a week.

However, first part of losing the weight is dieting and the second part is keeping your weight down. If you go back to your old habits of putting on weight, the benefits of the diet will disappear pretty soon. To make the weight loss permanent, you should change something in your life for good. For example, switch to diet Coke, stop eating candy, or anything that will make you eat less calories or consume more.

  • Share/Bookmark

Why code lines still should not exceed 79 characters

I hate long code lines. I hate them with passion.

You might think that in the modern era of programming everybody has a big ass monitor with loads of horizontal space for the editor. Unfortunately you are wrong. What is the point of having a big monitor if you have to make the editor cover the whole screen? I have two 24-inch monitors, but I have one of them rotated 90 degrees, which makes the resolution 1200×1920. 1200 pixels is enough for a pretty wide editor window, but I always have two editor windows open side by side, which makes the editor windows 600×1920 pixels and that is about 2x79x120 in visible characters.

I am also old school and use terminal windows, which, as you can guess, are about 80 characters wide. It helps a lot if the code, does not wrap inside the terminal window when using version control commands from the terminal.

OK, so far the reasons have been pretty much about the environment and a lot of people have environments that support long lines easily. However, something that is universally bad about long lines is the way they affect the readability of code. Long lines make the code harder to read. If you have to turn your head when reading code, digesting the code will be much harder.

Code is read more often than it is written, so writing elegant code should be the goal of every single programmer. It is much easier to get a complete picture of the code, when the lines do not run forever. There is also a reason why books are shaped the way they are. Who would like to read a book that has lines over 150 characters long?

People post questions to stackoverflow.com and sometimes include source code related to their questions, which is really useful. However, the code window is only 79 characters wide and there will be a horizontal scroll bar, when the code does not fit in the window. That is really annoying. Trying to figure out what is happening in the code when you cannot see full lines of the code is really hard. I am not blaming stackoverflow.com, but I am blaming the programmers, who post sample code that have long lines.

Sometimes code needs to be printed. Not that often, but there are good reasons to do that. If code looks like crap on screen, it will look even worse on paper especially with those long lines wrapped in the printed version.

Every time I see long lines, my respect for the author of the code drops a little. I do not think there are any excuses for writing long lines. Every programming language I have used supports splitting code to multiple lines. Some languages do it better than others, but the language is not the problem.

The problem might be the editors that most people use to write code. Usually when I see long lines, the code has been written in either Visual Studio, Eclipse or Xcode. People that use those IDEs have their environment full-screen, which makes the editor window quite wide. The template code that IDE will generate is usually terrible and people just copy ‘n paste that crap. Editors also indent lines differently when you split them, or do not have automatic indentation support for lines that where split. This might cause the writer of the code to avoid splitting, because it will require pressing the tab 6 times to get to the correct indentation point. Configure your editor to do this for you. (Or even better, use a real editor)

  • Share/Bookmark

What's with the DWORDs

If don’t know what a DWORD is, good. This is quite geeky so don’t read any further, unless you want to know what wrong with DWORDs.

If you have ever taken a look at windows.h, you know what I am talking about. For some reason Microsoft decided that they need to define every single type themselves, because the original types did not shout enough. VOID is clearly a lot better than void, but what the hell is the reasoning behind LPCSTR? It looks like vomit. I don’t want that my code looks like shouting vomit. What the hell is wrong with ‘const char *’?

The big problem is not that Microsoft re-invented types with this stupid system, but it is a big problem that some so-called professional programmers actually use those wonderful types in code that should be cross-platform. When they realize that their code needs to work on something else than their beloved Windows, they make nice typedefs for linux and others. Only problem here is that everybody has their own idea what DWORD should be defined. This is where the fun starts. There are situations where you might need to include to different headers files, but both of those header files define DWORD differently. Nice.

  • Share/Bookmark

Master of Speed Lite

Test your quickness and memory skills with this awesome game.

In speed mode you need to keep up with the flashing buttons as they flash faster and faster.

In memory mode you need to remember the sequence the buttons flashed. As the game goes on you need to remember longer and longer sequences.

This game is a modified and improved version of the classic game that was featured in Finnish TV show, Speden Spelit.

The Lite version has limited gameplay. Buy the full version to get unlimited gameplay.

Price: Free

View in App Store

  • Share/Bookmark

Master of Speed

Test your quickness and memory skills with this awesome game.

In speed mode you need to keep up with the flashing buttons as they flash faster and faster.

In memory mode you need to remember the sequence the buttons flashed. As the game goes on you need to remember longer and longer sequences.

This game is a modified and improved version of the classic game that was featured in Finnish TV show, Speden Spelit.

Price: 0.79€/$0.99

View in App Store

  • Share/Bookmark

Office Space Out

Don’t feel like working? Feel like spacing out? Want to surf the web or sleep without interruptions at the office?

Use “Office Space Out” to play keyboard typing sounds and fool you boss into thinking you are the hardest working person in the company.

Two different types of keyboard sounds to choose from.

Price: Free

View in App Store

  • Share/Bookmark