Book Review: Who Moved My Cheese?

As I’ve been thinking about my career I realize I have seriously neglected the business side for many years.  As part of my quest to learn more about the business side I’ve been finding lists of the supposedly best business books, adding them to my Amazon wish list, finding them at Half Price Books and buying them there, and then reading them.  I know my brother has been doing the same thing and I know if both of us are doing it, there are many other people who are as well.  This is what has gotten me to write a review about the books I’m reading in my copious amounts of free time (haha).  Anyway, this wonderful little ditty Who Moved My Cheese? by Spencer Johnson, M.D. has quite a following both good and bad.  Amazon’s rating (3.3 / 5) seem to show it’s split right down the middle. Some people have said it has changed their life and others view it as trite simplistic mush not even worthy of a first grader.  And, spoiler alert, I fall into the latter camp — I’m not a fan of the book.  I’m glad I got it at HPB for $4.99 cause at it’s $19.95 MSRP it’s a complete rip-off and even at $4.99 I was a little ticked off.  I will give Dr. Johnson props for making a boat load of money off this book of common sense.

I won’t bore you with the fable, but just know it involves Sniff, Scurry, Hem, and Haw as the characters and THE MAZE as the place they live and find Cheese.  If you have just a little bit of imagination you’ll know right away that

  • All four characters find cheese
  • When the cheese disappears Sniff and Scurry leave right away and don’t complain
  • Hem and Haw stick around complaining (obviously) until one realizes they need to be like S&S and find new Cheese in the scary maze
  • Eventually the other one decides to venture out from the original Cheese station and finds everyone else happy

Throw in some nice banal quotes about Cheese (metaphor for money or happiness) and you’ve got a best selling business book.  I’ll save you the money and give some real advice:

  • Things change, get over it
  • No use in crying over the change
  • Be sensitive to changes in the workplace, note what they are and start making plans
  • Always be looking for the next big opportunity
  • Leave on your timeline, not the business’ timeline for you
  • Spend the $5 it’d cost you for this book at HPB on a beer and the $20 it’d cost you for the full thing on a decent meal.  It’d be money better spent.

Running command-line programs from inside Java

I’ve been writing a program for a class I’m taking at school to automate some of the work that had to be done.  I could have written it as a shell script, but I wanted to be more adventurous and write it in Java so it could be run in more than just a Unix environment.  So, as you can guess, I had to do execute the program from the command line.  Normally, you’d do this with the exec method off of the Runtime, however, this causes some problems with permissions.  The way to do execute something from the command line is to use the ProcessBuilder object and it’s dead simple.

List command = new ArrayList();
command.add("file_to_execute");
command.add("option1");
command.add("option2");

ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();

process.waitFor();

As you can tell looking at this simple example, command is a list of String which becomes the command you’d normally type into the command line interface. The processBuilder object sets all the wonderful things up to run an operating system process and when you specify start() it turns it into a Process where you can then wait for all the return value.

Of course, there is a lot more to it than just this, but for my case, all I needed was a quick and dirty command to be run and this did the trick.  You can read more about it on StackOverflow.

Testing in Java: Part I – Introduction

As I’ve alluded to, this is the first article in a series to walk through the process of testing an application for production.  We’ll be making a simple REST interface web application that connects to a back-end database.  It will do some basic CRUD operations and I’ll show how I’d test each layer of this application.  Hopefully, you’ll find some of these techniques useful and able to apply them to your own work.  You can download, fork, or view the entire code base from the GitHub repo.

A typical web application has a few tiers: user interface, business logic, and data access.  In the case of this application I’m following the same idea, but have broken out the objects that are being modified into a domain-objects package inside of the project.  This is where we’ll start this series — talking about the objects and how to test them. I’m going to break the pure TDD-style for this series and write an object first, then write a test :).

In an application where you’re dealing with objects that the user is performing a CRUD operation upon you’ll want to think about these first.  The initial design would consist of determining what each of these objects are and create an interface and class embodying those concepts.  In our case we have a simple item that consists of a name and a unique identifier.  I’ve named it: MyItem.  Below, you’ll see the interface for MyItem along with a simple default implementation, MyItemImpl.

package com.laneholloway.unittestingexample.domain;

import java.util.UUID;

public interface MyItem {
    public UUID getGuid();
    public String getName();
}
package com.laneholloway.unittestingexample.domain;

import java.util.UUID;

/**
 * A simple default implementation
 */
public class MyItemImpl implements MyItem {
    private final UUID uuid;
    private final String name;

    public MyItemImpl(String itemName) {
        this(UUID.randomUUID(), itemName);
    }

