Swapping variables without introducing a third one

Swapping 2 variables without introducing a third one can be useful when the efficiency is critical that you want everything stay in registers. There are a few ways to do this.

Notice that this method still works even when a overflows during the first addition. Try it to see what I mean.

You can also do this using XOR operator like so.


Would also like to mention the right way to do it in Python for readability. No one cares about performance when they are writing in Python right?


Unity Tutorial Review (Part 2)

If you haven’t read part 1, here a quick link to it:

Last time we went through the “Interactive tutorial”, this time I will talk about the “Roll-a-ball” tutorial.

The “Roll-a-ball” tutorial is first basic tutorial after the “Interactive tutorial”. I followed the video tutorial in the Unity website and it provides step-by-step instructions which make it very easy even if you know nothing about Unity. I started from scratch and ended up with a Roll-a-ball game constructed by basic shapes. The game is simple, you control the rolling ball and collects the gems to gain points.

Creating basic 3D shapes is easy in Unity. It took me very little time to learn the geometry system in Unity and created the stage area with stretched cubes, the ball and the gems.

Things start to get a little bit more interesting. We need to enable the player to move the ball. Unity is designed to be modular and each object in the game has its own list of components. To enable the ball movement, we need to create a component in the ball which maps the arrow keys to apply forces to the ball in different directions. Such component is not provided in Unity, instead I need to create a script in C# which contains just a few lines.

The gems are also animated using the script so they are spinning slowly and become a bit more attractive. I also added the collider component for the gems. When the gems are collided by the ball, it will trigger a function in the script, which makes the gem disappear and adds one point to the score.

The “Roll-a-ball” tutorial is only about 1 hour long. After that, you can already start expanding the game by designing new levels if you want. The ball cannot fall from the ground in the tutorial since the playing area is fenced by stretched cube, but I can imagine they can be removed to make it a little more difficult and fun. There’s a game with a similar concept which you have to control the ball from point A to point B without falling off the cliff. I haven’t played it before and I don’t know the name. But I’ve watched it on Youtube and it looks kind of fun to me.

Again, Unity did a very good job to introduce itself to the community. It doesn’t matter how great your game engine is until you have great support for the beginners or potential users. Same applies to programming languages, APIs and basically every tool that has a steep learning curve.

Unity Tutorial Review (Part 1)

Finally, here it is. After spending about 5 hours on Unity to learn about its basics, I went through 3 tutorial projects – the interactive tutorials, the roll-a-ball tutorial and the survival shooter tutorial. I must say that Unity is totally better than what I expected. Unity is intrinsically easy to use due to its design, has a lot of resources for learning and creating your game. It is a fun experience to learn Unity in my spare time.

Fun and Practical Tutorials

The learning process of Unity is a lot more fun and easier when compared to other game engines. I have experience in several game engines such as “GameEngine”, “Microsoft XNA”, “Unreal Engine” and “RPG Maker”. None of them provides a learning experience as fun as Unity.

The “Interactive Tutorials” in Unity provides a scenario where you want to control the character in a game, move it from start point to the exit point, but you cannot make it just in the play mode since the game is broken. You have to edit the stage properly in order to finish the stage.

For example in one stage, there’s a deadly laser blocking the path. You want to press a button to drop the box, but the box wouldn’t drop because it lacked a component called “RigidBody”. I had to add the component and set it properly so that it can drop down and block the laser.

The whole experience is like solving a puzzle. It’s like inception where in order to complete the stage, you have to go one level deeper, to the Unity, fix something, then come back to the game to finish it.

The interactive tutorial helps you to get familiar with the editor and the component design of Unity. It teaches you pretty basic stuff and gets you prepared for the following tutorials.

Let’s talk about the 2 others tutorial next time.

It’s time to write again

Long time no see.

Really, it’s been a long time since I wrote on this blog last time. I’m still in the software engineering field and has exposed a lot more in performance and parallel computing, cloud and big data since I’ve been working in the bioinformatics field.

I have also invested a lot of time in Hearthstone, a digital card game published by Blizzard – from a casual player to a competitive player, then to the National Champion of China. I tried streaming, commentating in tournaments and now organizing tournaments and events with my local company XGamer. I will put the link here so you can go check it out. It’s in Chinese though. http://www.xgamer.pro/

Just a few months ago, I consulted for a sneaker app business in mainland China. The consultation was very fruitful and I found myself suitable in such position. I’d love to so more consult in the future and I think going back to my blog would be a good start.

So here I am. I will be writing something soon. I have just tried Unity the game engine so maybe I will write a little bit about that. That should be fun.

User Login Design

The Typical Software Design Story

Usually for the first version of most web applications, we have user and user login. The user login is so simple that it is verified by a username and a password. So we design the class like this.

This works pretty well for now. Project manager is happy. The developers are happy.

A month later, the team wants to simplify the registration process so that more users will sign up. your project manager asked you to support login by facebook and google accounts.

You think about it. You think about adding a type attribute to each user. But the normal users and facebook users have different information to store. This could be an obstacle so you move on and continue to think. It is very tempting to extend  User class to  NormalUserFacebookUser and  GoogleUser. They all have the same behaviours except the credential checking. So you go ahead and implement it. Project manager is happy. All developers are happy.

One more month later, we find that the application needs different type of users and you decided that using inheritance is the best design choice. However, we have already use inheritance for different login types. So what can we do?

Inheritance is not as good as you thought

Back in the college years, your professor taught you about Object-Oriented programming, giving you examples of inheritance to achieve polymorphism, code reuse etc. He didn’t tell you about the limitations. Don’t get me wrong. It might not be the fault of your professor since your OO class only lasts a semester and it wouldn’t have enough time to touch those parts.

Composition is the way

So how can we re-design the system here? Turns out it’s not that hard. We add another class for the purpose of authentication.

Common problem in web applications

Since most web applications have to store the user information and authenticate them some time later, this is a quite common problem in the field. When you are building a  User class next time, it could be a good idea to adapt the above design even before you can have different method to login.



Using GitHub for Mac/Windows as a GUI client

I always have a uneasy moment viewing Git diff and logs in the terminals. It’s not easy to navigate to different check-ins since you have to copy and paste the check-in hashes. Recently I reached to the almighty Google god and seek for solution.

GitHub for Mac/Windows

GitHub for Mac/Windows is a GUI Git management tools developed by GitHub, obviously. It doesn’t only allow you to examine your GitHub repositories but also your local repositories.

GitHub for Mac


The UI is neat. Almost everyone can pick up immediately. I suggest you to try it out if you use Git for your projects.


GitHub for Mac
GitHub for Windows


Head First Design Patterns


Head First Design Patterns is a light weight introduction to object oriented design patterns. It approaches each design patterns with different perspectives and compares them to each others.

However, just like another book in the Head First series, this book contains tedious exercises and not-so-funny cartoons which I would normally skip through. Those parts make up 40% of the book which is quite annoying.


Nevertheless, if these don’t bother you, it will still serve your needs to dive into the world of design patterns.

Edit: Some readers misunderstood me. I don’t really hate the book, but it would be much better if the editor can improve the exercises and the way they the story. Other than that, I still think HFDP is a great book that can guide you into design patterns step by step.

PHP: Unexpected behaviour in nested array

It’s a trap!

Let me first show you some code at the beginning.

Guess what it will print?  [["Hi"]] ?


Wrong. It remains  [[]].

Where it begins with

It is a piece modified code of my project. It surprises me that  $aVeryLongNameWhichHoldANestedArray doesn’t change. Isn’t $shortHand  suppose to be a reference to the element instead of a copy?

Let me show you the same piece of code to you written in Javascript and Ruby.

In Javascript and Ruby,  aVeryLongNameWhichHoldANestedArray is  [["Hi"]], which to me is a lot more reasonable.

Let’s take a look at something similar

So now, what will it print? It often surprises you again. It prints  [stdClass Object([hi] => Hi)].

Pitfalls of languages

No programming language is perfect when it applies to software engineering. Even C has some pitfalls after carefully standardised by ANSI. The fallthrough switch is one of them, which is considered a feature during standardisation, but it isn’t used most of the time – programmers just auto insert breaks after each case. Unluckily, PHP inherits that too.

