Category Archives: Programming

[Solved]Lag issue with meteor and stuck at loading

Meteor is the web framework that I have been learning for past few weeks. It is powerful and easy to learn but some functions are still under-developed hence some bugs.

When playing with the Todo List example, I discovered a bug in the example. The example has a function to display a “loading text” in item panel when the user selects a new list and the corresponding items are being loaded from database. However, if the user switches between the lists very fast and selects the next list before the current list is loaded, the item panel will be forever stuck at loading. This may not be a issue when running on a local server but it would be much more serious if the Internet connection to server is slow.

I am not entirely sure of the cause of this problem but it has to do with how DOM elements are updated when the variables get changed. For some reason, if the Meteor.subscribe() method does not return before the next execution, the DOM elements will not be updated.

I solved this issue by using a session variable, which is altered when new list is selected. In this way, a selection of new list will always trigger the change in session variable and hence update DOM accordingly. Below is the relevant code modified by me:

This technique is related to meteor’s reactivity, here is the reference on Reactivity for meteor.

On Whatsapp and the less known Tencent QQ

Recently the news on WhatsApp being bought by Facebook is all over the place, it seems to be a big deal. As a user of WhatsApp for about 2 years or so, I went ahead and posted about “how shocking” I was when I learnt that the messaging “giant” had been bought by the SNS “giant”.

However, when I really think back on WhatsApp and its exact features, something else came to my mind. It is the messaging application that I have been using since about primary 6 back in China – Tencent QQ. And surprisingly, or rather not, I realized that it is technically much more older than WhatsApp but with all the functions that WhatsApp provides.

QQ has all the basic chat functions, it also has a group chat function(QQ群) since 2000, which supports up to 500 users in the group, and within the group, you can send text, voice messages, pictures or even files to all the members. It appears that QQ group would be older and more capable than WhatsApp in terms of group chat functions.

In the beginning, QQ only runs on computers. However, that was the era when phones did not have coloured screen. By the time Nokia phones with its Symbian OS became popular in China, QQ has been able to run on Symbian OS. And when iOS and Android came out, respective QQ apps were also developed by Tencent to run on these platforms. So QQ also beats Whatsapp in terms of cross-platform compatibility.

So what is the exact reason that causes Whatsapp to be much more well-known(outside China) than QQ? Why people(outside China) did not use QQ before WhatsApp came out? I tried to come up with a few possible reasons:

Possible reason 1: Tencent’s Marketing strategy. We can safely assume that the Chinese market by itself is big enough for Tencent and it would not want to devote manpower and resources to market it outside China. (We can see that Tencent is now putting efforts to market its WeChat outside China and it is largely successful)

Possible reason 2: General impression and stereotypes on Chinese products. This could also be related to the general stereotypes that people have about Chinese products, i.e. unreliable, potentially linked to communist ideas. So even if Tencent did try to promote QQ outside China, it could have  failed because people just do not trust Chinese products.

Possible reason 3: The time factor. Maybe QQ was just not developed too early, not at the correct time to be successful. This happened to Microsoft when it tried to sell the idea of tablet way back before iPad. It could be that the need for instant messaging on mobile devices just came about in recent years. WhatsApp seized this this opportunity and cater to people’s need at the correct time.

Possible reason 4: WhatsApp is more focused and links to phone number: WhatsApp is only a messaging app, it does not come with other functions like QQ games, QQ pet or QQ show(a virtual avatar that you can modify like a Barbie doll). WhatsApp is also linked directly to the phone number so that it can get all the phone contacts on WhatsApp and act as a replacement for SMS for people with internet connection. This makes WhatsApp more professional as a messaging app. People can just enjoy messaging like sending normal SMS without distractions. After all, we are comparing them as messaging applications.

Possible reason 5: Ads. Ads is one significant feature of QQ, it appears almost everywhere unless you pay some kind of membership fee. This is arguably the preferred method of many apps nowadays, however, for WhatsApp , we see no ads at all and instead of paying the yearly subscription fee, somehow I can use it for free for over a year with my account automatically renewed.

That is all that I could come up with at this moment.  And I am not sure if they are sufficient to provide an explanation. Anyway, back to WhatsApp and Facebook time!

Add-on: On the topic of growth and expanding market internationally, here is a relevant answer from Quora.

How important is Google as a search engine, really?

When viewed in an inertial reference frame, an object either is at rest or moves at a constant velocity, unless acted upon by an external force.

– Newton’s First law

There is some issue with my connection to Google today. And surprisingly(or rather not surprisingly) there seems to be no problem in connecting to Bing. (And actually it is faster than I expected for displaying search result.)

This makes me think about the importance of Google, as a search engine. For all this time, I have been using Google as my default search engine, without wondering why it is default in the first place. Then I realized that the only reason I am not using other search engines like Bing or Yahoo right now is that I am used to Google. I am totally unsure of how Google is better than other search engines. In fact, I know that other search engines have their advantages over Google, for example, Bing gives the answer for gcd function in search whereas Google does not. How exactly does this “default” habit help Google to be the No.1 search engine?