    public MyItemImpl(UUID guid, String itemName) {
        this.uuid = guid;
        this.name = itemName;
    }

    @Override
    public UUID getGuid() {
        return uuid;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (o instanceof MyItemImpl) {
            MyItemImpl that = (MyItemImpl) o;
            if (that.getGuid().equals(this.uuid) && that.getName().equals(this.name)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}

Now that we have the default implementation, we need to have a test for how we want the item to behave within the system. In our case, if the id and the name are the same, the objects are equal. If either is not equal, the objects are not equal. To verify this, we can write a test class using JUnit4. Looking at the code, you’ll notice that I’ve written two of the three tests and I’ve left the last one as an exercise (it really isn’t that hard :)).

package com.laneholloway.unittestingexample.domain;

import com.laneholloway.unittestingexample.domain.MyItem;
import com.laneholloway.unittestingexample.domain.MyItemImpl;
import org.junit.Before;
import org.junit.Test;

import java.util.UUID;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Simple tests to show off the MyItemImpl
 */
public class MyItemImplTest {
    private MyItem myItemOne;
    private MyItem myItemTwo;
    private MyItem myItemThree;

    @Before
    public void setup() throws Exception {
        UUID testId = UUID.randomUUID();
        myItemOne = new MyItemImpl(testId, "test");
        myItemTwo = new MyItemImpl(testId, "test");
        myItemThree = new MyItemImpl(UUID.randomUUID(), "test");
    }

    @Test
    public void if_uuid_and_name_are_the_same_items_are_the_same() throws Exception {
        assertTrue(myItemOne.equals(myItemTwo));
    }

    @Test
    public void if_uuids_are_different_the_items_are_different() throws Exception {
        assertFalse(myItemOne.equals(myItemThree));
    }
}

Now, pat yourself on the back, with these three classes you’ve created the object that is being modified by the program and tested it to make sure the implementation behaves as expected.

From here, we will examine the more complicated data access layer, then the business logic, and finally, the REST layer.

As always, if you have any comments, concerns, edits that I should make, let me know in the comments below.

Programming and chairs

Recently the handle on the back of my Humanscale Freedom chair broke and got me really annoyed.  How could a piece so small that I never touch just break the first time I use it to pull the chair?  After getting over the annoyance of broken, sharp plastic, I emailed Humanscale and they sent me a free replacement handle.  Now, you’re wondering why I’m talking about a broken handle on a chair, and I’m getting to the meat of the post.  It made me think about how important the right desk setup and chair is when you’re programming.  You’re spending over eight hours a day in this chair.  Why would you settle for something that is so uncomfortable?

Many people, have written and debated about the perfect programming chair and I think it’s a worthwhile investment.  Granted, you can’t find Herman Miller Aeron chairs for $100 like you used to during the dot com bust but a really good chair can be found for a reasonable amount.  Personally, I’m a fan of the Humanscale Freedom with a headrest.  It wasn’t the cheapest, but it is by far the best investment I’ve made in my career as a programmer.  The ability to stand up after a coding marathon and feel like you just sat down is a wonderful feeling.

If you can’t afford or don’t want to spend money on a chair, think about setting a time and getting up and moving around for 10 minutes every hour or converting your sit-down desk to a standing desk so you can at least move around a little during the work day.  Your body and your mind will thank you for it.

See some of my code on GitHub

Just a quick update, I’ve decided to publicize the fact I’m using GitHub to store all the example code I’m creating for my website articles.  You can find the links to the repositories in the articles themselves or you can just see all the things I’m working on by going to my user page on GitHub.

You’ll see the unit-testing-examples repository if you poke around my repos.  This is going to be the basis for one series of articles about unit testing, programming, and designing a simple application you’d see in production.  I don’t want to give away too much but I think the articles are going to be great. 🙂

…and back to your regularly scheduled website

Sorry about that down time, apparently, a little billing mishap occurred and my site went down for a few days.  It’s amazing what happens when emails aren’t sent to your correct addresses.

And since I’m talking like I’m in a commercial, I’ll give you a preview of what to expect on Lane Holloway dot com in the coming months.  First, more content about programming process and design.  Second, more content about programming in Java and Scala and, if I get the time, Clojure.  Thirdly, I’ll talk about some of the newer things out there like “Big Data” (gotta have the air quotes).  Fourth, since I’m taking a class about SAT solvers, I might work up some articles on SAT solvers and some of the issues surrounding them.  It’ll help me understand them more and perhaps, give you, a chance to get your feet wet with them.  Lastly, I don’t know, I’m sure I’ll find something interesting to write about.

Anyway, coming up next will be some articles about programming.  I’ve got a few in the pipeline for y’all. 🙂