Why I chose Node.js for my hobby project?

If you know me, you can easily say I’m Java addict. I prefer to use Java wherever possible. But things change. JS MVC frameworks becomes hipster framework and I’ve already took a deep dive in AngularJS. After worked in AngularJS for past 5 months, I’m completely impressed with what they can do. Leaving all the bad things aside, it is a perfect RAD framework. If not for AngularJS, we wouldn’t have finished our project on time!

So it all started like that! So one day, my long term project, which is supposed to be my personal pet project, was started with Spring MVC + MySQL! It was left as it is because of me, not having enough time to spend it, and I’m incredibly lazy to write all those boilerplate codes! Anyways, fast forward to Feb, I decided to dust it off and start again. Since I’ve been studying/practicing Nodejs for more than a year, I thought I can give it a try. Well, whats wrong gonna happen and there is nothing to lose! So, meh!

It was nothing but simple yeoman like code generator for AngularJS!!(Don’t ask me why I want to do when it’s already there! It’s secret.) So I chose to go with Expressjs for it’s sheer simplicity in setting up and huge community support, better documentation. It was not more than a week and I’m almost at 50%! And I’m not even spending more than a hour a day! It was little tough at beginning, but once dice started rolling, things went smooth! I liked how the Nodejs community matured within such short time and npm is such a bad-ass! I’m not an pro in Nodejs and calling it THE BEST at this point will be over-doing it, but for now, it seems to best bet for all my pet projects, freelancing ones too! So the moment I put Angularjs with Nodejs, things never was this simpler.

But not all is well. Where it sucks is, when it comes to hosting the app.With PHP/Java, I will setup the server and get app running within an hour, but that’s not the case with Nodejs. It is just not noob-friendly. But once you understand the nuances and terminology, it just hits you right in the face.

So you might ask, what is this post about? It is about, things I liked/dis-liked in one the most touted buzz-word framework Node.js. So here for you!

Likes

  1. Ease of use and setup! Server configuration in local, just 2 secs! Yes, that easy.
  2. Huge community support. I mean huge. And Node.js have LTS now, which is a big relief when devs going in for critical applications where support is crucials.
  3. NPM! Enuff said. Nothing can beat this, not even close.
  4. Awesome plugins. Almost anything, you can find. Never have I felt so relaxed in my 7 years of developer life. Before doing something, I just check npm to see if a plugin already exists and so far it is the case!
  5. Simple, modular code structure.
  6. Very very very very less, learning curve.

Dis-likes

  1. Few plugins I want to use, is either not maintained properly or author simply left it there hanging. I can see many such one, with lot of PRs hanging around. I mean, I’m little concerned about using it. What if something breaks after some time, who I’m gonna ask for help?
  2. Infrastructure is big PITA! This thing is harder than setting up J2EE Server! This coming from Java guy. I mean, yes, heroku is great, but not as great as I thought. If a noob cant configure it, then it is indeed not great. It needs proper, setup guide for noob like us.
  3. Callback! Yeah I know it sounds dumb, because I know there was callback, but this is serious. For first few weeks, it’s fine. No, it’s great. But when code base is expanding, things get real ugly. But there are ways but still, I wish Node.js could have come up with some slick one instead of fat one!(Don’t tell me promise!)

So, it’s not bad at all as people claim. If planned and written properly, with some research on whether it suits you, this can be the best RAD framework, be it your pet project or anything.

 

Is AngularJS(1.x) ready for Enterprise Application Development?

Before I start, I know that we have tons and tons of posts on this topic, but this was based purely on my experience.

It all started with our new project kick start phase, where AngularJS was suggested out of blue to speed up the development due to the tight deadlines. I’m a J2EE guy, and I’m a not a big fan of JS Framework, but ever since words like Backbone.js, Node.js got around the web, it always fascinated me. Whenever a new one comes in the market, I will be start exploring the tech and get excited for couple of months, before the mainstream development get me involved! But this move, looked like I finally get a chance to use cutting edge, buzz word technology in a project at my workplace. Yay! This should be fun.

