android

To Android or to iPhone?

I’ve wanted an iPhone for at least a year. Six to eight months ago I decided I was going to make the switch from Verizon to AT&T – just for the iPhone. Our Verizon contract expires next month. Now that December 8th is almost here I have begun counting the days. How exciting it will be to walk into the AT&T store and pick up my new iPhone.

I appreciate the quality of Apple products. From the beauty and elegance of the design to the simplicity of the user interface. It is obvious that they put great thought into the product as a whole. I wouldn’t call myself a “fanboy” but I’m pretty close. This is why it has come as a surprise that I have decided to go with the new Droid that Verizon just made available (http://phones.verizonwireless.com/motorola/droid/).

Wait, What?

What changed my mind? The most powerful reason is this feeling I’ve had growing inside me. The feeling that going with the iPhone/AT&T combo might be a mistake. I cannot put my finger on it. It just didn’t feel right. I have never been one to follow the crowd. Perhaps somewhere deep inside I’ve gotten the impression that the iPhone has become too common. Then again maybe there is something to all the red and blue maps I’ve been seeing showing how much more coverage Verizon provides.

About 2 weeks ago I was listening to a podcast of This Week in Google (http://www.twit.tv/twig). Leo Laporte suggested that with the release of Droid there’s no reason to switch from Verizon to AT&T just for the iPhone. I don’t know Leo personally, however, in the past few years I have listened to Leo on a number of shows. I get the impression that he has an open mind and is not one to give in to hype. Leo’s comment was the proverbial straw. I needed to at least look into this other option.

I went to the local Verizon store a couple of times. I spoke with a couple of Verizon people in there, one of whom had just purchased a Droid. I’ve played around with it and must say it’s a nice phone.

Personally we have not had any issues with Verizon. Our bill has always been accurate. Once when speaking with them on the phone they noticed we were texting a lot and suggested a plan to help us save money on texting. We’ve not had to deal with their customer service for anything major. So far all my interactions with Verizon have been positive.

One thing that disturbs me is how Verizon locks down their phones. That Verizon wants to have control over the phones. One example is how they charge $15 to allow access to Microsoft Exchange. This is something the phone will do as is. “Enterprise Data Plan” is their fancy name for it but it doesn’t cost Verizon anything. They’ve just found something that they can charge for (e.g., http://www.rimarkable.com/almost-thought-for-minute-that-verizon-wasnt-going-to-lock-the-droid-down).

I hope to develop applications for the phone I get, for fun and profit so the development process is something I considered as well.

iPhone Development

The language is Objective-C. I’ve written a simple iPhone application and gotten it to work in the simulator. I enjoy writing code in Objective-C and the frameworks Apple provides make it quite easy to do some complex things. Tools used to build the applications like Xcode and Interface Builder work well together. Instruments is a nice profiling tool. The simulator integrates nicely with Xcode. Even so, I am still very much a novice when it comes to the language. The thought of having to deal with memory management still scares me.

From what I’ve read, it sounds like getting your iPhone application approved by Apple is a huge pain (e.g., http://developer.apple.com/iPhone/program/apply.html, http://iphoneincubator.com/blog/app-store/rejections, http://ephemera.lifewithalacrity.com/2008/07/the-nightmare-t.html). It is unfortunate that something that should be simple isn’t.

The fee is $99 to get into the program. But get this, you cannot get the application onto any iPhone, not even for testing, without paying the $99 fee. In order to install the application on your iPhone you need to pay the fee and get a certificate from Apple (http://www.mikeash.com/?page=pyblog/the-iphone-development-story.html). Even if you just want to write an application for your personal use on your own iPhone with out paying the fee you are out of luck (I know there are ways around this but why should you have to?).

Android Development

For Android the language is Java. I’ve been doing Java for quite a while and am very comfortable in that environment.

For Android it seems like the development and publishing process is easier. I’ve already created a development signature for the tutorial applications (http://developer.android.com/guide/publishing/app-signing.html). However, I won’t know for certain that I can install anything on the phone until I get a phone.

The fee is $25 to get your application onto the Android Market. It sounds like it is very simple (http://www.4feets.com/2009/02/developing-and-publishing-a-new-android-game-in-3-days/). As I understand it you can put your application on your own phone without paying anyone anything. The $25 is only if you want to get your application out to the world.

Another difference between the iPhone and Android phones is the ability for Android phones to run more than one application at a time. When I first heard this I thought about what this might mean to me as a user and it didn’t seem to matter. However, after reading the Android developer documents I can see how this would be very useful as a developer. As I understand it, this feature is available on the iPhone just not to third-party applications.

From what I’ve read, the Android world is much more open and overall less strict. For instance, I was able to download the Java source code. In addition, developers can publish to the Android Market anything they want. That’s nice but it also means there might be a bunch of crap apps out there (I see that as an opportunity to make things better).

I also took into account the current size of the 2 application venues. The iPhone App Store recently hit 100,000 apps while the Android Market is around 10,000. What that tells me as a developer is that it will be more difficult to create something new and different for the iPhone. If I’m going to create something new and (hopefully) make money from it, it seems to me that there is a much greater chance of creating something new for the Android Market.

One last thing. These are phones. They are nice phones with a bunch of stuff on them but their primary job is to be a phone. That means that the phone part should work without question. Friends and colleagues who have iPhones have mentioned that AT&T constantly drops calls and it seems to be getting worse. One guy at work said he loses at least a call a day. In the past 2 years with Verizon I’m sure I’ve lost a call or two but it happens so rarely it’s not something I even think about.

[EDIT: 2009-11-18] After talking with an admin friend about Verizon charging extra to connect to Microsoft Exchange he made a good point. There is a lot of data that flows between a client and Exchange server as it is constantly pulling and pushing. Don’t know if that warrants an extra $15 a month but it is a valid point.

maven

OutOfMemoryException in Maven

I came across a situation today where Maven threw an OutOfMemoryException. I didn’t think the process would have taken that much memory but it clearly did. I was trying to deploy. I then tried it again but skipped the tests. No good. I found out that by setting the MAVEN_OPTS environment variable to something like -Xmx512m I was good to go.

programming

Comparing Hash Tables

I was recently asked in a comment how to compare 2 hash tables in Perl. Furthermore, the commenter mention that this would be use in a subroutine.

There is a module Data::Compare http://search.cpan.org/~dcantrell/Data-Compare-1.19/lib/Data/Compare.pm. I’ve never used this in any way other than to learn what it can do. From what I can tell it will not provide details. It will just tell you yes, the data structures are the same or no, the data structures are not the same.

If you want to get any detailed information you can always roll your own.

use strict;
use warnings;

my %hash1 = ('a' => 'b', 'b' => 'c', 'c' => 'd');
my %hash2 = ('a' => 'b1', 'b1' => 'c', 'c' => 'd');

compare(\%hash1, \%hash2 );

sub compare {
  my $hash1 = shift;
  my $hash2 = shift;

  if( keys %{$hash1} != keys %{$hash2} ) {
    print "Hash1 has ", scalar(keys %{$hash1}),
          " keys but hash2 has ", scalar(keys %{$hash2}), "\n";
  }

  # Compare hash1 to hash2
  # Sorted for clear output
  # Case insensitive comparison for sorting
  # Assumes all keys and values are strings
  #
  for my $key_hash1 (sort {lc($a) cmp lc($b) } keys %{$hash1} ) {
    if( ! exists $hash2->{$key_hash1} ) {
      print "Hash1 contains key '$key_hash1' but hash2 has no such key\n";
      next;
    }
    if( $hash1->{$key_hash1} ne $hash2->{$key_hash1} ) {
      print "Both hashes have '$key_hash1' as a key ",
            "but hash1's value is '$hash1->{$key_hash1}' ",
            "and hash2's value is '$hash2->{$key_hash1}'\n";
    }
  }
}

To complete the process you’d also loop through the keys of $hash2 and compare its keys and values to those of $hash1 in a manner similar to the above loop.

References

The second thing to comment on is that hash tables are just a variant of array. In an array the idex is an integer. In a hash the index can be an integer or also be a string (any SCALAR really). The only thing that the => symbol does in the above example is make it more readable by humans. I could have replaced all => with a comma and it would work.

The problem with passing 2 hashes/arrays to a routine is that they would get flattened into one list of values and the routine would see one hash/array instead of two. In order to maintain their individuality the hashes get passed as a reference. To then use the hashes you have to dereference the hashes. In case you didn’t know what the extra symbols mean, they are there because we’re working with references.

Another Option

The commenter mentioned that he is working with 2 files. In the file there are key value pairs. I got to thinking that he might not have to use Perl. He might try sorting the lines in each file (unix/linux/cygwin has a command called sort that has a number of options for sorting). Then use a tool such as diff to show the differences. Depending on how large the files are this might be a more efficient solution. So if the files are sorted in the same way, the keys, being the first entry in the file, would appear in the same order. Example: file one contains

abc: def
abd: fed
efg: hello
xyz: pdq

abc: def
efg: good bye
xyz: pdq

So diff would show something like

2,3c2
< abd: fed
< efg: hello
---
> efg: good bye

You can see from this that file 1, noted by the lines beginning with <, contains abc: fed and efg: hello and that these are not in file 2. It also tells you that file 2, noted by the lines beginning with >, contains efg: good bye.

If you’re not comfortable with this kind of output you could use something like TkDiff to view the differences in a more visual manner (http://sourceforge.net/projects/tkdiff/).

Additional Reading

This article might be useful for understaning references in Perl: http://www.perlmeme.org/howtos/using_perl/dereferencing.html.

You might also find Tom Christiansen’s Object Oriented Tutorial found here: http://www.perl.com/doc/manual/html/pod/perltoot.html.

maven

Maven Commands

I’ve collected a short list of commands I use for maven and thought I’d share.

Install to Local Repository Compile Test Classes, Do Not Run Tests

mvn -Dmaven.test.skip.exec install

Install to Local Repository Do Not Compile Test Classes, Do Not Run Tests

 mvn -Dmaven.test.skip install

Generate Test Results in HTML Format (tests will be run)

mvn surefire-report:report

Generate Test Results in HTML Format (tests will not be run, source of reports will be last tests run)

mvn surefire-report:report-only

Run Specific Test Class

mvn -Dtest=MyTestClass test

Use Patterns to Run Specific Tests

mvn -Dtest=MyTest*lass test

Run Multiple Specific Test Classes

 mvn -Dtest=UnitTestClass,Accep*TestClass test

View Dependency Tree: output is a mono-spaced tree views of all classes in the current artifact

mvn dependency:tree

Download Sources: Usually when using Maven only the binary version of your dependency are fetched from the repository. This will tell Maven to download the sources of your dependencies.

mvn [goal] -DdownloadSources=true

Download JavaDocs: Same as with sources, Maven doesn’t usually grab the JavaDocs.

mvn [goal] -DdownloadJavadocs=true

programming

Hash tables

I have found a number of potentially unconventional uses for hash tables (aka “associative arrays”). I suppose the first thing that comes to mind when thinking of hash tables is as a way to map a given value to another value. As a very simple example, say you have a list of items and want to keep track of how many of those items you have.

my %items = ();
$items{shoes} = 2;
$items{pants} = 1;
$items{dogs} = 5;
$items{cats} = 50;

We often refer to this arrangement as a “key/value” pair. Now, if you want to know how many shoes you have you can do so by referencing $items{shoes}. If you want to know just how crazy the cat person is, look at $items{cats}.

If you want to print out the item and count, loop through the hash as follows:

for my $key ( keys %items ) {
  print "I have $items{$key} $key\n";
}

On my machine, I see this output:

I have 50 cats
I have 1 pants
I have 2 shoes
I have 5 dogs

One thing to note is that there is no guarantee that the hash table will store the keys in a particular order. That is, there is nothing that says that every time that loop gets run the output will appear in the same order. In practice, for a list that small it probably will print out in the same order. However, you cannot bet on it, and once the list gets larger and items are added and removed the likelyhood of an unpredictable order will increase. Part of it has to do with how perl determines the most efficient way to store/retrieve the values.

If the order is important, it is useful to sort the keys before using them.

A nice thing about the for loop is that a sort routine can be inserted in the parentheses. As long as a list of elements appears in the parentheses after all processing is done perl doesn’t care. In the above example, if I wanted to sort the hash based on the keys:

for my $key ( sort { $a cmp $b } keys %items ) {
  print "I have $items{$key} $key\n";
}

This produces the output:

I have 50 cats
I have 5 dogs
I have 1 pants
I have 2 shoes

Since there is a sort done you can be certain that this is how the ouptut will appear every time.

If, on the other hand you would like to sort by the value instead of the key:

for my $key ( sort { $items{$a} <=> $items{$b} } keys %items ) {
  print "I have $items{$key} $key\n";
}

This produces:

I have 1 pants
I have 2 shoes
I have 5 dogs
I have 50 cats

You may have noticed that I used cmp and <=> We use cmp to compare alpha entities. The <=> is used when comparing numbers.

If you want the order reversed, simply revers the $a and $b (or $item{$a} and $item{$b}). The $a and $b variables are special variables in perl. When sorting, 2 elements are compared to see which one should come before the other. Two elements are tested. Then two more elements and so on until the list of elements is exhausted. For now, just know that in sort $a represents the first element and $b represents the second element. If you are curious, you can print out the elements as they are being sorted. Remember when I said that as long as a list of elements appear within the parentheses perl doesn’t care? Printing doesn’t affect the final list so this is valid:

for my $key ( sort { print "$a cmp $b\n"; $a cmp $b } keys %items ) {
  print "I have $items{$key} $key\n";
}

I see this output:

cats cmp pants
shoes cmp dogs
cats cmp dogs
dogs cmp pants
pants cmp shoes
I have 50 cats
I have 5 dogs
I have 1 pants
I have 2 shoes

Now, back to some of the less common uses of hash tables. The keys of a hash are unique. That means that in the following code, there will only be 2 keys: cats and dogs. The second time cats apppears the existing entry for cats gets overwritten with the new value.

my %items = ();
$items{cats} = 10;
$items{dogs} = 1;
$items{cats} = 50;
for my $key ( sort { $a cmp $b } keys %items ) {
  print "I have $items{$key} $key\n";
}

This is the output I see:

I have 50 cats
I have 1 dogs

One way we can take advantage of this feature is to ensure the uniqueness of a list.

Say we have this list:

my @list = qw(apple banana pear apple peach apple cherry apple berry);

By passing the values of this list to a hash table, we can remove dupliate values:

my %fruit = ();
foreach my $value ( @list ) {
  $fruit{$value}++;
}
my @unique_list = keys %fruit;
print "unique list: ", join( ', ', sort {$a cmp $b} keys %fruit ), "\n";

I get this as the output:

unique list: apple, banana, berry, cherry, peach, pear

Again you can stick a sort in there to get a sorted list.

Not only did you get a unique list of fruit, you also got a count of each fruit in the original list. That’s what the $fruit{$value}++ did. When not explicitly initialized the value of an entry in a hash table will be set to 0. In case you are unfamiliar, $fruit{$value}++ is a short-hand way of doing:

foreach my $value ( @list ) {
  if( ! exists $fruit{$value} ) {
    $fruit{$value} = 0;
  }
  $fruit{$value} = $fruit{$value} + 1;
}

In fact, if you do not use the ++ notation and use warnings are in effect (along with use strict, use warnings is a good habit to get into), perl will give a warning stating something like: Use of uninitialized value in addition (+) This means that $fruit{$value} has never been set to a value so perl is warning you that you might be attempting to add a value to an uninitialized value.

Back to the counts. You can access the counts, the value part of the key/value pair, in the same way as we accessed the number of shoes at the top of this article.

print "unique list: ", join( ', ', sort {$a cmp $b} keys %fruit ), "\n\n";
foreach my $key ( sort { $a cmp $b } keys %fruit ) {
  print "$key appears $fruit{$key} time(s) in the original list\n";
}

I get this output:

unique list: apple, banana, berry, cherry, peach, pear

apple appears 4 time(s) in the original list
banana appears 1 time(s) in the original list
berry appears 1 time(s) in the original list
cherry appears 1 time(s) in the original list
peach appears 1 time(s) in the original list
pear appears 1 time(s) in the original list

There you have some ideas about how to use a hash table in ways that may not have crossed you mind.

maven

Converting from Apache Ant to Apache Maven

At work (http://www.monsanto.com/) we’re converting all projects from Apache ant to Apache maven. The architecture team came up with the POM which all our applications would extend and set up the company-wide repository. I spent about a week and a half converting our project. Everyone thought that conversions would go smoothly. However, I haven’t heard of any project taking less than a week. A colleague wrote an application called “mavenizer”. The mavenizer helps analyze your project and generates a POM for each artifact. We had to determine how to split our application into artifacts but using the mavenizer app helped us figure out the dependencies and got us started with an initial POM.

I can’t speak for other projects but probably the most time consuming part of the process was weeding out the dependencies. We had some production code that had interdependencies but the majority was located in the test code. For instance, we have a TestHelper class that provides methods setting up the data environment prior to running a test and for tearing it down once the test completes. This class was used in a couple of leaf artifacts (domainpos and batchprocess).

When working with maven each, for lack of a better word, installable module is packaged in what’s known as an artifact. An “installable” would be something like a jar or war or ear or some other unit. We have a “common” artifact that’s a jar and can be used by all other artifacts. Then there is an “appdao” jar which contains all database-related code. Finally there are 3 leaf artifacts which can depend on the common and/or appdao but should not depend on each other: domainpos (war that is our point-of-service); plugin (jar that is a plugin for the company-wide GUI framework); batchprocess (jar that runs on a schedule for batch processing).

I’ll post here more about the conversion process and about things I learn about maven.

%d bloggers like this: