TechWire

Author - Thameera Senanayaka

The Javascript Revolution – Part I

Scott Hanselman, the popular blogger from Microsoft, asks: if you had to start over, what technologies would you learn in 2014? It’s both an interesting and a timely question, when the software world is overflowing with new languages, programming paradigms, platforms and frameworks. Which ones to choose? Not only the newcomers to the industry, but the veterans as well seem to be in doubt about what language or technology he should master next.

Hanselman has an interesting answer to the question:

Learn one language you can build large systems with AND also learn Javascript.

That one language might be Ruby, Python, C#, Java, Scala or anything from the myriad of other choices available. That choice may depend on where you work at and your personal preferences. But Hanselman also goes on to declare that learning Javascript a must for everybody. But let us stop for a moment and ponder: why Javascript?

A decade back, Javascript was only a scripting language used to add fancy animations and validate inputs in websites. It gained popularity since it ran in the browser (as opposed to in the server) and almost all web browsers supported it. But that’s all Javascript was good for back then.

Then in 2007, Jeff Atwood, co-founder of Stack Overflow, proposed what was to be known as ‘Atwood’s Law‘:

Any application that can be written in Javascript, will eventually be written in Javascript.

It was a pretty radical statement, even in 2007.

Fast forward to 2014, we see Javascript everywhere. From client-side and server-side web apps to mobile and desktop apps, Javascript is being used almost everywhere. Never has any other programming language been able to become omnipresent to this extent.

Though Javascript started as a client-side language for webapps, it has now conquered the server-side as well with Node.js. Node is to Javascript what’s Rails is to Ruby. Many tech giants like Paypal, LinkedIn and Yahoo have begun to ditch their previous (and successful) web frameworks in favor of Node.

Nodejs

Projects like node-webkit have enabled developers to write desktop apps in Javascript. The effort is still in its early stages, but the community is ever more enthusiastic and are actively working to see a light in this horizon. As for mobile apps, a considerably large percentage of apps for Android and iOS are now being written using Phonegap. Ubuntu is pushing the developers to write more and more native apps in HTML5 that would work on both desktop and mobile devices.

It has made its way to hardware programming as well. Tessel is an upcoming microcontroller (similar to Arduino and Raspberry Pi) that runs on Javascript. There are other projects like Espruino that are already in the market.

One bitter point of Javascript used to be its low performance record. It was okay for short bursts of calculations and event handling, but when it came to the nitty-gritty of complex problems Javascript always lagged behind. But the tide has turned with the advent of asm.js, an optimized subset of Javascript, which can be generated by transpiling C or C++ code. This has paved way to create FPS games, database engines, and even video converters that would run in your web browser!

Keep calm and learn Javascript

Has Javascript become the one language to rule them all? We wouldn’t know yet. In the coming weeks, we’ll explore how and why Javascript has become successful in each of these domains in detail. Stay tuned!

Book Seeker will save you hours at the Colombo International Book Fair

The time in the year every book lover is waiting for has come. The Colombo International Book Fair is just hours away. It’s time to grab the hardly-earned money and run to BMICH before another book worm grabs the most sought-after books.

But, let’s face it, it’s not as easy as it sounds. During the nine days of the fair, BMICH is packed with book lovers from all over the country. There are long queues at the stalls. Finding the exact book you are looking for would not be the best experience you’ve had. That’s exactly what Book Seeker aims to solve.

Book Seeker is a mobile application developed by a group of undergraduates of the Faculty of Engineering, University of Moratuwa with the collaboration of Dialog and Sri Lanka Book Publishers’ Association. It aims to make use of the technology to let you find where you should be looking for to buy your book.

Being SMS-based, you don’t need a high-end mobile device to use Book Seeker. Any phone that could send a simple text message and a Dialog connection would do. After registering for the service (more details below), you type in ‘isk’ followed by the name of the book you are looking for and hit Send. Within a few seconds Book Seeker would reply you with an SMS with a list of book publishers who would be selling the book at the fair. It would also include the stall numbers they will be in.

Book seeker sample results

You have to type the name in English, even if it’s a Sinhala book. For example to look for Gam Peraliya, your SMS would be ‘isk gam peraliya’. Even if you made a small typo in the name, the app is intelligent enough to find the correct book for you. In case the terms has no immediate matches, it will show up with a list of possible suggestions as well.

How to use Book Seeker:
1. Send reg<space>isk to 77115 from your Dialog mobile. This is a one-time registration and you will receive a confirmation SMS.
2. Type isk<space><book name> and send to 77115. Book Seeker will get back to you with the stalls you can find the book in.
3. Repeat the 2nd step for all the books you need to search.

Book Seeker banner

It is a simple concept, but one that solves an actual problem. The team has put forward a commendable effort to make the lives of every book lover easier. So if you’re planning to visit the book fair in the coming days, make sure you do check the books with Book Seeker beforehand. It would save you hours of precious time you could have spent reading those gems.

Minuum – a solution to touchscreen typing?

When it comes to keyboards, Android boasts of a large eco-system of third party apps. Unlike in the world of iOS, you can choose from the dozens of available keyboard apps and even customize them to your wish. The most popular among these include SwiftKey, Thumb keyboard and Swype.

Even though these keyboards are filled with heaps of fancy features, all of them suffer from one major problem: they take way too much space on your mobile’s precious screen.

(Image source: http://www.wired.com/images_blogs/gadgetlab/2013/02/130219_SwiftKey_0070.jpeg )

Minuum is trying out a different perspective to solve this problem. They are introducing a keyboard that squeezes all the keys to a single dimension to reduce clutter.

The keys in the row aren’t randomly scattered. In fact, they represent their horizontal positions in a standard qwerty keyboard. This means you won’t have to frantically search for letters while typing.

Minuum also aims to go beyond the touchscreen and implement the same technology to enable typing in other mediums, such as analog game controllers, camera-based systems like Google Glass and other wearable devices.

But the biggest problem remains. Would you still be able to type fast while maintaining good accuracy? Less screen size usually means less accurate key presses. The Minuum site however assures that the smart auto-correction and word prediction algorithms embedded in the app would let you type even faster without worrying about being precise.

How would Minuum fair with the rest of the popular keyboard apps that already have huge userbases? It’s too soon to tell. Meanwhile, you can join their beta list to receive a free beta version of the app. That way you’ll be among the first users of an awesome keyboard if it really hits (and we sincerely hope it would). Even if it doesn’t, you have nothing to lose.

The perfect swap in C++

So you are coding this cool app and you need to swap two variables. How does a good programmer do that in C++? The STL (Standard Templates Library) provides the std::swap function which does exactly what we want.

[code language=”cpp”]int a = 10;
int b = 12;
std::swap(a, b);[/code]

That’s easy. But hey, why don’t we go ahead and see actually what std::swap does behind the scenes? A grep in the Apache STL implementation gives us:

[code language=”cpp”]template <class _TypeT>
inline void swap (_TypeT& __a, _TypeT& __b)
{
   _TypeT __tmp = __a;
   __a = __b;
  __b = __tmp;
}[/code]

Woah, all the underscores! But don’t panic just yet. All it does is the grade-school swapping:

[code language=”cpp”]T tmp = a;
a = b;
b = tmp;[/code]

Hmm. That is perhaps the most straight-forward swap implementation. But how does it perform? Look again.

[code language=”cpp”]T tmp = a; // a copy of ‘a’ is created
a = b;  // a copy of ‘b’ is created
b = tmp;  // a copy of ‘tmp’ is created[/code]

That’s a lot of copies for a simple function! What if we could just ‘swap’ the two values without copying?

We google around a bit and find out that the above std::swap implementation is actually the old way of doing things. The new C++11 implementations does this differently. So we check the C++11 include files.

[code language=”cpp”]template<typename _Tp>
  inline void
swap(_Tp& __a, _Tp& __b)
   {
   _Tp __tmp = _GLIBCXX_MOVE(__a);
  __a = _GLIBCXX_MOVE(__b);
   __b = _GLIBCXX_MOVE(__tmp);
  }[/code]

That’s more confusing than the previous one. Again, don’t panic. We can simplify the things. _GLIBCXX_MOVE is defined to be std::move. Let’s just call it ‘move’. So the above function is roughly similar to:

[code language=”cpp”]T tmp = move(a);
a = move(b);
b = move(tmp);[/code]

Now we are scratching our chins. At first glance, the implementation looks much similar to the grade-school swap. And then, there’s this move-thingy. Okay, looking back, we remember that the elements were ‘copied’ in the grade-school algorithm. Instead, it looks like the variables are ‘moved’ here.

And we are right! In the first line, tmp is set to the value of a, while the variable a is (temporarily) invalidated. No copying is done. The previous memory location of a is now the territory of tmp. In the next line, a is set to the value of b, while b is invalidated. Finally, b is set to the value of tmp, and tmp itself is invalidated, which we don’t need again anyway. And the result? The two values are swapped without any copy operations!

How does this moving really work? C++11 introduces the so called “rvalue references”. The ‘move’ function returns the rvalue of the input parameter without triggering a copy construction.

[code language=”cpp”]T &a = x; // normal (lvalue) reference
T &&b = y; // rvalue reference [/code]

A full description will not fit in this post, but you can go through this nice little introduction on rvalue references. You might also want to refresh your memory about lvalues and rvalues.

And let’s call it a day and meet again with another little C++ adventure.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html

Twitter in Sri Lanka through the years

Twitter has always played a giant role in Sri Lanka’s social media scene. With only a handful of Sri Lankans a few years back, Twitter has now grown to tens of thousands of tweeps from all over the country tweeting their thoughts away.

At first were the geeks. Those who always try to keep up with the technology and test out new things. Along came the bloggers from both English and Sinhala blogospheres. It was a place for them to promote their content, meet fellow tweeps with common interests and discuss everything happening around.

Organizing committee of the first ever TweetupSL (Image source)

One of the earliest milestones of the Sri Lankan Twitter community was the first ever TweetupSL. A tweetup is in fact a gathering of tweeps. Friends you make in your twitter stream meeting in real life, face to face. The event took place in 2010 in Coco Veranda, one of the first Sri Lankan businesses to have a commendable presence on Twitter.

Some of the attendees at TweetupSL 2 (Image source)

Since then, two more TweetupSL happened in 2011 and 2012 respectively, each with far more success than the previous ones. The community has grown in leaps and bounds. More businesses joined Twitter and several big events were organized in the twittersphere. Mini tweetups, hashtag nights became a common occurrence. Not to forget all the lovers who first met on Twitter. 🙂

Learn Startup Engineering Online….from Stanford!

How many of us so badly want to get away from that routine job and work on our cool startup idea? But still, how many of us shy away from the whole deal as we feel we are strangers in the world of startups and have no clue where to begin?

The newest course offered by Stanford University might be the one that may change your life. Startup Engineering aims to guide you through the process of building and scaling a startup from the ground up.

Startup Engineering is a 10 week course focusing 50% on technology and 50% on the philosophy behind startups. Besides from the usual video lectures and course notes, the students are required to complete weekly programming assignments and finish the final project – a crowdfunding site based on bitcoin and SelfStarter. Lectures are conducted by Balaji S. Srinivasan and Vijay S. Pande from Stanford University. About 100,000 students have already registered and the forums are filled with energy and enthusiasm from all over the world.

The course is available for free on Coursera. It just started (on 17th of June) and you can join within the coming days to make sure you aren’t missed out.

Visit the course home page: http://online.stanford.edu/course/startup-engineering

Auto-matic typing in C++

C++ is a statically typed language by design. In other words, you have to specify which data type your dear variable is supposed to be. If you declare myCoolVar to be an integer, you can’t let him play with his string friends. Type checking is done at the compile time. So myCoolVar will soon be caught red-handed if it goes matchmaking with a string or a vector.

This is contrast to the radical kids in the block like Python and Ruby who don’t care whom their variables play with. They call themselves dynamically-typed. A washed up floating point might become a variable with a character in a few moments.

Cool as it may sound, dynamic typing is not for everyone. C++ is mostly used where performance and reliability are key factors. Thus the C++ standard has always insisted on strong, static typing. But even if you haven’t been programming since the dinosaurs roamed the earth, you may know that it’s not always easy to remember and explicitly declare the correct variable type. The new C++ standard, or C++11  as it’s known, aims to change this.

C++11 isn’t exactly new to the city. Most compilers around, including gcc and Microsoft C++ compilers, now include support for C++11. Among the dozens of improvements it has brought, let’s have a look at the auto keyword.

Suppose you want to take the length of a C++ string called myStr. Easy-peasy. myStr.length() is the way to go. But wait. The return value of the length() function is string::size_t. Besides from being hard to type in, it’s not easy to remember all these return types either. That’s when the auto keyword comes handy. Instead of,

[code language=”cpp”]string::size_t len = myStr.length();[/code]

you can simply write

[code language=”cpp”]auto len = myStr.length();[/code]

The compiler detects the return type of myStr.length() and makes len a variable of that type. So very convenient.

Not convinced? Suppose you have a map of objects that you need to iterate. How do you initialize the iterator?

[code language=”cpp”]std::map<char,int>::iterator it = myMap.begin();[/code]

Ugh. That isn’t the nicest code snippet you’ve written. But hey, we have our friend auto:

[code language=”cpp”]auto it = myMap.begin();[/code]

Skadoosh! Auto has automagically made it an std::map<char,int>::iterator, because that’s the return type of myMap.begin()! Now that does look nice.

What if I want to declare a variable with a default value?

Suppose you want to declare a variable that holds the length of a string. But you first need to set it to zero.

[code language=”cpp”]auto len = 0;[/code]

Ha! That is wrong! This would make len an int, not string::size_t. But C++11 has an answer for that as well. You can use the decltype keyword like this:

[code language=”cpp”]decltype(myStr.size()) len = 0;[/code]

This declares len as a variable of type returned by myStr.size(), but still initializes it to zero.

Is this dynamic typing?

No, not really. Even though it appears as if auto changes a variable’s type dynamically, it actually happens during the compile time. It’s just that you don’t have to explicitly code the type of the variable, the compiler does that for yourself. Which is mighty sweet of C++11.

Is it safe to use auto?

Isn’t letting your variables roam free considered as bad parenting? No. Not only is it safe to use auto in your C++ code, it’s even recommended that you use it wherever possible. Bjarne Stroustrup, the creator of the C++ language himself, advocates its use. Just make sure that your compiler is updated to support C++11.

Image Credits : http://geekandpoke.typepad.com/geekandpoke/2010/01/best-of-both-worlds.html