Ubuntu insights, Programming in groovy, java, et als!

Monday, September 19, 2011

Serializing objects in Groovy

Never came across a more sexier way to serialize objects. Thanks to groovy goodness.


def map1 = ['1':'one']
def map2 = ['2':'two']

def file = new File("C:/serializedObjects.txt")

file.withObjectOutputStream { out ->
    out << map1
    out << map2          
}
                     
file.withObjectInputStream(getClass().classLoader){ ois ->
    ois.eachObject{
        println it                  
    }
}      

Monday, August 22, 2011

Retrieve the RSS Feed URL of a Webpage

/* A dirty code tweak that can search the meta tags
* of a webpage for its rss feed url.
* If a feed exists for the page,
* the rss feed link portion of the page source
* will be extracted using regex.
*/

def getRSSFeedURLFor(String urlStr){
def url = urlStr.toURL()
def result = []
def str
def lookForStart = '''<link rel="alternate"'''
def lookForEnd = '''>\n'''

url.eachLine {
if(it.contains("RSS"))
result << it + "\n"
}

result.each{
if(it.contains(lookForStart)) {
strtIdx = it.indexOf(lookForStart)
lastIdx = (it.indexOf(lookForEnd)+2)
str = it.substring(strtIdx,lastIdx)
}
}

str = str.replaceAll(/(.*)(href.+)"(.*)"(.*)\n/,'$3')
str = str.trim()
return str
}

def urlStr = "http://www.ashes-phoenix.blogspot.com"
def rssURL = getRSSFeedURLFor(urlStr).toURL()
println rssURL

PS: For most of the standard websites, if an RSS feed exists for the page, it will be specified in one of the metatags of the page source that looks like:

<link rel = "alternate" type=atom/application/rss+xml href=''...>

The logic lies in extracting the link within the href attribute of this tag which is actually the rss feed of the page we are trying to retrieve


Friday, August 19, 2011

Build a Binary Tree & Perform an Inorder traversal



/*
* A simple groovy implementation to create a Binary Tree
* and to perform inorder traversal
*/

class Node{

    Node left, right
    int data
   
    Node(data){
        this.data = data
        left = null
        right = null
    }
}


class BTree{

    static def insert(Node node, val){
      if(val < node.data){
          if(node.left == null){
              node.left = new Node(val)
              println "inserting $val to the left of $node.data"
          }
         
          else insert(node.left, val)
      }
     
      else if(val > node.data){
          if(node.right == null){
              node.right = new Node(val)
              println "inserting $val to the right of $node.data"
          }
         
          else insert(node.right, val)
      }
     
    }
   
    //left, root, right
    static def printInOrder(Node node){
        if(node == null) return
       
        else{
            printInOrder(node.left)
            println node.data
            printInOrder(node.right)
        }
    }
}

//Shift to main if you aren't running this as a groovy script
def root = new Node(25)
BTree.insert(root, 10)
BTree.insert(root, 30)
BTree.insert(root, 24)
BTree.insert(root, 299)
BTree.insert(root, 266)
BTree.insert(root, 121)
BTree.insert(root, 920)

//Inorder traversal of the entered elements
BTree.printInOrder(root)


Friday, July 08, 2011

Latest Trends in Google - July 2011

Off late Google has been adding so many features into its products with so much of pace like its ass is on fire. And probably this is good news for users. Firstly, the launch of +1 button took place, which was pretty much accepted by users as it could enhance the quality of keyword based web search. Although I feel that there are two serious flaws in its design. Firstly, the authenticity of +1 could not be trusted to 100%. I'd just hit a +1 button to my blog that shows up on a keyword search simply because it is MY blog. Or I could simply program a bot for this if possible. Secondly, the +1 shows up on keyword search. How can a user judge the quality of a website shown up before visiting it. Assuming that one finds the site relevant and accurate, why would one ever come back to the search page to hit the +1 button. These issues, I believe, though not showstopper flaws, certainly needs intervention.

And then comes the much hyped Google plus, perhaps which is still yet to take off. As of now, people believe that it is just old wine in new bottle. Nevertheless, it is still possible that g+ might surpass Facebook one day because of its renewed older features of social networking along with a few new features.

Google has also modified its user interface for the sidebar of Google search. It now comes with readable, glassy gray and brown fonts. On the similar lines Google also worked out a clutter free theme for Gmail named 'preview' which I believe, is so far the best theme for gmail ever. It has this elegant wider inbox list which can now be filtered to be read based on 'Important first', 'Unread first', et al.

Adding to this, Google has confirmed that it would lay emphasis on changing the look and feel of its products for a better user experience. And today, we also witness a change in UI for blogger. Login to http://draft.blogger.com with your blogspot id to check out the new interface for blogger.

Look and feel apart, Google is yet to officially rename its well known two products Picasa and Blogger. Picasa would now be renamed to Google photos and Blogger would now be renamed to Google blogs. This is Google's attempt to allow complete integration of these products into its latest revelation, Google plus. This might proabably become one of the major 'thumbs-up' for Google plus over Facebook.

Google is in a hurry now to prove its presence in everything and I guess it might just be able to prove this point to the world that it might eventually become omnipotent, omniscient and all pervading, probably a digital god. As of now all we can say is, It's cooking in Google's Kitchen.

Wednesday, July 06, 2011

Programming Practices for the Corporate Developer


'A mediocre level in coding and proficiency in a programming language would suffice to develop an application'

Well, that was my mindset just post completion of my under graduation. Fortunately, it was blatantly proven wrong soon after I set foot into the corporate world. Technically, knowledge in programming is all one needs to develop a software application but in the corporate world, I've learned lately that the style of programming adapted is of more importance than that of programming itself. Here are few programming practices that are expected to be adapted by a software developer in the corporate world.

Design patterns

Perhaps the most important of all the programming practices is to strictly adhere to the standard design patterns available. We often come across such scenarios in the world of Object Oriented Programming where in multiple instance creation is a necessary operation yet it tremendously slows down the performance of the application. This is where design patterns come to rescue. For ex: Adapting a singleton design pattern would restrict the deliberate creation of unnecessary instances of an object. On the other hand adapting a design pattern like factory would efficiently handle multiple instances of an object that are too many yet necessary, so that selecting the appropriate instance of the same from the factory would be a lot easier than manually keeping track of the multiple instances. A programmer can always create his/her own design patterns too, adhering to which the re-usability can be exploited to the maximum enabling better performance of the application developed.

Naming Conventions

Naming conventions when considered petty or trivial can certainly lead to headaches while developing an application especially when there is scope for extensibility. A programmer must always be sensible enough to follow naming conventions, after all, he/she is not the only one dealing with the same piece of code in the corporate world. For example: Take a look at the creation of a java swing button with the name string 'Okay'.

JButton b = new JButton("Okay") //You suck buddy!!
JButton button = new JButton("Okay") //Duh!!! WTF ???
JButton okayButton = new JButton("Okay") // Hmm. Better!!

Hope it is clear from the above example! Never ever follow senseless or vague naming conventions. Nevertheless, a good piece of code will always be understood by any other programmer where as a bad code will never be.

Constant Refactoring

An application when developed from scratch starts off with a minimal set of classes and packages and it often grows so huge that it would be so damn difficult to manage the growing size and added functionality. So a programmer must make sure that the piece of code written must be made reusable to the maximum extent possible so that it can be further used again. Splitting up functionality into miniscule atomic functions specific to need would help a tonne. Further growing number of functions can again be refactored to different classes when one particular class gets cluttered with too many functions. And one level above, growing number of classes can further be split up under packages. Such constant refactoring code would make it easy for a programmer to rope in additional functional components and also the refactored code could be reused as well.

Ease of the programming vs performance of the application

Software applications when developed on dynamic languages are often susceptible to performance issues when the programming is deliberately done ineffective by a lazy programmer. For example: High end programming languages support dynamic binding that also adds to the ease of coding for a programmer. In one such instance, a programmer always has a choice to deliberately ignore mentioning the return type of a function so that it could be determined at run time later on. Such lazy programming practices can prove costly and lead to performance issues on a large scale considering the method's overall implementation. So a programmer must be intelligent enough to exploit the power of programming but never at the cost of the application's performance.

Considering that my experience in the IT industry is too less to talk more about better programming practices, the above are few things that the corporate world had taught me in the last couple of months. Hope that these few would help peers and the readers of this blog to code better in the corporate world.

Friday, July 01, 2011

What's new in Google Plus?


Apparently the most searched keyword for this week on Google has undoubtedly been 'Google plus'. Every one is eager to know if Google's going for a head on collision with Facebook, the social networking giant head of the recent past. Based on my experience, here's my take on Google plus, so far.

1) Well, If you ask me, what do I love the most about Google plus, I would surely say that it is its clutter free user interface. White background, glassy finish, the most readable Sans serif font certainly adds to its elegance. As of now, the UI looks neat without advertisements or stupid suggestions (as in facebook) showing up.

