Tuesday, April 30, 2013

April 30, 2013


This would be the first of my monthly recaps. I intend this to be one of the avenues I sprinkle some personal touch on this blog: a break from my seemingly impersonal technical post and rants. I hope I get to sustain it. :)

Trip to Nigeria
I was back in Nigeria from 22th March to 5th April. It did not feel as if I have been away for that long (don’t know if that is a good or bad thing). But the trip was nice. Met up with family and friends, and took a break from the gray weather of the Netherlands! :p

Hung out at CChub…for the first time!
Was already out of the country by the time CChub (Co Creation Hub) was kicking off, so it has always been at the top of my list… a really “must do list”… to pay CChub a visit whenever am in the country. I did. How could I have not?  :)

Cchub is as I imagined it to be. I am really excited that such a space exists, at least if for nothing else (creating an open innovation ecosystem, Pre-incubation to start-ups  gingering the Tech. community etc.), for providing a shared office space with constant power and internet developers.

Gtbank sucks!
Like really. The customer friendliness of the bank has taken a rather sad dip. Gtbank used to be the friendly bank: staffs willing to help, smiling and courteous and all; but my experience at the bank was really disheartening. Frowning and saucy staffs was the order of the day. Sad!

Then long queues have started creeping into Gtbank. I spent 2 hours at one of the Gtbank branch in Ibadan! Reminds me of the horrors of having to go to Union bank/First bank back then!

Not happy that Gtbank is going down this way. :(

Inverters have gone mainstream!
Well, that isn't surprising..is it? 

And...

MTN still sucks! 
Need I say more?

Cologn and Bonn...A nice weekend
The next weekend after I got back from Nigeria saw me in Germany (Cologne and Bonn). Hung out with a couple of colleagues I worked with last year on AIESEC International. It was nice to see everyone is fine and doing good. Meet some other guys in Cologne that were really into rap music...kindred spirits, we are, I’ll say and we spent some nice time bopping heads to some Biggie, Coolio, ‘Pac, 50, Dre and Kriss Kross.

While in Germany, I even got to compose a poem…while in a club :) 

SimpleJSMap
April saw me making simpleJsMap public. It’s A MAP-like implementation that I wrote in Javascript that makes saving stuffs on the client side easier via keys and values. 

JProwork
I also completed most of the work on JProwork and its ready to go. JProwork is a Java API wrapper for Prowork.

Kicked the Facebook Addiction
April is the month I eventually managed to rid myself off the attention sucking and time wasting and zombie turning Facebook! It was getting too much. It was like an addiction and it was getting frustrating. It got to the point that I couldn't concentrate on a task at work for 30 minutes straight without having the urge to go check Facebook. At that point it became obvious it was detrimental to my well-being and I needed to kick it out. I toyed with the idea of totally deleting my account but kept on holding back at the final stages; all thanks to Facebook’s various tactics and emotional blackmailing during the deletion process.

So instead of deleting, I trained my will power to totally get rid of Facebook. And I did it. I feel a lot better, less drained and focused now.

Queens Day!
Yes today is April 30! And in Netherlands it’s the Queen’s day! But in Amsterdam, its not just Queen's Day it is one hell of a party! Somewhat a street carnival. Everyone dressed in orange, on the streets, marijuana smoking, party rocking, and Liquor flowing!

It’s crazy!

So you guessed it, once I press the publish link on this post, I am hitting the streets too!

And if you ask me, it is sure a nice way to bring April to an End :)

Saturday, April 27, 2013

JProwork Ready to Go

JProwork is a Java wrapper for Prowork. 

Prowork itself is an intuitive project management app that makes team collaboration easy. It was launched 2011 at Start-up Weekend in Nigeria. It recently won the Apps4Africa 2012: Business Challenge. 

For more information, check: About Prowork and the team behind it and  How it works.

Overview
JProwork is then a Java wrapper for Prowork’s API. It encapsulates the API calls to Prowork and exposes Prowork’s entities as Java objects to the developer. 

As can be seen below:


It sure would make developing on Prowork faster with Java. 

JProwork has been a weekend project for a couple of weeks now and right now it is done as far as covering the existing Prowork's API is concerned. Just need to put some structure here and there regarding versioning before we can have a first official release. But in the meantime you can grab the Source code from Github and to give it a spin, compile the source, update your class-path as necessary and you should be ready to go:

Dependencies
Jprowork requires Gsona Java library that can be used to convert Java Objects into their JSON representation and also to convert a JSON string to an equivalent Java object.

Roadmap and versioning
Things left to do?

Have a proper versioning structure in place. I plan to go with the format of: <major>.<minor>.<subminor> which, in this case, would translate to  <Prowork's API Version>.<Major Changes in Jprowork>.<Minor and Bugfixes>

Mavenized JProwork and also have it available as Jar files.

Documentation
Documentation is in the code. You can grab a copy of the documentation using Javadoc. A generated Javadoc is also included in the repository.

Do give it a spin ;)


Saturday, April 20, 2013

simpleJsMap


I find having to riddle the Dom with stuffs dirty :(

Just because there is no other way to easily keep stuffs on the client side, we sometimes do this; and it is painful! having to unnecessarily pollute the DOM.

I was in such a situation a couple of months back where I was on a project in which stuffs, AKA. the states of the application were being pushed to the DOM attributes just to keep them.  Dirty!!!

I didn't want to continue doing this, so instead, I rolled up a little thingy that enabled me put my stuff out of the DOM. :)

I called it simpleJsMap.

simpleJsMap is a simple MAP-like implantation that enables me to keep and retrieve my stuffs easily in the memory via keys and values without having to set things in DOM attributes.

This felt a lot cleaner :)

simpleJsMap is on github so it can easily be grabbed, and hopefully, someone apart from myself finds it useful. :)

Getting Started

Include the simplejsmap.js file into your script. The variable simplejsmap would then be available. 
<script src="//path/to/simplejsmap.js" type="text/javascript"></script>
<script type="text/javascript">
// simplejsmap now available
</script>

I am a big fan of pure Javascript first before framework, thus simplejsmap is not dependent on any Javascript framework; not even jQuery :p

So once you have the script included you ready to go.

Function reference


createMap()
var map = simplejsmap.createMap();

Creates the map like object and assigns it to variable map. You always start using simplejsmap by calling this function.

add(key, value)
var map = simplejsmap.createMap();
map.add("key1", "value one");
Adds stuff that needs to be kept. The stuff has a value and is associated with a key. Returns true if successfully added and false if not. If the key is already present, nothing happens and false is returned also. To modify the value of a key that is already added, update() function is used.

key and value can be of any Javascript type.

update(key, value)
var map = simplejsmap.createMap();
map.update("key1", "updated value");
Updates the value already added by a given key.  If the given key exists, its value is updated. If the key is not found, no update operationis done. False is returned instead.

remove(key)
var map = simplejsmap.createMap();
map.remove("key1");
Removes a value accessible via the given key.  If the given key is already in existence, it is removed and true is returned. If not, false is returned.

get(key)
var map = simplejsmap.createMap();
map.add("key2", "Hello World");
var val = map.get("key2");
Gets the value stored via given key.

getLength()
var map = simplejsmap.createMap();
var len = map.getLength();
Returns the number of stuffs/keys that has been added.

getKeys()
var map = simplejsmap.createMap();
var keys = map.getKeys();
Returns all the keys that has been added as an array.


UPDATE
simpleJsMap can now be gotten via bower. Just use "bower install simpleMapJs" to add it to your project.


Friday, April 19, 2013

Callback functions in loops in Javascript

In other to implement the Reddit twitter bot for r/Java (@redditJava) and r/programming (@redditprogrammn), I had to get the lists of entries from Reddit, loop through it and call a function to tweet each item. On successfully tweeting, a call back function is called that stores the id of the item that was tweeted. This list of saved Id is checked every time, before tweeting, in other to prevent double tweeting.

