Keep your classes and methods as small as possible

I have a friend who recently started a new programming job and he kept complaining that his code was being rejected. Most of the time it was because his methods and classes were too long; it didn’t make sense to him why he needed more classes or methods to accomplish something simple. He felt that it would make his code more complex and harder to debug instead of just keeping it only one class and a couple of methods at most.

This is an advice that we programmers tend to give a lot, but most of the time we don’t really explain why is a good advice. Some people take it to far and I see why you would think a class with too many methods will be more complicated than one with very few. In the interest of keeping things simple, I won’t go into the details of SRP ( Single Responsibility Principle ) and encapsulation, but a basic definition would be that a class or method should be responsible for one thing and you can change it, without affecting the other parts of the system.

Let’s say that we need to send an email with some data when a user is older than 18. The data will be read from a config file. To keep it short we’ll assume that the email sender and config are in different classes.

class MemberEmail
{
  const MAX_AGE = 18;

  public function sendEmailToMember( $user )
  {
    if ( $user->age > self::MAX_AGE )
    {
      $config = new SomeConfig( );
      $values = $config->getValues( );

      if ( empty( $values ) === false )
      {
        $emailSender = new EmailSender( );

        foreach ( $values as $value )
        {
          $emailSender->sendEmail( $value );
        }

        return true;
      }
    }

    return false;
  }
}

Seems simple and easy to read, but if you take a closer look, its not very testable since we can only test on user age and not if the config is empty.

class MemberEmailTest
{
  protected $memberEmail;

  public function setUp( )
  {
    $this->memberEmail = new MemberEmail( );
  }

  public function testSendEmailToMemberReturnsFalse( )
  {
    // Test user is younger than 18
    $user = new User( );
    $user->age = 16;

    $this->assertFalse( $this->memberEmail->sendEmailToMember( $user ) );

    // Test config is empty. Wait how can I mock SomeConfig?
    $user->age = 18;
  }
}

So we need to refactor the class by taking out the read config logic into its own method.

public function getValuesFromConfig( )
{
  $config = SomeConfig( );
  return $config->getValues( );
}

By doing this, we now can test both conditions by stubbing the getValuesFromConfig method and defining our own value. This allows us to check that the config is returned as an array as well.

 public function testSendEmailToMemberReturnsFalse( )
  {
    // Test user is younger than 18
    $user = new User( );
    $user->age = 16;

    $this->assertFalse( $this->memberEmail->sendEmailToMember( $user ) );

    //Test config is empty
    $user->age = 18;

    $stub = $this->getMockBuilder( 'MemberEmail' )
                 ->setMethods( array('getValuesFromConfig') )
                 ->getMock( );

    $stub->expects( $this->any( ) )
         ->method( 'getValuesFromConfig' )
         ->will( $this->returnValue( array( ) ) );

    $this->assertFalse( $this->memberEmail->sendEmailToMember( $user ) );
  }

But even though this improves things, the class is still not very maintainable.  What if the action now depends on the user being a certain age, gender, or country? We could create a lot of methods to validate each thing, but our class would do too much.  Don’t forget SRP, so lets create another class to handle this.

class MemberEmailValidator
{
  const MAX_AGE = 18;
  const VALID_GENDER = 'female';
  const VALID_COUNTRY = 'Mexico';

  public function userCanSendEmail( $user )
  {
    $this->validateAge( $user->age );
    $this->validateGender( $user->gender );
  }

  protected function validateAge( $age )
  {
    if ( $age < self::MAX_AGE )
    {
      $this->raiseException( 'Too young' );
    }
  }

  protected function validateGender( $gender )
  {
    if ( $gender !== self::VALID_GENDER )
    {
      $this->raiseException( 'wrong gender' );
    }
  }

  protected function raiseException( $msg )
  {
    throw new Exception( $msg );
  }
}

Now we can write separate tests as well and know that if we change our validations we will not affect the other class and viceversa.

class MemberEmailValidatorTest
{
  protected $myValidator;

  public function setUp( )
  {
    $this->myValidator = new MemberEmailValidator( );
  }

  /**
   * @expectedException Exception
   */
  public function testValidateAgeThrowsException( )
  {
    $this->myValidator->validateAge( 12 );
  }

  /**
   * @expectedException Exception
   */
  public function testValidateGenderThrowsException( )
  {
    $this->myValidator->validateGender( 'male' );
  }
}

Quick note, when writing these kind of validators, I like to use exceptions in the private methods instead of returning true or false.  That way I don’t have to write one big if and I can take advantage of the @expectException notation. This leads to cleaner tests.