2) Spam messages on chat, improper offline visibility et al. Some reasons why we hate facebook chat. We never had any issues with Google talk and henceforth the Gtalk chat element would remain intact within Google plus.

3) Circles is more apt to conceptualize in terms of social networking when compared to lists in facebook and twitter. Editing lists on facebook can be a pain in the ass where as Google plus made it an all easy drag and drop.

4) Google Buzz was initially much hyped because of its basic feature that one doesn't have to go searching for friends, as in Facebook or Twitter. Google plus exploits the same feature as well.

5) Sparks on Google plus, IMHO is an insanely innovative concept. It is a great thought to include such a feature that would keep you posted with news feeds related to topics of your interest. Although I am not sure of the quality and accuracy of the feeds in sparks. Either ways this is one aspect where in Google plus surpasses Twitter that provides real time feeds. How would one ever know which photographer to follow on twitter if he/she is interested in photography? This feature reminds me of stumbleupon, Nevertheless it is awesome.

6) Facebook curbs freedom giving way too much importance to privacy. Twitter on the other hand gives too much of freedom for a user. You've to be friends with a person on Facebook to view his/her feeds. On the contrast, everybody on Twitter is given the right to view anyone's tweets and follow 'em. Google plus is a perfect blend of both. It doesn't compromise freedom of use for privacy. Any user is free to add up to his/her circle, any person of his choice (as in twitter) but is again enforced with a restriction of not being able to access all the updates of the user. One can always set the visibility of an update or a shared item to public (viewed by all users who follow you) or it can also be set to be viewed by one/few particular circle of yours. This is again a simple yet brilliant thought of Google to combine the best of twitter and Facebook leaving apart their shortcomings.

7) Adding to this, tiny yet noticeable features of easy editing of updates and comments, mobile app availability, total sync from picasa web albums and easy pop ups to include friends in a circle, all of these add up to what Google plus could provide better than the rest of the social networking sites.

Hopefully, in the near future we could also expect Google to expose the API for the same. Comprehending the fact that Google plus is still in its first phase of release, it is still yet to take off. It is not a very serious threat to Facebook or Twitter as of now but we surely cannot deny the fact that it has the potential to leave a trace on the sands of social networking, After all it is Google at work!

Wednesday, June 01, 2011

Installing Tweetdeck on Ubuntu 11.04

Now, I must gladly admit that I've fallen in love with Natty Narwhal. With Ubuntu, It has always been a love-hate relationship. I lost track of the number of times I've installed and uninstalled Ubuntu for the fact that it has always been a pain to cope with the messy linux commands to get things running. But with Natty Narwhal, things seem to be different. The unity launch pad is one unique thing about the Natty release, enabling ease of use to some extent for Linux illiterates like me. Trust me, you would be startled to know how easily I've installed Tweetdeck on Ubuntu 11.04 without the need to run any conventional $sudo apt-get install commands from the terminal.

Installing Adobe AIR

Unlike the former versions of Ubuntu you don't have to download the debian/rpm packages and run terminal commands to install AIR, Rather you will find this cute icon on the unity interface named 'Applications'. You can click on it and search for Adobe AIR in the search box, the 'Adobe Air' option would show up under the 'Available Apps To Download' column. And hereupon you will be redirected to the Ubuntu-Software-Center which is typically a simple GUI to help the user with the installation process.

PS: I wonder if the latest Adobe AIR for Linux is totally compatible with Ubuntu 11.04 64 bit version because I've found some glitches while trying to install AIR for a 64 bit version. That which I am not sure though. You might want to recheck the process if you are using a 64 bit version. Nevertheless it worked perfectly fine for the 32 bit version.


Installing Tweetdeck

After you are sure that AIR is installed correctly (cross check in the installed software menu items list within the Ubuntu Software Center), you can download Tweetdeck which typically comes in with an .air extension. All you need to do now is to try and open the tweetdeck.air installer with Adobe air. By default Ubuntu would open up the .air file as a package and show up the contents within it. To change this you can right click on the file and select properties, change the 'Opens with' option to Ubuntu-Software-Center. Now close the window and double click the tweetdeck.air file again which will now open up with the Ubuntu-Software-Center which will automatically open up the downloaded .air file with Adobe AIR and install it.

And that's it. You are done with installing Tweetdeck on your Ubuntu 11.04 without the need to touch upon the pain-in-the-ass sudo apt-get install commands from the terminal. :)