A simple way to get this done is to get the items to be tweeted in a form of an iterable (array or object in javascript), loop through the items and in the loop, put the logic that tweets the item and the call back function.

A pseudo code would look thus:
for (var n=0; n < thingstotweet.length; n++) {
      // tweet function
      Tweet(thingstotweet[n], function(e,r) {
         // the call back function
          //if no error, then save the id of the tweet
          if (!e) {
              Db.save(thingstotweet[n]);
          }
      })
   }

In Javascript, this is a faulty implementation. If you implement such a solution for having callbacks inside a loop you would soon find out that the things gets out of sync. For instance in the tweeting example, one tweet would be sent while a different one would be saved in its place. And the reason is simple: It takes time before the function sending the tweet returns and the call back is called. And since functions call like this in Javascript are non blocking, in the time it takes before the calling function returns and the callback called, the value of n would have changed leading to saving the wrong item. 

For example.
1. Start the loop when n is 0;
2. Tweet the item in index 0 in the thingstobetweet list
3. The tweet function is sending the tweet but it takes a couple of seconds.
4. Value of n increased to 1. Still tweet has not been sent, so call back Is not to be called yet
5. Value of n increased to 2. Still tweet has not been sent, so call back is not to be called yet
6. Value of n increased to 3. Finally the tweet was sent successfully, time to call the callback
7. The call back is called. But n is now 3. So thingstobetweet[3] is saved for a tweet at thingstobetweet[0]

How then do you deal with this? What would be the correct way to implement a task that needs a callback function inside a loop?

There are two ways I normally implement a solution for this specific scenario:

Use Closures

In a previous post I briefly explained closures in Javascript.  Dealing with callbacks in loop is a situation where closures can be put to use. The solution is to create a closure for the tweet function. A pseudo implementation would be thus:
  for (var n=0; n < thingstotweet.length; n++) {
      (function(clsn){
          // tweet function
          Tweet(thingstotweet[clsn], function(e,r) {
              // call back function
             //if no error, then save the id of the tweet
              if (!e) {
                  Db.save(thingstotweet[clsn]);
              }
          });
               
      })(n)
 }

You can check the post on closures in Javascript for more information and why the pseudo code above works.

Use Recursive Function

The other solution is to implement a recursive function.  An implementation would be thus:
 var tweetRecursive = function (n) {
      if (n < thingstotweet.length) {
          // tweet function
           Tweet(thingstotweet[clsn], function(e,r) {
              // the call back function
              //if no error, then save the id of the tweet
              if (!e) {
                 Db.save(thingstotweet[clsn]);
                 tweetRecursive(n + 1);
               }
           });        
       }
  }


  //start the recursive function
  tweetRecursive(0);


The logic to send the tweet is defined as a recursive function.  This works because the function to tweet the next item (the recursive function) is also called in the callback function. This would ensure that there is no out of sync situation and the correct tweet would be saved for the one that was sent.

The only repercussion in this implementation is that it breaks the asynchronous nature as the tweets would be sent in a procedural manner, i.e. one after the other instead of asynchronous fashion.


Saturday, April 06, 2013

Closures are hard? Not really: A simple introduction to closures in Javascript


The concept of closures exists in various programming languages. Javascript is one of such language, and anybody who spends an ample time with it would sooner or later run into situation where using closures is warranted.

But closures are hard? Not really.

So what are closures? From personal experience, giving a detailed pseudo-academic explanation benefits little in really getting the concept; well, at least for me. I realized that even after understanding it, translating that knowledge into practical usage remained a little fuzzy for a while until I had to use it in real projects once or twice after which I sort of formed a personal mental model for it.

So I won’t give a too much detailed explanation. There are tons of posts and articles out there that already did that. I listed some at the end of this post. What I would quickly outline is how I came to understand it to the extent of practical usage.