In times of break-down like this, I would not hesitate to switch to an alternative search engine. Afterall, the information that I am looking for is on sites such as Quora, Facebook or Wikipedia, not on Google. It is just a tool for me to get to the destination that I wanted, and when one tool breaks down, why not just take another tool, knowing that it would work equally well?

Perhaps that is reason why Google is spend resources on Android, Google+, YouTube and more recently Google Glass. Being the default search engine now does not guarantee the default search engine forever, in fact, it is hardly the case. What is really needed is an ecosystem that make people depend on Google, not just in terms of searching. People are using Google’s different services, like Gmail and Google Calender because of the benefit of this whole integrated data and user experience.

The irony is that Google is so slow right now that I have to use Bing to get this image

The irony is that Google is so slow right now that I have to use Bing to get this image

In the end, I feel that Google, as a search engine, may not always be the default for people. When it fails at times, people may consider changing it. However, if the ecosystem of Google, the whole package of services, become the default for people, it will be much harder for people to switch. Even if some major disruption happens, people may still want to stick to Google, where they store all their data.

Data really means a lot. For me, all the data I stored in Google may be sufficient to create an identity for me.

Tip and tricks on using the eclipse

I have learnt from CS2020 a few useful tricks to make my life easier and an enjoyable one when using the eclipse.

This is a feature, not a tip, but I really like the automatic indentations in eclipse. When copying and pasting codes, they are automatically indented in the context, saving the trouble of indenting them manually.

Here are some useful short-cuts and tips I learnt so far.  This list may be expanded in the future.

Multiple edits (of the same variable name)

Alt + Shift + R

Very useful for changing the name of variable halfway

Auto-complete

Crtl + Space

This is like tab in linux and sublime text. eg. sysout in java will be auto-completed as System.out.println();

Auto format

Ctrl + Shift + F

Automatically add or delete extra spacings to make code neat and clear, also fixes the over-lengthy lines

Auto fix import

Ctrl + Shift + O

Automatically add the necessary imports and delete unnecessary imports for the class

Auto-add getters and setters

Source – Generate Getters and Setters

This automatically add these two classic methods to the class body

Extract method

Select the code and right click and choose Refactor, extract method

This helps to break down a long method to several sub-methods to make the code more organized, also helps to maintain layers of abstraction by establishing hierarchy in methods.

CS2020 Learning Sorting

Sorting was one of the first things that I learnt for programming, and inevitably we had to go through this as part of this algorithm module.

The truth is I never really paid much attention to sorting until now, since I  knew that I can just use the buit-in qsort function in most cases. However, this is certainly not we learnt during this module, and we are dealing with more problematic sorting, involving analysis of different types of sorting algorithm in terms of running time and space usage(and I realised what we have learnt from past few weeks is very similar to the first two chapters of book Introduction to Algorithms)

The different sorting methods covered so far are:

  • Bubble sort Stable

A total of n iterations for n inputs, for each iteration, swap the two neighbouring elements if they are not sorted.

Bubble sort has a loop invariant – at the end of jth iteration, the biggest j elements are correctly sorted in the final j positions in the array

Running time is O(n²), best case is O(n) if array is already sorted, Space usage is O(n).

  • Selection sort  Unstable

A total of n iterations for n inputs, for each iteration, find the smallest element from unsorted part and put it(by swapping) at the end of sorted part.

Selection sort also has a loop invariant – at the end of jth iteration, the smallest j elements are correctly sorted in the first j positions in the array

Running time is O(n²), best case is O(n) if array is already sorted, Space usage is O(n).

  • Insertion sort  Stable

A total of n iterations for n inputs, for each iteration, put the first unsorted element  to the correct position of the sorted part.

Insertion sort has a loop invariant, which is slightly different from selection sort – at the end of jth iteration, the first j positions in the array is sorted, however, they may be subjected to “insertion” on the subsequent iterations.

Running time is O(n²), best case is O(n) if array is already sorted, Space usage is O(n).

  • Merge sort  Stable

It uses the idea of divide and conquer, divide the array into two halves on each iteration and sort them during the merging process. It is relatively easier to sort the array during merging as we assume that the arrays to be merged are already sorted in the first place.

Invariant – the arrays to be merged are sorted before the start of merging.

Running time is O(n log n), best case is still O(n log n) if array is already sorted, space usage is O(n log n) for a simple approach, which can be improved to  2n+O(1).

  • Quick sort Unstable(Classic)

For each iteration, a pivot is chosen and the array is partitioned into two parts, one part with all elements smaller than(or equal to) the pivot, the other part with all elements bigger than the pivot.

The invariant is the same as the mechanism of quick sort.

For the process of partition, running time is O(n), space usage is n+O(1), where O(1) accounts for the process of swapping. The running time for the whole process can be improved by optimizations(dealing with duplicates by three-way-partition and etc.). The worst case is O(n²).

The pivot can be chosen deterministically or randomly.

  • BogoSort Unstable

This sorting method involves choosing a random permutation of the array A and return it if it is sorted. A related joke(or maybe not) is QuantumBogoSort which uses quantum computing concept and many-worlds interpretation