Taswar Bhatti
The synonyms of software simplicity
scala-ubuntu

First one has to get their java working.

To install java

Once java is installed we will install scala, I like using stow for my package management for non apt-get stuff.

Now that we have scala is installed lets get play framework installed

If you get an error when you launch play that means you havent given the rights to the boot folder, check the commands I have listed above again.

linq

Was going through some legacy code recently and found a big for loop that had multiple switch statement inside. So being that I hate something that is fairly large, every method should be small and concise, I went along to refactor it to SRP.

This code is generic so you might see something like this in your code base also.

As you will notice one can go on and keep adding types to it (so did the legacy code).
But if you think about it what this code is basically doing is that its just filtering things out to seperate collections.

Let see how we can answer things with LINQ on this one

First lets remove the if statement for customer IGNORE.

Ok one step done thats better, now lets see that the loop is basically a filter, so lets just take the payingCustomer example and break it down first. Its is going through a loop and finding the A_TYPE customer. Therefore a linq query like

collection.Where(x => x.Type == A_TYPE).toDictionary(t => t.Key, t.Name);

can answer that one.

So lets create a function/method that does this.

So here we have a single responsibility function, that just does one thing and does it well.
Its more of a functional style of programming (data in and data out).
No side effects of any sort. No saving of data to database or file etc.

Now lets go back to our original code, we can now remove the loop and switch statement all together.

Note: we now have a filterCustomer collection, which filters out the ignore customers first.

By doing so we can write a unit test to test the method/function FilterCustomers and we can be 100% sure that all it does is a single responsibility task.
We now have removed complexity in our codebase and the test-ability goes up.

linq

Here is some poorly written code that I had to review today and modify (i.e refactor). I have removed the domain of the code and renamed the variables, so that it remains anonymous, but the just of the code is still intact.

Lets go through the inner loop of the code first.

First the loop is really unneccesary one can find out how many items there are using the
count of if since it is an IEnumerable

Thus it becomes

Now we go to the outer foreach we can put a Where clause to remove the if statement we don’t really need to check for the “0” condition since 1 + 0 is still 1 so why filter for those.

Finally lets remove the entire foreach loop and the count if statement. Since if we have 0 items this code will still not fail.

Now we have replaced all this code with a linq query that would tell us how many items there are with one line of linq. I believe this makes is easier on the eye and readability of it rather than looping and branching of the code. The more branches you have the harder it is to read code.

dotnet C#

Here is a quick way to detect if a text is in Canadian Aboriginal Syllabic in C#.

It is just an extension method that would detect if your string is in that range by using regex.

Enjoy.

eclipse_indigo

So today I came in to work and saw that my wonderful windows machine was restarted due to Windows Update, and yesterday I was working on some java stuff and did not close out my eclipse, thank god that I do regular code check-in 🙂

Anyways tried to relaunch eclipse and it kept hanging on workspace.

To fix that I had to go into my workspace folder C:\workspace\.metadata\.plugins

Then I copied the org.eclipse.core.resources to a backup directory and deleted it from the plugins folder.

I restarted Eclipse and then did a File->Import->Existing Project into Workspace and browse my workspace/project directory

And voila I was back in action 🙂

techchange

Driving Technical Change Book cover
Have wanted to post the review of this book for a while, which I read somewhere beginning of this year.
This book does a very good job in classification of the “typical” stereotypes of people in IT.
– The Uninformed
– The Herd
– The Cynic
– The Burned
– The Time Crunched
– The Boss
– The Irrational

After the first section we go into what kind of techniques we can use to drive technical changes.
– Gain Expertise
– Deliver Your Message
– Demonstrate Your Technique
– Propose Compromise
– Create Trust
– Get Publicity
– Focus on Synergy
– Build a Bridge
– Create Something Compelling

Lastly the author goes into Strategies one can use to drive technical changes
– Simple, Not Easy
– Ignore the Irrational
– Target the Willing
– Harness the Converted
– Sway Management

The book is a short read with only 125 pages, it is a good read but at the same time one should always remember there are people whom you have to deal with that are emotional, and us human’s are not predictable, the classification helps but if we really want to drive the change we have to be the change agent. I would recommend this book for a beginner who wishes to drive technical changes.

Jruby

Have been giving eventmachine a try but I do have issues with it thus wanted to try out the beta version on github.

Here is how you get it to install on your local machine.
– First clone the repo
– Build the gem
– Install the java version

goliath

What the ???? goliath ERROR: While executing gem.
I was wanting to try out goliath.io (Goliath: Non-blocking, Ruby 1.9 Web Server) for more info look at http://www.igvita.com/2011/03/08/goliath-non-blocking-ruby-19-web-server/

In any case I was not able to get it to install on JRuby so went and get me 1.9.2 MRI.

Once installed MRI 1.9.2, I went into the gem dir to gem install goliath and guess what this message pops up.

Really helpful (scarcasim) , then what I had to do to fix this was update my gem

After doing so I was able to

Now its time to try out goliath, wish me luck and if anyone knows how to install it with Jruby please let me know cuz jgem install goliath didnt work for me.

delegate

Here is a nice little code I did recently, where the Command Pattern is used with a notification observer like pattern.

First of the Command Pattern, a simple interface for task with an execute method

Then the notification interface, with 2 methods, one when it started and one when its complete and the event delegate

Now the class that implements the interface, I used an abstract class so that I can just use a subclass to implement the simple task I wish to have

Now I can write a simple class that inherits from AbstractTask like this

Now to consume the task

This is more or less the simple form of creating a command pattern to run task that includes a notification to send back. It makes sense to have multiple task like creating users and running database scripts etc etc. This allows one to have flexible design and have an IEnumerable<AbstractTask> and run through each one with an execute method.

Hope this helps 🙂 Happy coding.

ASP.NET-MVC

Here is a simple trick on how to disable browser cache in asp mvc application with an attribute.
If you have a base controller just add this to your base and all your request would have Pragma No-Cache

Simply add this to your base controller, and you are done 🙂

UA-4524639-2