Blog

No ifs ifs or ifs

I don't come across it very often, but there's piece of code that I find so jarring as to be offensive. It seems like a very simple thing, but it drives me up a wall. Here is the basic snippet that irks me so:

if ( condition ) {
    return true;
} else {
    return false;
}

You should never return the exact same value that your conditional clause evaluates to; just return the conditional clause! Failing to do so leads to useless clutter and it screams "amateur". I suspect that experienced programmers fall into this trap when the conditional statement is complex enough to distract the eyes. It could also crop up during a refactor session. Whatever the reason the code got this way, it shouldn't ever be checked in like this.

Comment

Properly checking Android version

If you're doing Android development, you have to support multiple versions of the operating system. It's pretty foolish not to and, for all the mellodramatic cries of "alas, fragmentation!", it's really not that hard. The basic idea is that you want to execute one piece of code for one version of Android (and higher usually) and another piece of code for everything else:

if ( version >= gingerbread) {

    do it the new way

} else {

    do it the old way

}

I've seen many ways that people will attempt to check the version of Android. It's usually something along these lines:

 

if (android.os.Build.VERSION.RELEASE.startsWith("1.") ||
        android.os.Build.VERSION.RELEASE.startsWith("2.0") ||
        android.os.Build.VERSION.RELEASE.startsWith("2.1")){
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}

 

This code is needlessly complex and it's doing several wastefull string comparisons every time you perform the check. Don't do this. Instead, rely on the Build.VERSION.SDK_INT value. The previous comparison becomes:

if (android.os.Build.VERSION.SDK_INT >=

        android.os.Build.VERSION_CODES.FROYO) {

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

}

This is an integer comparison and it's much more readable. You may be thinking that this comparison will explode when the current operating system is being compared to a newer one. For instance, how would a froyo phone be aware of gingerbread? Shouldn't that cause a runtime error? The operating system doesn't need to be aware of newer versions of Android because the Build.VERSION_CODES values are injected by the compiler. This is why you should always compile against the highest version of Android available.

In Tags
Comment

Hey, LogCat, shhhhhhhh.

One of the most frustrating things about using LogCat from the Android Developer Tools is that it can be really noisy. The situation seems to be particularly bad with manufacturers who use heavy customizations. The problem is the way too many logging statements were left in and your one-liner can scroll right off the screen before you notice it. Of course you can filter by a single tag if you're looking for a particular statement, but if you want to monitor the situation overall, you'll need to be a quick draw on that pause button. If you're dealing with camera hardware, the log is useless. For some reason, the developers decided to create 4-5 log statements per frame.

Well, you can fix this by creating a new filter and using a regex to ommit the noisy tags. Here's what made my log readable:

^(?!.*(CameraHal|MotOverlay|DOMX_RPC)).*$

The most important thing to take out of this is that you, as a developer, need to be sensible about what you leave in the final build. Your logging statements create new String Objects, so you're negatively impacting the performance for every logging statement. Worse still, you may be exposing information that puts you or your user at risk. It's a hassle to try to comment all of them out before generating the final build, and this is where Proguard is your friend. Use it to take out some or all of your logging statements:

 

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** e(...);
}

 

Comment

Abe Clone Attack (learning OpenGL)

I wanted to learn OpenGL, so I threw together a game using libgdx. Below is the text from the market page for the app.

This isn't meant to be a serious game. I threw this together in my spare time over the course of a week to learn OpenGL basics on Android. I wrote a rudimentary game engine including animation, collision detection and score keeping.

Why Abe Lincoln? When I needed an image to represent a sprite, his face popped into my mind. Maybe it's his striking facial features or the fact that he's a person that I respect deeply that made me think of him in that moment.

The sprite class holds an array of animations and the state of the sprite (position, dead, type). For each call to the renderer, the engine passes the number of frames (calculated by time since last render) to the sprite, which then advances that many frames into its current animation.

Upon completing a "death" animation, the sprite marks itself as "dead". Upon the next call to render, the engine removes the sprite from those to be rendered.

I wrote a texture library to reuse textures among sprites and animations. Animations as for a texture (by resource name) from the texture library. The library either returns the texture from a map, or loads it into the map and then returns it. This is an easy way to share a single texture for all identical animation frames.
I know this is ugly. I know it's simple. It was fun to make and I'm happy to say that I learned something in the process.

Android Market Link

In
Comment

PAX 2011 Unofficial Guide

Sorry, I never followed through on my series of guides. I got so involved in learning the Android API that I actually started and completed a full app.

I went to the Penny Arcade Expo for the first time this year. I was planning on traveling with three buddies, but they all had to back out due to uncontrollable events. I saw it as a chance to create an app organizing all of the maps, schedules, and twitter feeds for PAX. There were nearly 70,000 attendees this year, so playing it by ear sounded daunting.

The biggest feature I was not able to implement was syncing events with Google Calendar. I finished it at nearly the last minute and wasn't able to promote it properly. Even so, I had about 300 downloads at peak. That number is dropping like a rock now that the convention is over, but I'm planning on updating it for the next convention in spring.

I think I'm in love with mobile development and I'm hatching an idea for my next app.

Though I went alone, I did connect with several friends from Gamers With Jobs and the weekend was great success.

 Android Market Link

Comment

Android For Absolute Beginners, Part 1

If you've successfully completed part 0, your computer is set up and read to write Android apps. The final part of that guide had you launching an app that was generated automatically. In this guide you'll take a peak at the guts of this app and, hopefully, learn just a little bit about Android.

Problem: I don't know anything

Okay, you know plenty. In fact, you might even know some of what is in this lesson. To make this as helpful as possible for absolute newbies, let's pretend that you don't know anything yet.

Read More
Comment

Android for Absolute Beginners, Part 0

I decided to write a beginner's programming guide for Android. My intention is to be as basic as possible for people who have the interest, but no experience. The requirements are low, but there are a few and they are:

  • You do not know how to program
  • You want to learn how to program
  • You're interested in making Android apps
  • You know how to install software on your computer

The fact that you found this guide means you have interest, but let me help out those who are on the fence. First, let's talk about what programming is.

Read More
Comment

vacation.finish()

Now that the family vacation is over, It's time to get back to work on my first Android app.  It's time to knuckle down and churn out a basic but fully functional alpha.  My first app is tentatively called "Scoreo", by me at least.  It will be a basic score keeper / stat tracker for board and card games.  I'll try to put out basic tutorials as I make progress.  I always feel that I have the strongest grasp on something when I can teach it to someone else.  I don't really have anyone to teach directly so tutorials will have to suffice.  My first tutorial will handle Activities.

Comment

Paint.NET

When I was eighteen I thought it was amazing that every popular piece of software was free for the taking.  I didn't need to decide between Fireworks and Photoshop, I could have both.  Actually, I could have the entire suites of software offered by Macromedia and Adobe.  I'm not interested in lecturing anyone, but suffice it to say I've turned to free alternatives when I can't afford the software I want.

The Gimp has matured as a powerful free image editor, but the startup time and awkward interface make it frustrating to use.  I use Picasa as my photo organizer and it has taken over basic cropping and color adjustment duties.

Read More
Comment

Family++

At 10:30 a.m. yesterday my second child was born.  I feel as though I'm floating.

 

Comment