Coding Commentary

In addition to Android projects, we're currently involved with Java and Python applications.  Occasionally I come across tidbits that need to shared in hopes of preventing others from stepping in the same "pothole".  As they say, an ounce of prevention is worth a pound of cure!

Intellij IDEA vs. inertia

posted Aug 3, 2013, 8:34 AM by Marsh Gosnell

Inertia is a pretty hard force to change.  As a long time Eclipse user, I found it pretty much met my needs despite a few reoccurring problems and cruft. Despite reading lots of comments about how much better Intellij IDEA, from JetBrains was, it was not much more than a curiosity to me.  Who wants to pay for an IDE when Eclipse is free... 

Inertia moved a little last December when JetBrains offered an "end of the world" sale to celebrate the end of the Mayan calendar.  On an impulse, I bought myself a copy but inertia took over again it just sat there pretty much unused until May.

This spring, a project at one of my clients finally migrated from Visual Source Safe to Git.  I was using an old Eclipse plugin for VSS but that plugin didn't work in anything newer than Helios. I remembered that Ultimate version of IDEA supported VSS and Git plus had a Python plugin so I decided that it was time to give it a serious try.  It took a while to convert the Eclipse shortcuts in my muscle memory to IDEA but I can now say that I wish I had switched earlier.  I find IDEA more intuitive and feature rich compared to Eclipse.  For example, I like how IDEA hilights unused variables and methods.  I have also found the static analysis built into IDEA to be as good as and sometimes better than Klockwork.  One of my biggest surprises was to find that the static analysis also worked on Python code.  

Icing on the proverbial cake is that IDEA is now the basis for Android Studio.  Even though Android Studio is still in developer preview, it has been very stable for me and is now my Android IDE of choice.  I build subtly different versions of Cargo Decoder for each Android distribution channel and the Build Variants feature in Android Studio and Gradle has turned this task from cumbersome to trivial.

If you don't need things only supported in IDEA Ultimate, the free community edition might be the IDE you are looking for.

Google CodePro Analytix

posted Oct 25, 2012, 8:07 PM by Marsh Gosnell

When I work on C or C++,  I turn up the compiler warning level to the highest setting because I firmly believe that a compiler warning is a bug you haven't found yet.  Java on the other hand, will keep you honest in many respects but it is still pretty easily create a bug and not easily find it. 

Google offers a really nice set of free tools for Java developers that think Android developers will also find very useful.   The package is named Google CodePro Analytix and offers code analysis, similar code analysis, JUnit test editing and generation, code coverage, and other features.  More information about the package can be found at https://developers.google.com/java-dev-tools/codepro/doc/

Static code analysis is great because it can find those bugs you stare at and don't see.  One mistake I almost always see a new Java developer make is to compare strings with "==" instead of equals().  If you are diligent code reviewer or have excellent coverage during unit testing, this problem will usually be found.  CodePro will find and complain about this bug.

The code analysis also offers suggestions to improve performance.  Did you know that passing a single character to String::append() is faster than passing a String containing a single character?  CodePro does.  Similarly, CodePro suggests using charAt(0) instead of startsWith() when searching for a single character.

If you work in Java and use Eclipse, you really should take a look at Google CodePro Analytix.  Your customers will appreciate it when you find the bugs before they do.

Build it Once, Run it Everywhere

posted Oct 28, 2011, 5:46 AM by Marsh Gosnell

Cargo Decoder is designed to run on just about every version of Android and on just about any Android device with a single APK.   I presented the tricks and potholes of developing Cargo Decoder at the October meeting of Android Alliance Philadelphia.  While ActionBarSherlock and the Android Compatibility Library are key tools in building apps that are compatible across devices running Android 1.6 and later, being compatible with Android 1.5 adds a special set of rules to follow.  The presentation I gave is available here.

equals is not your friend

posted Jun 21, 2011, 8:17 PM by Marsh Gosnell   [ updated Jun 22, 2011, 5:29 AM ]

I fixed a bug in a Java application recently that took me a while to find because the code looked OK but was fatally flawed.  The statement that caused all the grief was:

          If (shelfType.equals(m_neType)) { 

If the statement was written as:

          If (shelfType == m_neType) {

there would have been a nasty compiler error stating you cannot compare a ShelfType object to an NE object.  Using equals trumped that compatibility test and the equals() always returned false for many months. 

The moral of this story is that equals() is not your friend and should never be used to compare objects when == and != can be used.  Only use equals() when the native comparison operators cannot be used (e.g., comparing the contents of objects like a String).

1-4 of 4