So the first few weeks has been excited, and I’m very eager to get the prototype up and running to showcase the team, that AngularJS is the right candidate for this project. The base code which we created for the project was so smooth, we felt that we can REALLY speed up the development, because it was so easy, where we spend 100s of lines of code, AngularJS did it in much fewer and simple way! But that excitement didn’t stay long with me.

Before I get any further, this is not another AngularJS Bashing post, but rather a post to clear the mythical air that surround the front-end JS MVC framework. Let me get into the topic.

  1. Documentation : Most part of the development time was spent trying to understand how certain things worked. Because when I try to find them in official documentation, it isn’t there. For a guy like me, who read official documentation for all my doubts in a framework, this was a big blow. The documentation to getting started with was good, but once you are past that honeymoon period, it will be a tough time to find and understand certain aspects. I strongly feel official documentation should be the main place to dig anything and find solution, or learn a framework. Not in some forums. Infact, SFO helped me a lot with AngularJS than the official documentation!
  2. Learning Curve : One funny thing I noticed about AngularJS was, as much easier as it is to learn the basic, it will get tougher when you get going. I’ve done some learning curve graph for easy understanding.ang-learn-curveIt was exactly what happened. Initial excitement levels gave confidence that we can achieve anything but as we are into middle of the project, things get tough and it becomes a swirl-wind about to destroy the city! But after a lot of digging, it get’s down but after certain point of time, it feels like a landmine hidden somewhere safe, but we don’t know where it is. It is mainly attributed to my first point about Documentation. If it was any easier, it could have saved us a lot of time!
  3. Performance : Hey, I am not complaining, but it isn’t what we believed. Since it is an JS framework, things get uglier if we can’t look under the hood(code!). It’s a like a kid left unnoticed with dangerous toys! Hello world can be snappy but when the code base gets big, headache gets bigger! Of course, it is a developer’s responsibility to make sure to tune up the performance, but kind of added more work for us. As a J2EE guy, we were more into server side tweaking and even after bringing skinny data to client side, it took ages to render them! Why, because bloody watchers infested all over the page. Then it hit us, that now we are not only going to tweak server, but the front end tools too! It took us a while to figure out, but in the end we figured out stuff and moved on.
  4. Browser Rendering Engine : Before this, all we cared about was not how our app will perform in different browsers, because all the stuff will happen at back-end. But now, we need to be cautious about this. It adds one more layer of work. In fact, plain Vanilla AngularJS performed just fine, but with all added plugins and modules, it is pretty tough to make it work across all browsers. It is mainly attributed to the fact that we are newbie at JS framework, but still we never thought of this, because we never had this issue before. Although this might sound silly for many, but when it comes to Enterprise Development, things aren’t as glamorous as it’s with other industry. Client browser restrictions, extreme pressure, unnatural deadlines were just a recipe for stress, and this will put more pressure to already squeezed developers!

Conclusion :

All the points I’ve mentioned are just a few to go with, but in the end, instead of speeding up the development, it did slowed us done after the initial surge. Th main reasons were

  1. Lack of proper R&D. If it’s new and shiny, just pick it up mentality was the big mistake. It would’ve saved us hell a lot of time.
  2. Poor documentation should’ve have been taken into consideration.
  3. People’s general belief. If it’s front-end JS framework, work should be done fast. This is what made some guys go with this new techs, but in the end, without proper understanding about the underlying technology, it is quite opposite.

And there are lot more to say, but I feel you get what I am about to say. If you want to use something, take your sweet time to understand the in and out of the framework before you start churning 100s of lines of code. And if you are seriously tight on deadline, please don’t go with this new shiny thing. Because it can backfire you, if you don’t have the skill and the person.

Finally, after looking at AngularJS 2.0, I am quite confident that it can solve a lot of issues of v1 and the documentation looks a lot better. It sounds quite exciting(see, again I fell for this!) and I hope it will help a lot in enterprise application development.

Thanks for reading!

 

Why hate Java/C/Python/$language_name

For a long time, It’s been a lazy trend to yell at Java or C or Python or what ever languages. But lately it’s becoming mainstream. Of course comments in those post’s made my day, but deep down I always wonder why people do that. It’s like they compare onion with f’ing banana. They both meant for different purpose. Eating an onion and saying it is not sweet is totally retarded. And same applies when someone compare Language A v B.

