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

Monday, January 23, 2012

Find Source code Line Numbers of Methods at Runtime

Comparing a source file and a runtime equivalent object of the class, here is a ten liner groovy script that determines the line numbers of every method inside a class. A plausible approach to do this in java is is to throw a stacktrace explicitly wherever needed and examine it to determine the line number at the point of execution. This I find is too clumsy a way. One can also take an other approach by parsing the source code file, creating a tree node structure and manipulate it but then the problem with this approach is that one has to reinvent the whole wheel again.

Groovy's dynamism comes to rescue here exposing the runtime object to the programmer during execution. At runtime, one can inspect an object, study its methods and properties and manipulate them as per the need. This can also be done in java using the Reflection API but I find it too monstrous and verbose. I resort to groovy instead. Here's the ten liner script for the same.

//supply the source file path, the only input to the script
def sourceFilePath = 'C:\\Temp\\GroovyUtils\\Sample.groovy'

//a map that stores method names vs line numbers as key val pairs
def methodToLineMap = [:]

//find all the methods of the Sample class and assign default line number as 0 first.
def listOfMethods = new Sample().metaClass.methods*
listOfMethods .name.sort().unique().each{

//parse src file to find the line numbers of methods & store line number in the methodMap
def idx = 0
new File(sourceFilePath).eachLine{ line ->
    methodToLineMap.keySet().each{method ->
            methodToLineMap[method] = idx

//print the elements of method vs line number map
        println it

PS : You can also supply a java source code file (in sourceFilePath) to find out the line numbers of the java class's methods but ensure that the .class file is on the classpath while running the script.

Tuesday, January 17, 2012

Windows 8 - (Dev Build) Review So Far

I just experienced Windows 8, a leaked version from the dev build beta archives, on my friend's laptop. If there is one adjective to describe it, I'd go for the word "different". I personally felt that the tablet-ish aspect of its UI certainly looks elegant with all the native MS-zune like Metro-style interface but I am afraid that it would certainly compromise the desktop experience. A scroll though between panels is obviously easier on a tablet because of the touch. On the contrary I find it a pain in the butt to move my mouse to the tiny scroll bar below to navigate between the home panels. The tablet targeted applications provided by default are brilliant, like the RSS/News feed reader, Picstream, Paintplay, etc.
Users are likely to face hassles until they get used to it. First and foremost of all, as you login to the system you are not directed to a desktop screen, instead you are directed onto a tablet like home screen. 
To see the Windows 7 like desktop, you need to select the desktop launcher from the home screen. When an application or external software is installed, instead of the shortcut being dropped onto the desktop, a similar such launch icon will be dropped onto the panels. Of course you can manually drop the shortcut onto the desktop too. Secondly, there is not even a slightest trace of the conventional "All Programs" type start menu within the desktop. So if you heavily rely on the Start menu - mouse click way of launching your applications, well, you might hate Windows 8. But good news for the keyboard freaks who rarely use the mouse to launch apps. Windows 7 start menu search is extended further to make it look the unity panel of Ubuntu 11.04 and above. So applications can be launched easily by hitting the windows key and typing in the name of the app to be launched.

Some other changes in the UI include addition of an abnormally huge toolbar (as in MS office apps) in the FIle Explorer. Windows task manager has changed, looks good but again, it is not too intuitive to a conventional Windows User in its first sight. Some other minor changes in alerts are also evident. 

As of now, I am ignorant of the intricacies of this version of Windows OS, but from an eagle eye's perspective, the speed and performance seem descent enough, just like its predecessor. Nevertheless, I wouldn't go too far exaggerating that Windows 8 is immaculate because it isn't. I am not as thrilled as I was when I experienced Windows 7 for the first time. It is too good in some aspects and atrocious in some other. I hope the annoying aspects of it are answered in the coming build versions. So to wrap it up, I stick to the word, different. Yes!, Windows 8 is different, a lot different and it has changed from its former versions. It depends on how the users tune themselves with this change. 

Thursday, January 05, 2012

Why Android Sucks? Why iOS is better?

Prior to the reasons I depict to prove a point here, I would want to confess that neither do I blindly follow the cult of apple nor do I hate android based on sheer ignorance. The musings I share here are out of daily observances, experiences and a little bit of drilling into the technical aspects of Android as an OS and more.

I have been a heavy user of android on my HTC wildfire for a while. I find that Android does do what a smartphone ought to do but it doesn't do it with elegance. There are way too many shortcomings and glitches buried down deep into the sheath of the Android eco-system. On the contrary my experience with apple iOS has been limited only to iPod but I must admit the immense pleasure that it gives me and about how it provokes me to lay my hands on it again and again unlike my HTC Android that makes me moan about why I even considered buying it in the first place.

Initially, I was under the presumption that support for multiple hardware was cool but what I eventually realized after drilling in to details is that support for multi-hardware comes at a cost. If I were to design a gallery app for an android mobile or a tablet, I need to ponder over how to get the rendering right on different hardware at different resolutions. This is one reason why many apps on android are not compatible with all kinds of hardware. Take angry birds for instance, it doesn't run on all mobiles and tablets as it ought to. This is annoying both to the developer and to the user as well. On the contrary, within the apple world, all these complexities boil down to a simple fact that I am designing an app for a single target hardware that only runs at a given resolution(s). Because of this streamlined atmosphere to code, my psyche as a developer will subconsciously adhere to deliver THE BEST app for a single hardware rather than delivering a DECENT app that supports all hardware in the world.

Another interesting observation, Every new release of Android version makes me realize how crappy its predecessor is. On the other hand, every new release of iOS would make me realize how awesome the newer version is. There is a wide difference between the two in spite of seeming same. The focus on developing a newer version of Android is more towards rectifying the bad things in the current version and making it better in the newer version. In the iOS scenario, the focus of developing a newer version is to rope in newer features that never existed before. For example : It has been around eight months since I had bought a HTC wildfire running Froyo Android 2.2. Looking at the newer ice cream sandwich version, I whine about how atrocious Froyo is in its speed and performance compared to the latter and how good the newer version of HTC wildfire IS. Whereas if I had bought an iphone 4, probably the only thing that I would be whining about is that I do not get to use the SIRI but I would never be whining about its performance in what it delivers to me as a smartphone. So the key point that I would want to highlight here is that newer versions of Android or its supporting hardware would seem awesome only because the former versions were bad whereas newer versions of iOS and hardware would seem awesome because of their added innovation into every new release or update.

On an other note, the power of a smartphone would be realized to a maximum extent by the apps that run on it. As far as I know, Android is probably the first and foremost entity that can be credited for indirectly being responsible for the injection of infectious malware at a tangible level on a Linux based system. I mean, though not impossible to do it, a Linux core by default is highly resilient to such stuff and with all the malware and antivirus crap that are evidently used in the Android world could only mean one among these two things. Either Android could not exploit the strengths of a Linux core or the hacker/developer is way too smart, which I think the case is certainly the former.

Adding to that, it is way too easy to develop an app consisting of malware and releasing it into the Android market. Of course, Google continuously scrutinizes the apps submitted but that is not just good enough to prevent the damage. Forget malware, Nothing really stops me from writing an untested low quality lameass hello world android app and release it into the market whereas if I were to develop an app for iphone/ipad, I need to ensure that it is thoroughly tested and it literally takes 3 to 4 weeks for iStore to approve the app after submission. This is an example that depicts the spirit of apple, precisely the spirit of Steve Jobs to give the deepest attention to minutest of detail to develop something impeccably tangible.

A recent article on Google+ posted by Dianne Hackborne, an Android developer at Google, throws light, complaining the inefficiency relating to the Android's methodology of hardware acceleration to render graphics. Reading the article one would be convinced that Android fares well only under the availability of considerably high end hardware. One doesn't have to go all technical to comprehend the disabilities of Android. Pick up a HTC or Samsung smartphone, open up around 10 applications one after the other, the performance of the 11th or 12th application that you open up will certainly not be as smooth as the first few. Typically iOS does this task brilliantly. No matter what, most of the RAM will be used only by the opened/active process in the Apple tablet or mobile. Only one application can run at a time on the screen which gives a tremendous performance hit. Android does a similar job in a crooked way, somehow temporarily suspending the inactive process but that doesn't restrict an inactive app running in the background eating away my puny little RAM. For this reason, I even see task managers and process killing apps on Android. Geez! For Christ's sake, I am running a phone, not a super computer! Yet again, Apple proves its best in technical brilliance without compromising performance. With all the brouhaha that surrounds the Galaxy tab's 1 GB RAM and hardware excellence, I wonder if the users of the tab did ever make it a point to notice how iPad 2 does all this process magnificently better than the former within a bare minimum of 512 MB RAM.

From a techie's viewpoint, though the core of Android is written in C and C++, all the APIs and applications that surround it are built on java. There is all this dinosaur conversion of bytecode in dalvik's understandable dex format and other humongous stuff which certainly can never beat the iOS's simplistic approach of limiting all complexities to a mutually compatible C, C++ and Objective C. Anything that involves java usually sucks blood in performance. No matter how awesome the Samsung's hardware is, god knows how many hundreds and thousands of method calls are gone through in java to understand and process the multi-touch event. And again, one doesn't have to drill through all the technical crap to understand the complexities and disabilities of Android. Go ahead, Take a delicate bird feather and compare the touch sensitivity of an iPhone vs an Android phone or an iPad vs any other cog-in-the-wheel tablet you know of, You will get a hang of what I am trying to convey here.

In spite of all its internal flaws, more and more newer and better versions of Android may come up in the near future but it all accounts to mere band-aiding. What's the point? You got your basement built on wooden sticks and lay concrete pillars on your higher floors! Technically, an Android can never beat the brilliance of an iOS in its flawless design and architecture (both hardware and software). Who cares about the numbers in market? After all, it is okay for Galaxy tabs and the rest to be content about being second runners but in reality Apple is running a different race altogether.

EDIT: This post was written when Android devices were running 2.2/2.3. With the advent of ICS, things seem to have changed where in a lot of changes have been made to the Android code base (some portion completely re acrchitectured from scratch) to make it scalable on tablet devices. Also to admit, Off late Apple is just not able to pull out something innovative. Android is certainly moving up the ladder with almost 50% of tablet market conquered. Looking at the circumstances, Android is a clear winner especially when it comes to appealing to the eyes of a user.

PS: As I have quoted before, I am not an Apple fanboy. I am a technical person and I tried to throw some light on fineness of iOS as a whole when compared to Android. I never intended to compare the complete eco system of Android devices and Apple devices. In fact, to be honest, I'd never ever dream of buying an iPad over an Android tablet, for I know it is just a toy, completely ungeek and strict in user customization despite its flawless OS.