Now the code is looking a lot better.

class MemberEmail
{

  public function sendEmailToMember( $user )
  {
    $myValidator = new MemberEmailValidator( );

    try
    {
      $myValidator->userCanSendEmail( $user );
      $values = $this->getValuesFromConfig( );

      if ( empty( $values ) === false )
      {
        $emailSender = new EmailSender( );

        foreach ( $values as $value )
        {
          $emailSender->sendEmail( $value );
        }

        return true;
      }
    }
    catch( Exception $e )
    {
      // fail silently or log this
    }

    return false;
  }

  public function getValuesFromConfig( )
  {
    $config = SomeConfig( );
    return $config->getValues( );
  }
}

But wait! There’s more. We still have not completely solved our testable problem. We moved one dependency into its own method but we still have two left that will cause problems.  We can create new methods for them, but remember when I told you that some people take it too far with creating a lot of methods?

Dependency Injection (DI) to the rescue!

Instead of the class getting the object it needs, you provide the class with the other objects they’ll use by “injecting” it, just like the name says. One of the advantages of using DI is that it makes our tests a lot easier, instead of mocking our config object like we did above, we can create an empty config object and use that for out tests. For this, always prefer interfaces over direct classes, it will make your code cleaner and easier to maintain since no matter what object is passed along you’ll know that it will have the methods it needs.

  interface IConfig
  {
    public function getValues( );
  }

  class SomeConfig implements IConfig
  {
    public function getValues( )
    {
      // code for retrieving values
    }
  }

  class EmptyConfig implements IConfig
  {
    public function getValues( )
    {
      return array( );
    }
  }

Dependencies can be injected in the constructor or with setter methods, lets use the constructor

class MemberEmail
{
  protected $myValidator;
  protected $emailSender;
  protected $someConfig;

  public function __construct( IValidator $myValidator = null, IEmailSender $emailSnder = null, IConfig $someConfig = null )
  {
    $this->myValidator  = ( $myValidator === null ) ? new MemberEmailValidator( ) : $myValidator;
    $this->emailSender  = ( $emailSender === null ) ? new EmailSender( ) : $otherClass;
    $this->someConfig   = ( $someConfig === null ) ? new SomeConfig( ) : $someConfig;
  }

  public function sendEmailToMember( $user )
  {
    try
    {
      $this->myValidator->userCanSendEmail( $user );
      $values = $this->getValuesFromConfig( );

      if ( empty( $values ) === false )
      {
        foreach( $values as $value )
        {
          $this->emailSender->sendEmail( $value );
        }

        return true;
      }
    }
    catch( Exception $e )
    {
      //Log or fail silently
    }

    return false;
  }

  public function getValuesFromConfig( )
  {
    return $this->someConfig->getValues( );
  }
}

Now we can write our tests and be sure we won’t be using production data

class MemberEmailTest
{
  protected $emailSender;

  public function setUp( )
  {
    $this->emailSender = new EmailSenderStubbed( );
  }

  public function testDoSomethingReturnsFalseNoConfig( )
  {
    $user = new User( );
    $user->age = 18;
    $user->gender = 'female';

    $config = new EmptyConfig( );
    $myValidator = new MemberEmailValidator( );
    $memberEmail = new MemberEmail( $myValidator, $this->emailSender, $config );

    $this->assertFalse( $memberEmail->sendEmailToMember( $user ) );
  }
}

So in the end we took 20 lines of code and turned them into 67 lines ( 2 classes, not counting interfaces ), but I hope that you see that by taking “simple” classes and breaking them into smaller ones, you end up with code that’s flexible to change, easier to debug, and cleaner.

Harnessing pgTAP in the Large

My colleague Peter Eisentraut and I had the opportunity to speak at PGConf NYC 2014. I gave a talk on how we use pgTAP as part of our Postgres test harness to unit test the PostgreSQL function API we provide to the application developers at MeetMe.

You can find the the slides at http://github.com/grzm/2014-pgconf-nyc-harnessing-pgtap.

The audience asked a lot of good questions and I got to meet some great developers. One of the benefits of speaking at a conference is interacting with the attendees and other speakers. Everyone has a different story to tell based on their own experience. It’s a wonderful way to get fresh perspectives on issues that we see in our own work.

As always, a lot of sharp minds turn out for Postgres events, challenging (in a good way!) the ideas I present, which in turn gives me the chance to reevaluate and improve what we do.

We’ll be releasing the test harness as a Ruby gem in the coming days. We’ve found it useful in providing a straightforward way to develop and test our API. There’s a lot of great work being done in the area of database testing and deployment (such as sqitch), and I’m looking forward to feedback.

Best Practices for Consuming APIs On Android

First Some History

Up until fairly recently integrating APIs into your Android application had been the wild west. Each application had their own networking and caching implementations that were generally pretty brittle and didn’t handle non optimal networking conditions terribly well.

Thanks largely to Square Inc, the innovative company bringing credit card processing for businesses to mobile phones, there is now a wealth of high quality open source libraries available that support API integration in your Android applications.

What will we learn how to do?

In this post, we will learn how to use a Retrofit, OkHttp and GSON stack in order to be able to simply and robustly integrate a REST API into your application. Using this stack we will download and parse some data from Twitch.tv.  Following the steps outlined, you will be able to integrate most REST APIs into your application in just a few minutes, without writing any boilerplate code.

Lets Learn About the Stack

Retrofit

Retrofit makes it incredibly easy to download JSON or XML data from a web API and parse it into a Plain Old Java Object (POJO). For example to download a users repo information from Github all the code you need to write is:

@GET("/users/{user}/repos")
List<Repo> listRepos(@Path("user") String user);

Additionally you would need to create the Repo POJO so that the parser knows what type of data you are expecting from that endpoint.  This code can be auto-generated, more on this later.

Its just as easy to provide query parameters or do POST or HEAD requests. For instructions for how to connect with different types of APIs just see the documentation.

One of the nice features of Retrofit is the ability to add additional logic to all requests and responses. You could for instance add additional data to all request HTTP headers or intercept any unauthorized response error codes and redirect the user to your login screen.

OkHttp

OkHttp is an HTTP client for Android applications. It is efficient by default and includes support for SPDY, connection pooling, GZIP, and a HTTP response cache.

In addition OkHttp handles common networking issues automatically and silently such as retries and SSL handshake issues. Retrofit will use OkHttp automatically as its networking layer if it is present in your application.

GSON

GSON is a Java library that is used to parse JSON data into a POJO. It can also be used to convert a POJO into JSON, which in Android can be helpful as a way to store generic objects into shared preferences.

To use GSON you essentially just need to create a representative POJO of the data you wish to parse and run GSON on the data to parse it into an instance of the POJO. Its dead simple and makes parsing a breeze. To learn how to create your POJOs compatible with GSON see the documentation. Retrofit uses GSON in order to do its JSON parsing.

Lets do Some Coding!

Add the libraries to your project

  1. Download the jar files for Retrofit, OkHttp, and GSON.
  2. Drag each of the jar files into your libs folder in your project
  3. If using Android Studio Sync the project with gradle

Find or write an API

You probably already have an API in mind but if you are looking for a directory of APIs I recommend ProgrammableWeb. For the purposes of this tutorial we are going to be parsing a list of streams available from Twitch.tv. To see the request format see their API documentation. The basic API request to get the list of steams from Twitch.tv in JSON format is: http://api.justin.tv/api/stream/list.json

Capture Some Sample Output

In order to proceed we need some sample data that is returned from the API. In this case since it is a GET request we can just run the request in a web browser, and copy out the response such as:

