Sunday, June 23, 2013

Groovy and its awesomeness on integration tests.

I know its been really a while like a year and half long since i posted something out on this side. I finally decided to stretch my thoughts out a little.

It's been close to 2 years since i came to Kansas city and I have to say I am loving the work i am doing here everyday because the scope of picking a new stuff everyday is fantastic. The biggest hurdle i faced was migrating from Visual studio to Eclipse but everything after that was smooth. I have been messing with Groovy, Ruby, dipping my feet into clojure of late. I actually wanted to start writing about clojure today but still i think i don't have enough experience with it to post a blog on it. So i will stick to Groovy for now.

Alright so what's groovy before i delve into more finer details. let me start off with a comparison here and as to what we're comparing. I am going to compare a method in java to a method in groovy.

public static long add(final long a, final long b) {
  return a+b;
}
Simple enough to add any explanation for this method, lets look at its groovy equivalent.
def add(a,b) {
 a+b;
}
First thing which stands out is the missing return type, second there is no types for the arguments passed and third there is no return statement inside the function, Groovy is a dynamic language and supports lazy evaluation where the compiler doesn't care about the types of the arguments until the runtime also the groovy by default returns the last statement it executes on the method eliminating the need to specify a return type. Let me show some more nifty features. say if i want to create a list and set in java.

List list = new ArrayList();
list.add("A");
list.add("B");
Set set = new HashSet();
set.add("A");
set.add("B");
Well with guava this can be reduced to simply these.
List list = Lists.newArrayList();
list.add("A");
list.add("B");
Set set = Sets.newHashSet();
set.add("A");
set.add("B");
With Groovy it's this simple
def list = ["A","B"]
def set = ["A","B"] as Set
I felt this was much more lucid and simple and in my integration test which is usually a lot of logic for initializing variables, helper classes, wrappers around making the calls the groovy notation reduced the number of lines of code by more than 30%. The best part was the improving the readability of the code because of the advanced workflows covering multiple scenarios. for example lets take this scenario for a method signature in both java and groovy.

public static BigInteger computeAreaOfTriangle(final long side1, 
final long side2, final long side3) {
//TODO logic to compute area using side1, side2, side3
return BigInteger.valueOf(area);
}
when this method is actually called it looks like this.
final BigInteger area = computeAreaOfTriangle(10,20,30);
Although Groovy it supports the concept of named arguments which makes it look like this.
def area = computeAreaOfTriangle(side1:10,side2:20,side3:30);
Although it looks like there is more space to represent in this notation than in the Java way but the readability it provides is much elegant and more easy to troubleshoot as well because we clearly know which argument is mapped to which parameter. That being said Groovy also did have its own pitfalls, one of them being the aspect Groovy gobbled up all the exceptions and it was very difficult to get the stacktrace of an issue as to why its actually failing. I will list this incident which actually drove me a little crazy for 20 minutes to get to the bottom of actually quite a simple issue.

//Once again blogger choking on tags for a map of Strings 
//keyed by longs.
ImmutableMap.of(1,"A",2,"B",3,"C",4,"D",5,"E",6,"F");
I used guava to create an ImmutableMap which is map of strings keyed by long values. If you were to do the step above in java, compiler would mark an Red X and complain that you can't use pass more than 5 arguments (keys and values) to the method of() it will says its an invalid argument and ask you to use the ImmutableMap.Builder for more than 5 arguments. 

 If I were to tell you on maven goals perspective. If you do maven clean verify on that code i have it will give you a compilation error immediately but with Groovy since its dynamic it was compiling the code but when it was doing the test phase it was throwing that error in the runtime and marked the test as failed on the unit test phase which confused the hell out of me until i looked at the guava javadocs for that method. 