We are programmers. We do programs. No matter whatever language it is. If you look at big names technology stack you may understand this. They try to use proper tools for jobs and as long as it is working fine it is all it matters. Instead of sitting at your desk and wimp like kid, and telling some you hate X so you wouldn’t code sounds real bad. And what’s worse than that is, blogging about it. Apart from those funny comments at those posts, those articles never had any purpose in my view.

Why hate X? If you don’t know the true potential of X, where to use and how to use, it’s your mistake.Not X’s. Some idiot did some unoptimized pathetic excuse of code and made a mess out of it and it is not the language’s mistake. Instead of bitching about it, start analyze and fix the problem. Not make fun or yell at X.

After looking at guys who implement plethora of technology in their stack, I started following their footpath and it worked well so far. I love Java, but never hesitated to learn something new. As a result of this, now I know a handful of languages which I use together depends on situation to get the best result.

I can hear you all saying “What the hell he is trying to say? “. Well, all I am saying is, try to learn as many as possible, implement them based on needs and stop complaining about an onion not being like an banana.

HashMap in Javascript

I was searching for a way to play around with associative arrays in Javascript just like HashMap. And i found this useful link http://www.mojavelinux.com/articles/javascript_hashes.html, which exactly what i was looking for. So i thought of sharing the content with other users thought someone need it as i am. All the credits goes to the mojavelinux and i am simply replicating the content here.

Introduction

Hash tables are a permutation of associative arrays (i.e. name => value pairs). If you use PHP, then you are very familiar with this type of data structure already since all PHP arrays are associative.

The Javascript language implements very loose and somewhat limited support for associative arrays. Any JavaScript array can use other objects as keys, making it a hash, but there exists no formal constructor for initializing them and it is more or less unweildy to work with. A short example of a hash in JavaScript would be as follows:

var myArray = new Array();
myArray['one'] = 1;
myArray['two'] = 2;
myArray['three'] = 3;

// show the values stored
for (var i in myArray) {
	alert('key is: ' + i + ', value is: ' + myArray[i]);
}

Just as in PHP, the ‘foreach’ contruct is used to run through the array, doing something for each key => value pair. However, notice I did not do:

for (var i = 0; i < myArray.length; i++) {
	alert('key is: ' + i + ', value is: ' + myArray[i]);
}

This approach would not work in Javascript because the length property of an associative array in JavaScript is not incremented for arrays with non-numeric keys and must otherwise be explicitly assigned. Part of the reason for this goes back to the missing constructor for created associative arrays.

Fundamentals

After a brief discussion of fundamentals we will begin to focus on the core problem. In JavaScript, every variable is in fact an object. Okay, so what does this mean? Well, essentially, no matter what the variable, it can be used as though it were an instance of an object. This means it has a constructor, methods and properties. A property is just a variable that is owned by the object and thus local to that object. A property is accessed using the syntax:

myArray.one

where one is the property and the ‘.’ symbol signifies we are talking about the property of the array (or object) myArray. So the above example could be alternatively executed as:

for (var i in myArray) {
	alert('key is: ' + i + ', value is: ' + eval('myArray.' + i));
}

NOTE: In this example we have to use the function ‘eval()’ because we want to contruct the call myArray.one dynamically.

Since each object has default properties that are accessed using this very same syntax, such as length and constructor, consider the case where the key in the associative array is the same as one of these properties. This situation highlights the fundamental problem with associative arrays in JavaScript. It should be clear now why the length property is not set when we make an associative array data structure. By creating an associative array, you wipe out the original structure of the object because it is no longer possible differentiate between data keys and object properties. They become one in the same.

As a matter of fact, this is not the only conflict. Each object has a property called contructor, which is the function reference which is used to contruct the object. Ahhh, so now we see why hashes in JavaScript have no contructor. By creating an associative array, it is possible to wipe out the constructor in the process. In fact, hashes in JavaScript are somewhat useless for anything more than a very simple, static array. You have to know what it will be used for before you use it. Okay, so enough run on, what are we going to do about it?