[{“broadcast_part”: 4, “featured”: true, “channel_subscription”: true, “audio_codec”: “uncompressed”, “id”: “6640712464”, “category”: “gaming”, “title”: “Fnatic xPeke, Normals(ranked down) on smurf”, “geo”: “DE”, “video_height”: 1080, “site_count”: 8014, “embed_enabled”: true, “channel”: {“subcategory”: null, “producer”: true, “image_url_huge”: “http://static-cdn.jtvnw.net/jtv_user_pictures/xpeke-profile_image-a182a5fe5a8f239b-600×600.jpeg”, “timezone”: “Europe/Madrid”, “screen_cap_url_huge”: “http://static

Generate the POJOs for your data

Now for the fun part, using the sample data we just captured we are going to auto generate the POJOs that represent that data. Use jsonschema2pojo and be sure to enter in your desired package name, class name, JSON as the type, and use primitive types. For this example the generator got confused because the root of the JSON is an array and not an object; so I only pasted in the first element of the array. Some fiddling may be necessary.

POJO-Generator

Integrate the Generated POJOs Into Your Code

With the POJOs generated now we can just paste those generated classes into our project. In my sample project they are in the models package.

Use Retrofit to Download/Parse the API

Create a Rest Adapter

Creating the adapter is as simple as setting the endpoint:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint("http://api.justin.tv/api")
    .build();

Define the APIs interfaces

Define the interface for each endpoint you are going to connect with. In this case we are connecting to the steams endpoint with the limit and offset parameters which are used for paging the data. These are specified in the Justin.Tv API documentation.

public interface TwitchTvApiInterface {
    @GET("/stream/list.json")
    void getStreams(@Query("limit") int limit, @Query("offset") int offset, Callback<List<JustinTvStreamData>> callback);
}

You will notice we are expecting back a response from the endpoint that is a List of JustinTvStreamData objects, which is the POJO we auto-generated earlier. For more information on how to define this interface see the Retrofit documentation.

Create the Twitch.Tv Service

Now that we have the endpoint established and the interface defined we need to make the Twitch.Tv service that allows making requests.

TwitchTvApiInterface twitchTvService = restAdapter.create(TwitchTvApiInterface.class);

Hit the API

Making the API request is equally simple.  We just need to use the service we just created.

twitchTvService.getStreams(ITEMS_PER_PAGE, pageNumber * ITEMS_PER_PAGE, new Callback<List<JustinTvStreamData>>() {
    @Override
    public void success(List<JustinTvStreamData> justinTvStreamData, Response response) {
        consumeApiData(justinTvStreamData);
    }

    @Override
    public void failure(RetrofitError retrofitError) {
        consumeApiData(null);
    }
});

What is interesting here in the fact that Retrofit will download and parse the API data on a background thread, and then deliver the results back to the UI thread via the success or failure method. Retrofit also supports just downloading on whatever thread it is called on (not shown here).

Do Something Interesting With the Data

Now that the data is in a POJO do something interesting with it!
For this demo project I display the Twitch.Tv channel image and description and used the Picasso Library to download and cache the images.

TwitchClient

References

Open Source Sample Project

Interesting? 

Do you find this sort of work interesting? Do you love quaint Bucks County Pennsylvania? Are you an amazing Android Developer? Then we would love to hear from you!

Android Tricks and Tips: Storing a POJO Into Shared Preferences

Often you will yourself with the need for storing some simple persistent data in your Android application. Maybe you are tracking the last time you showed a user an upsell dialog? Perhaps you want to know if this is the first time onto a screen so you can show some new user experience flow.

Shared Preferences to the rescue! Sweet! All I need to do is store my Plain Old Java Object into a shared preference key and we are all set. So you open up the interface document for the Shared Preference editor class to see which method to call.

SharedPreferences Editor Interface

Wait a minute? I only see interfaces for basic Java types? No support for Parcelable or Serializable? Looks like there is no way to just store a POJO into Shared Preferences. Or is there…

GSON to the rescue! GSON is a beautiful Java utility library that allows converting to and from JSON and POJOs.

Saving to the Shared Preference

  1. Integrate the GSON library into your project. (Just download the jar and drag it into your libs folder)
  2. Create the POJO
  3. Serialize the POJO into a JSON String using GSON
  4. Save the serialized POJO into your shared preference

Restoring from the Shared Preference

  1. Read the shared preference value into a string
  2. Using GSON convert the serialized string back into your POJO

Time for Some Code!

First lets take a look at our class that we want to save and restore from a Shared Preference. What is important to notice is we have encapsulated the serialization and deserialization into the class.

public class MyData {
    private String mString1;

    private Long mLong1;

    private Double mDouble1;

    public MyData(String string1, Long long1, Double double1) {
        mString1 = string1;
        mLong1 = long1;
        mDouble1 = double1;
    }

    public String serialize() {
        // Serialize this class into a JSON string using GSON
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    static public MyData create(String serializedData) {
        // Use GSON to instantiate this class using the JSON representation of the state
        Gson gson = new Gson();
        return gson.fromJson(serializedData, MyData.class);
    }
}

Now lets take a look at the code to store and retrieve the POJO from the Shared Preference.

String PREFS_NAME = "MyPrefName";
String PREFS_KEY = "MyPrefKey";

// Create our data object
MyData myData = new MyData("thing1", 5L, 2D);

// Serialize the object into a string
String serializedData = myData.serialize();

// Save the serialized data into a shared preference
SharedPreferences preferencesReader = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferencesReader.edit();
editor.putString(PREFS_KEY, serializedData);
editor.commit();

// Read the shared preference value
String serializedDataFromPreference = preferencesReader.getString(PREFS_KEY, null);

// Create a new object from the serialized data with the same state
MyData restoredMyData = MyData.create(serializedDataFromPreference);

Interesting?

Do you find this sort of work interesting? Do you love quaint Bucks County Pennsylvania? Are you an amazing Android Developer? Then we would love to hear from you!

A Tale of Migrating From Eclipse to Android Studio

I have recently done the unthinkable and taken the plunge into full time use of Android Studio over Eclipse for my Android development. This was not willingly mind you, but as a necessity since the project I was to work on was using Android Studio along with its Gradle build system.

After several months of using Android Studio as my full time IDE I have just one thing to say:

I will never go back to Eclipse

Through the years Eclipse has treated me fairly well and is a robust IDE that I have become quite expert at, but anyone that has used it for any length of time for Android development is aware of some of its shortcomings:

  • It has a tendency to get slower and crash more often with each successive release
  • In order for it to run for more than a few hours you need to edit the config files to give it massive amounts of RAM
  • It tends to run out of memory and crash when you are trying to export an APK
  • It requires a massive amount of CPU power to use it without getting a please wait beach ball every few seconds. I built a quad core 4.5 GHz machine just to make it usable as my daily driver.
  • Theme support is fairly mediocre, there are plugins to add support however none of them theme anything but the code window.
  • Creating UI for Android is extremely clunky
  • Anytime you touch a file outside of Eclipse you need to tell it to refresh itself. Anyone that works on multiple code branches every day will quickly realize it is very easy to forget to do this.
  • There is a mysterious need to constantly “clean” the project. Changed a file not using the editor? Better refresh then clean. Modified just an XML file but not any code files? Better clean or that XML modification probably won’t make it in the build, or worse yet the string values will all offset by one from what they should be.

Welcome our new robot overlords

Android Studio is a welcome change that addresses many of this laundry list of issues in Eclipse and installation is as simple as downloading and dragging it to your applications folder. The first thing you will notice is you can now theme the entire program and it looks beautiful!

Android Studio

Some key improvements

  • Full theming takes away much of the eye strain I was getting with Eclipse
  • Very stable and does not leak memory forcing constant reboots
  • The UI creation tools are vastly improved
  • No refreshing! The editor automatically updates itself whenever files are changed outside of the editor. No need to refresh every time I change branches.
  • There is no need to constantly clean my projects!
  • Command clicking on Android related items actually works. For instance command click on an android id and it opens the XML file it is defined in.

Stability

Android Studio has multiple builds in the wild including canary, dev, and beta. I do not recommend running the canary build as it tends to be quite buggy. I recommend running the dev build. After running the dev builds for over four months now I can say that it is a much more stabile development environment than Eclipse. Performance is good and I have never had to close and reopen the program because it was slowing leaking more and more memory and getting slower and slower.

For the Eclipse Convert

An incredible feature is the ability to set your hotkeys to the same that are used in Eclipse. For myself, the main reason I had held out was an intense fear of having to relearn all of the hotkeys I use daily.

UI Creation is Better

Creating UI in Eclipse is painful; not only do you have to constantly toggle between the XML tab and the rendering tab to check your work, but you have to do it twice every time because of a bug where the first time it doesn’t always render correctly. Android Studio UI In Android Studio you can actually see the UI rendered as you type the XML. Not only that, but you can click on the fields in the rendering and jump right to the XML. Additionally there is support for design time attributes which allows you to add things like text in each of your TextViews to aid in designing your XML. No longer do you have to design everything with canned data so its not invisible only to have to delete it before you actually commit your code.

Gradle Gradle Gradle

The biggest hurdle to overcome with your transition to Android Studio is that is uses a different build environment in Gradle. While there is absolutely a learning curve here most of the common questions have already been answered on StackOverflow. In addition Eclipse has a new option to aid in migrating from Eclipse to Gradle and migration is generally pretty straightforward.

The first time you try to add a library project or jar file you will need to relearn the process, but once you have it really is pretty trivial. One drawback is many open source libraries do not have Gradle support yet so you will need to use Eclipse to export and add Gradle support. While you are at it why not upload them back to the source repo? Its a great way to move things forward and get some easy Github cred.

Gradle has some real advantages over what you are used to in Eclipse:

  • The build process is scripted, meaning you can add useful dynamic features to it
  • Adding a GUI toggle for build variants is pretty easy. Want an internal build and a store build? An Amazon and Google build?
  • You can export APKs with the version number in the filename
  • You can run a script when your APK is created; for instance to automatically upload the APK to TestFlight for internal testing

Interesting? 

Do you find this sort of work interesting? Do you love quaint Bucks County Pennsylvania? Are you an amazing Android Developer? Then we would love to hear from you!