On the overall aspect i love using Groovy and i am using that in favor of jBehave frameworks i used for integration testing previously and its making the test more sleeker in my perspective. I am also playing around a lot with Ruby and clojure as well. Hopefully by the next time i post i can make some strides onto that. (It won't be a year and half)

 Until Next time!!

Friday, February 10, 2012

Finally Awake

It's been a long time since i made a posting out here, Well it was the winter and I was hibernating you might say. Well I actually was transitioning a whole lot in these past 6 months or so. I can't believe how quickly time flies. To my surprise in my LinkedIn profile I found that I am in my new job for about 6 months now. 

I have been busy though finally I have mustered enough courage to say I can write unit tests pretty well and most important of all I made the choice to transition from C# to Java. As surprising its for people who might read it here and it was something which i thought about carefully before i chose the move. It really came at a time when i was LINQing my way to glory. 

One of the prime reasons being the team i wanted to be part of when I took the offer. (Also more on the effect of interviewers who interviewed me on that very day). When i got the offer my recruiter told me clearly I would be working on services development for iPad and Android. Oh Yes! So you know the reason of my big switch from all these years of C#. I don't miss it though for I am stackoverflow just about everyday and I keep my tabs on those two. Ironically the options I had to choose from were very difficult and other offer which I gave up was services development on .Net 4.0. Both shared the traits of being major healthcare providers here in US. Well I have a more sense of fulfillment right now that my work would impact a whole lot of people directly. 

So that's the story of how I became a Java developer. Transition from Visual studio to Eclipse wasn't that hard because of grad school experience with Java. But Eclipse is not Visual studio because of its glitches but serves the purpose of being an IDE very clearly. I certainly picked up a bunch of key attributes in this hibernation phase to write nifty little unit tests. I didn't realize how much powerful they were primarily because in all my previous companies they had an extensive testing team to backup the developers for testing but in here every developer has to be their own tester which includes unit tests initially followed by Integration tests which are black box tests.

I have written a demo restful web service (which I would post here next time definitely. I promise :D) which I thought could be very useful for anyone to pick it up easily. For I was writing a bit of those when i started out now I am working on a rather different but exciting story for the moment right now. I finally have sometime on my hands to update my blog which seemed to be getting some attention recently. 

Plus next time would be my recent exploits on Kindle Fire and as fate would've it I ended up with 2 iPads as well. One was given to me at my work for testing out the services. Any takers for my old iPad?

Until Next time!!

PS: Yawns... Finally out of slumber well and truly


Wednesday, October 5, 2011

Duh...iPhone 4s


After the amazing set of releases by Amazon over the course of last week where in I was keeping my fingers crossed for Apple to make its next move. In a way felt let down after the much hyped iPhone 4s announcement despite the fact I knew that there won't be much to expect in that announcement.

It's more than a matter of choice for me currently I am going to jump into the Bandwagon eventually will be ruing this decision here when iPhone 5 is going to come out. As of now I am twitching to order the iPhone 4s because of my lack of a smart device for that matter.

Amazon is the big winner in the week of announcements by throwing in a bunch of marvelous items during the last week press release. Gadget of the month will be iPhone 4s for me.

It's less than 2 weeks for the release of Arkham city and finally D day is heading closer

Wednesday, September 28, 2011

Rubbing the salt

This one is going to stand on a sad note rather than a tech note. First up is the Mango update which started rolling out when my phone broke couple of days before and it's weighing heavily on my mind.

Second one is the announcement by Jeff Bezos. Everyone kinda knew about Kindle Fire the tablet they were about to release but the surprise factor happened to be the new kindle. Oh boy they took me by surprise. I was looking in depth with the functionalities which each of them are having. So as I could get guess from the names. Anyone interested would find the detailed specs when you visit Amazon's home page today. I like the Kindle touch by the looks of it I am waiting to see the hands on video to see how it looks from Engadget.

 From first impressions and an avid fan of Amazon products I am blown by the new kindle more than Fire. Touch based kindle by the price range would be a huge seller in my perspective and a big money making strategy of Amazon because I paid close to 200 bucks on my old Kindle along with the Smart cover and stuff now that I reflect on it I could get 2 Kindle Touch's right now.

 Well now you know what rubbing a salt feels like. I am looking for any takers for my Veronica. Another smart move I admire is their placement before Apple's big announcement. Ball is your court apple let's see what you come up with. Until next week or I until I find more info on Kindle touch. Cheers!

Monday, September 26, 2011

RIIP Macho Phone!

Yesterday was one of the really worst days which I faced in these past 2 months. Just when i was waiting so eagerly to get hooked up to a Mango update. My Windows Phone died on me, apparently due to the failure of the LCD screen. I didn't think I needed the insurance with that phone primarily because of the fact that I never had a problem with any of the phones I had in a long time. I did realize that with ATT it's much better to have an insurance policy even though if the cost of the product is very cheap.

I loved my Windows Phone. These are the contingency plans I came up with, Order a Samsung Focus from Amazon.com and weather the storm. Something cool crossed my mind which would work out perfectly if it goes well. I wanted to get an iPhone 4 yesterday because I didn't want to be deprived off a smart phone but i realized I could get something better which would be the iPhone 5 which is due to come out soon. Well that would definitely make my phone rather distinctive. I consider this as the heavenly sign primarily because my love for apple products has increased exponentially after the usage of my iPad.

I hope to repair my Windows Phone and solely use it as my crash device for testing my applications. All in all I was able to avert the major damage which I would've faced yesterday.

On a side note. After all the travel in the past 2 months I have finally settled down and I am really raring to start with my new team in my company. Apparently I will be getting to play with a lot of smart devices. I simply can't wait. 

I got a request via email to reveal about the naming conventions I used for my gadgets. Well I have this obsession of naming my device after a girl (based on literary references and some of my favorite names) . My iPods were Megan & Cilla. My Kindle is Veronica. My 360 is an Agnes. I still haven't christened my iPad cause I am finding to choose between 2 very beautiful names. By the looks of it I should create a list of new names.  I should probably post it on my Personal blog too but I think this blog makes it a suitable avenue to divulge it.

As for things I am looking forward too, Arkham city is hardly 3 weeks away i can't wait to get my hands on the Collector's Edition

More next time...


Wednesday, August 3, 2011

Food for thought

I had this question on an interview where I was asked to solve this problem on C#. Reverse a string :) well my eyes were gleaming when i heard the question, best part being asking me to do it in all possible ways I could think off to answer it immediately. I was happy with the number of answers I could do it in that short tenure of time but  I realized how knowing a framework in detail could streamline the thought process very quickly. I am going to list my answer in this post so that someone might bump on it on the rare instance and find it useful.
class Reverse
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Input String:");
            string stringToBeReversed = Console.ReadLine();
            string reversedString = ReverseTheString(stringToBeReversed);
            Console.WriteLine("Reversed String is: " + reversedString);
            Console.ReadLine();
        }
 
        private static string ReverseTheString(string stringToBeReversed)
        {
            //Nifty one ain't it?
   char[] chArray= stringToBeReversed.ToCharArray();
   Array.Reverse(chArray);
   return new String(chArray);
  
        }
   
   private static string ReverseTheString1(string stringToBeReversed)
        {
            // interviewers love one liners don't they?? :)
   
   return(new String(stringToBeReversed.Reverse().ToArray()));
  
        }
   
   private static string ReverseTheString2(string stringToBeReversed)
   {
            //Old fashioned way how could i ever miss the one with good'ol for loop
   string sOutput = String.Empty;
   char[] chArray = stringToBeReversed.ToCharArray();
   
   for (int i = chArray.Length - 1; i > -1; i--)
   {
   sOutput += chArray[i];
   } 
   return sOutput;
   }
  
  private static string ReverseTheString3(string stringToBeReversed)
        {
            //Old fashioned way with string Builder upgrade
            StringBuilder sOutput = new StringBuilder();
            char[] chArray = stringToBeReversed.ToCharArray();

            for (int i = chArray.Length - 1; i > -1; i--)
            {
                sOutput.Append(chArray[i]);
            }
            return sOutput.ToString(); ;

        }

  
  private static string ReverseTheString4(string stringToBeReversed)
   {
         //one with recursion
   if (stringToBeReversed.Length == 1)
   return stringToBeReversed;
   else
   return stringToBeReversed[stringToBeReversed.Length - 1] + ReverseTheString4(stringToBeReversed.Substring(0, stringToBeReversed.Length - 1));
  }
  
  private static string ReverseTheString5(string stringToBeReversed)
        {
            //one with LINQ :)
            var reversedValue = stringToBeReversed.ToCharArray()
                         .Select(ch => ch.ToString())
                         .Aggregate<string>((xs, x) => x + xs);

            return reversedValue.ToString();
        }
  
   
    }