Private Constructor

Few years ago

I recalled a story today. When I was studying university, I asked my Professor who taught Java:

What is the use of private constructor?

He didn’t have the answer. We thought of a few possibilities but weren’t so sure about them at the end of the day.

The Private Constructor

We cannot instantiate MyClass outside it. new MyClass() can only be called inside  MyClass. Is it useless?

The Singleton Pattern

Turns out the private constructor is useful for the singleton pattern to avoid other classes instantiating the singleton on their own.

From this, we can restrict the  MySingleton class instantiates at most once throughout the run time of the application.


In bridge, it is also called Singleton if the hand contains only 1 card of a suit.


No, I am not anti-PHP, not in that way.


I often talk casually about how bad PHP is and people often misunderstood that I am a PHP hater. In fact, I don’t hate PHP and even use PHP in some of my projects. I just feel that PHP is a sub-optimal choice in most situations.

PHP has its long history and popularity in web pages and applications over any other languages. In recent years, it has been frequently compared to Ruby and Python, saying that PHP is inferior in its programming language design. While most of the criticisms are valid, I still think there are situations and reasons to use PHP over other languages.

2 reasons for me to use PHP

1) The skill set of your team

If the developers in your team has little experience in other languages, forcing them to code in a new language can be very risky to your project. Even if you have great developers, it could take half a year for them to feel comfortable in a new language. For less skillful developers who are familiar with PHP, if they write Java, they would still be limited by the mind set of PHP. Eventually they will be coding PHP in Java.

In case you are in a startup company working for the prototype, avoid going for a new technology. Otherwise you will end up in wasting time in familiarizing yourself with INSERT_YOUR_COOL_LANGUAGE_HERE.

2) Simple web pages

The way that PHP originally designed favors plain HTML in .php files. PHP can be a great choice if the majority of your content is static. For example, if you want to work on a few web pages which shares the same header, you can simply do this in PHP.






Achieving the same simplicity in other languages may require an additional library or a light weight framework. Doing this in a heavy full stack framework such as Rails or Django? No thanks.

Also, setting up a PHP server is relatively simply and straight forward. There are also packages that provides a ready-to-use LAMP (Linux, Apache, MySQL, PHP) platform for production.

Invalid arguments that PHP is the choice

1) Facebook uses PHP too!

Having some giant companies that use PHP doesn’t mean it is suitable for you. Facebook, WordPress and Wikipedia could have invested a lot to overcome the drawbacks of using PHP. Facebook puts a lot of effort to optimize PHP to handle 1 billion active users. They even tried to compile PHP into C++ to take advantage of the gcc compiler optimisation. You may also want to dive into the code of WordPress as it’s available to everyone. It can take you quite a while to understand it.

2) It doesn’t matter if you are a good developer

There are some fundamental properties in the programming language that you can’t change not matter how good you are. For example, you can’t change the inequality operator <>  in Pascal even if you think it is better to use !=  as in C. You cannot change C into a dynamically typed language when your application needs it.


The majority of programmers like to describe programming languages as tools. But in my opinion, they are more like materials, especially in software engineering. Once you decided to use bricks to build a house, your designs are limited within the bricks. You have to consider the weight, the durability, and the cost of the bricks when drawing the blueprints. Later on if you want to rebuild the roof with some other material, you still have to consider the properties of the bricks, whether the brick walls can withstand the weight of the roof that you build with the new material.

Similarly, if you choose PHP for building your application, you will live within it. You have to beware of its relative slow performance when comparing to other compiled languages if you are building high usage real-time application. Also, PHP may not be the choice if you are writing multi-thread application since it is not designed for it. Later on if you want to optimize your application by rewriting the ORM in C to throw in tons of low level optimization, you still have to consider the interface between PHP and C.


Despite the fact that I consider PHP as a sub-optimal choice for most cases, I still see PHP is suitable for some cases, especially in small web sites. However, don’t take successful companies that use PHP as a proof that PHP is better. Remember your building material always matters. Maybe you agree to none of my statements here. That’s fine. But the very bottom-line is, be conscious to your decisions and convince yourself why you’d make that choice with valid arguments.