Constructing a Hash Class

Javascript is very nice language in the sense that we can create our own classes. So what we are going to do is create a Hash() class just like the Array() class, except we are going to get around the conflicts that we are running into with this Array() class.

You might thinking, “okay, so we make a class, but how do we get around the conflicting properties problem?”. Easy, we make a property which itself is an array and call it items. Then, we can use any key we want, and store the data about the array in other properties. The trick is to move the data part of the array inside of a property of the class. The following listing is the Hash() object definition:

function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
}

NOTE: You should select another name for this class if you are using the prototype JavaScript library to avoid a naming conflict.

Let’s break this down a bit. Right off the bat, we create a length property, which will just be 0 to start with. Additionally, we create our items array using the Array() contructor. Next we populate that array with the key => value pairs we passed in and continue to increment the length. Ah, but JavaScript doesn’t know anything about the special syntax that say PHP uses. So we have to invent our own. What we will do is just alternate key and value arguments to the contructor (similar to Perl). A typical call to create a Hash() object would use the following syntax:

var myHash = new Hash('one', 1, 'two', 2, 'three', 3);

Already it must be nice to see a contructor…so much easier to add data to the structure!

Now, as you may recall before, we couldn’t have any properties or methods in our associative array, so besides a ‘foreach’ construct, there was not much we could do with our associative array. Now that we have the ability to add methods and properties, let’s get started! So we enhance our Hash() class.

function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}

	this.removeItem = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}

		return tmp_previous;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			else {
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}

		return tmp_previous;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}

	this.clear = function()
	{
		for (var i in this.items) {
			delete this.items[i];
		}

		this.length = 0;
	}
}

Understanding the Implementation

We now have lots of useful methods! In JavaScript, any variable can be a reference to a function, so to add methods to the class, the easiest way to do it is to just write the function and then assign it to a property in the class. Okay, so you may be thinking, “But I can’t have the same property name as a method name.” That’s right, another limitation of JavaScript objects is that methods are properties. However, in most cases, it won’t be a problem because method names should be ‘behavior’ names and properties should be ‘state’ names.

In order to access the underlying items, we added the methods ‘setItem’, ‘removeItem’ and ‘hasItem’, and a ‘clear’ method to flush out the data. For now we will refer to each key => value pair as an item. We could create a ‘getItem’ method as well, but that is a bit slower and it doesn’t server any real useful purpose. If you would like, add it for your own class…either way works. Note that if you are using the prototype JavaScript library, the ‘hasItem’ method will return values for any of the methods that prototype adds to Array. You will also need to ensure the item found is not a function.

The most important role of our methods is to keep the length property up to date. As we can see, it takes a lot of work out of our job and we can use these nice methods to work easily with our hash. Just like a Hash in Java, the return value is a reference to the item in the Hash() that was replaced:

alert("Previous value: " + myHash.setItem('foobar', 'hey'));

If you now want to iterate through the Hash() like we did the array in the very beginning, you may do so using two different approaches:

for (var i in myHash.items) {
	alert('key is: ' + i + ', value is: ' + myHash.items[i]);
}

or

for (var i = 0; i < myHash.length; i++) {
	alert('key is: ' + i + ', value is: ' + myHash.items[i]);
}

and if you made a method for ‘getItem()’, you could also do:

for (var i = 0; i < myHash.length; i++) {
	alert('key is: ' + i + ', value is: ' + myHash.getItem(i));
}

If you are not concerned with speed down to the millisecond, then it is better to use the more formal approach of making a ‘getItem()’ method because it helps to prevent exposing the internal Array items.

Summary

Now you should go home and start using this in every JavaScript code you write because it finally makes Arrays in JavaScript useful. It is great for storing configuration data, returning multiple values from function, and the list goes on.

Now, I added a few more properties in some of my JavaScript programs to get more information out of the Hash() and so can you. In one application, I wanted to know how many integer keys the Hash() had rather than just the length of the whole Hash() and I wanted to be able to work with just the integer keys in one instance. You can do this by modifing the contrutor and methods to check for the type of the key using the function ‘typeof()’ and then setting the indexLength (as I called it) appropriately.