It's a real simple piece of code I posted it here since I felt the urge to write something since it's been a while I posted something on here. I am currently working on a top secret venture currently and once I form a working prototype I would definitely post it here. Hope someone finds my little post amusing.

Until Next time!!

Sunday, July 17, 2011

Epic Fail

I wanted to install Mango Tools SDK onto my laptop to start tweaking a new app I had been prototyping for quite some time now on Windows Phone 7, alas there was one major hindrance. My laptop was running Windows Vista Home Premium and for mango be installed needed a SP1. I let my windows update to download the SP1 and tried installing it which happened to be the beginning of the end.

SP1 corrupted my HDD with an error message and I was lucky to note it in the middle of the night when I saw the light on my laptop persisting than normal. I force reset it and boom windows wouldn't started. it was of no concern to me since I had the same situation umpteen times. I used safe mode now I got the prompts for the drivers being loaded and I was watching the sequence eagerly when i got the message CRC DLLs cannot be loaded then I realized what the service pack installation had gotten me into.

I realized I had no way to boot again into my current windows version and finally decided to reset my lappie to factory settings. I had my external HDD handy and manually copied all files to my Backup using DOS copy  commands. Huh took me the entire Saturday night to make a backup and I regret missing my Dev Environment currently. 

Moral of the Story: Its better to upgrade to Windows 7 rather than installing Windows Vista SP1