Tuesday, February 19, 2013

Starting Is Easy

Do you know what the best part of a project is? 

Its the beginning. In the beginning the project is bold, wonderful, exciting, challenging, intellectually stimulating. It's a new thing that barely exists except in your mind. You sit in your chair and you can see the whole complete picture  buzzing along doing all the things you want it to do. Think of the users! Their lives will be made so much better by it! People will be impressed at this technological miracle you have birthed. 

This fantasy is made possible by a combination of energy and excitement brought on by a change to your status quo and the blind faith that what you make will be as wonderful in reality as it is in your head. 

You break open your trusty text editor, fire up a new repo, and file new project that idea right into existence. Now your idea has become a little more concrete and now instead of being purely in your head some of it is real and tangible. Sure there's not much there but its something you can point to as proof it is more than just an idea. It even has the name you've given it. 

Now a feeling of invincibility is sweeping over you. You think to yourself "I'm going to do this the right way. I'm not going to take all of those short cuts in the name of deadlines. I am going to make something that I am proud of."

And so you do...

Tests are written. Well designed interfaces and models are drawn up on your whiteboard. Levels of abstraction seem so clear and obvious. 

Now reality begins to creep into your project, and reality is a cruel mistress. She brings with her a cold, bright light that follows her illuminating every dark dirty corner of everything she touches. 

A test is failing.

Good! The tests are supposed to fail at first. A failing test is like a to-do. It tells you that you need to write some code to make that test pass, no problem. 

Ten minutes go by and you have made no progress. This is difficult. You think to yourself, "I have good momentum that I don't want to break. I'll come back to this." Your momentum seems unprecedented. You could easily graph the number of tests you need to get this working and see the rate at which you are making the tests pass and you will have this whole project done in record time. A genius you are! Everyone else are such lazy fools to not complete all of their projects at the rate you are finishing.

The next day comes, starry eyed and barely able to sleep because your mind is racing with thoughts and energy to pour into the project. But that test is still failing. 

Progress continues, but it doesn't seem to be at the rate it was yesterday. Yesterday you were invincible. Today suddenly frustration seems close at hand.

Come to think of it that interface isn't going to work out like you hoped. 

And your models aren't as pure and well thought out as they seemed yesterday. You are violating some clear design principles but it would mean so much extra code to make them conform and work "the right way." If it wasn't enough, Super Awesome Framework you are using to build this with has some abstraction leakage and quirks that force you to write ugly work arounds.

Soon you are left questioning why you are working on this. The enthusiasm and intrigue towards the project are sapped. Problems with the interface keep coming up. There are use cases that no one seemed to think through that have the potential to clutter your interface just to get it working. 

After awhile, the deadline, real or imposed, is rearing its ugly head. 

This function is so simple and obvious, it must work right? Certainly you will save yourself some time by not writing tests against this obviously working code. After all, you're a genius remember? One of those 10x programmers? 

Shortcut after shortcut is taken. Ideals are traded for pragmatism. Technical debt is accrued and you are left wondering just how proud you really are of this creation. Is this something you want to point to and have people remember you by? Would you forever engrave your name into this? 

As reality takes its toll on the project, the weight and burden of the project on you personally begins to grow. It will cause you to constantly reevaluate what you are doing and how you are spending your time. Is it really worth it? Is it really worth all the time and energy you are putting into it? If you put it off for a week will it still be as important and worth a continued time investment?


Will it ever be complete? 

The stakes of the project have the greatest impact on whether or not it will. If you are doing this for a business that is counting on increased customer satisfaction and/or improved revenue, that business will ensure that one way or another this project will be completed no matter how late it is. 

For personal projects it may never get this far. Was this an interesting academic exercise you wanted to show off on Github? Was this something you were trying to make for your own personal use, or were you making it with the intent that someone else could directly benefit? Is this something that you know quite well will most likely never see the face of a real user?

Of course everyone says to just start. Starting is the hard part right? Once you start the hard part is over isn't it? Not so Kimosabe.

Starting a project is easy. Starting just means showing up and taking one step. Finishing means taking thousands of steps, mile after mile, pounding the pavement until your feet are blistered, your body aches, and sweat drips from every pore in your skin. Starting is easy. Finishing can be the most difficult thing you've ever done.

Saturday, February 16, 2013

My Productivity Advice


Computers might very well be both a blessing and curse to our society. The Internet has improved communication thousands of times over and with continuous improvements in speed and design it seems to be improving exponentially.

That is for some definition of improving. As a software engineer working with largely network-based systems, I am right at the forefront for all of this. My day job consists of building faster and more innovative ways to make you, the lowly consumer, buy more stuff you don’t need.

But since something has to put food on the table, and I actually do enjoy it, I find myself in front of a computer far more than eight hours a day typing, reading, scrolling, and reading some more. I always have at least one web browser open, ten chat windows, a code editor of some kind, and a terminal window. For all of us desk jockeys toiling away at our networked computer screens, the siren’s song of information is never more than a click away.

Have you ever thought about why you keep finding yourself wanting to check Facebook? Or why you know you shouldn’t but you find clever ways to rationalize reading the latest tweets in your feed? It’s all about dopamine. Whenever you go to check for some new information whether it be on Reddit, Facebook, Twitter, Hacker News, or any of those other ugly time sucks your brain releases a hit of dopamine as soon as you read something new. This is not unlike chemically what happens in your brain when you take a hit of cocaine.

As you check more of this information over and over you begin to crave more and more. It becomes harder to focus on a given task for longer than a few minutes. If go to work, maybe spend 10-20 minutes catching up news, the front page of Hacker News, and your Facebook feed, then continue to check back in on these things over and over throughout the day you may vary well find that you have easily spent up to half or more of your entire working day on suckling from this fire hose of a teat.

We know its wrong, but yet we still do it, over and over, day after day. It’s a not unwarranted joke that when Reddit goes down productivity among white collar workers improves.

I’m here to say there is a better way. I’m here to inform you life does not have to be like this. I’m here to tell you that you can make a measurable improvement to your happiness and quality of life by tackling this wretched demon.

Thankfully, the alcoholics from before our time have given us some tips to rid ourselves of this beast.

The first step is to admit you have a problem. I know I do. I am weak and powerless in front of these rationalized dopamine dealing drug peddlers.

The second step is to quit cold turkey. At this point you are probably skeptical of me. I was you at one point. I understand your apprehension towards this idea. I challenge you to try this just for two weeks. At the end of those two weeks you can go about your business gorging on a backlog of tweets, likes, updates, and posts.

But before we go further I’ll tackle some of your skepticism head on.

But the Pomodoro technique….
No. This is not well suited to tackling this problem. This is best suited for the manager facing a barrage of emails, phone calls, and face to face interruptions that prevent him or her from doing meaningful work in the day. Do not use the Pomodoro technique as a rationalization to allow “just a little hit.”

I’ll just limit myself to only check these things once or twice a day.
No you wont. You must admit you are weak. Checking at all will do to your mind what Buddhists describe as “stirring up the muddy water.” You need to let the dirt and debris in your mind settle, and taking in any of this pointless information is going to leave you just as bad and unfocused as when you were wasting half your day doing it. Plus, from personal experience, you will relapse.

But I will miss out on all kinds of important information.
Doubtful. In todays super connected day and age if you live in a city of any size and talk to at least one person a day you will most likely be unable to miss current world events. Someone will tell you about it, you will catch a glimpse of something on a TV on in a restaurant, you will see the headline on a newspaper as you pass by. All other information is just noise.

I’ll assume at this point you have given up your rationalization of the issue and have given in to the idea that quitting cold turkey is the only way. I will now give you a step-by-step process to help.

  1. Turn off notifications on your phone for all possible apps. Email, Facebook, Twitter, Foursquare, Farmville, all of it goes. My only exception to this is that during my on-call shift I do temporarily turn on work email notifications. Otherwise, no exceptions. While you are at it, just uninstall everything except e-mail. People who matter will call or text you. People who don’t matter will Facebook message you.
  2. Turn off your TV. In fact, cancel your cable subscription right now. Even if you take nothing else away from this blog post cut the cord and don’t look back.
  3.  Limit your email. I recommend only checking twice a day at most. Email is a necessary evil because it is so ingrained in our day-to-day communication. The best you can do is keep Outlook, iMail, or Gmail closed except for dedicated times during the day. While you are at it, spend this time really reading what people are sending you instead of just skimming it, and take more time to write thoughtful messages when you have a need to respond to someone.
  4. You will find yourself with huge amounts of time you don’t know what to do with. Previously you would get bored for a second and use that moment to check your email, or twitter, or whatever. You will need a replacement activity or you will relapse.


a.    I too am a smartphone user, and I highly recommend downloading the Amazon Kindle app, and putting together a reading list of books. Anytime you find yourself with a few spare minutes you would otherwise check the latest Reddit, read a few pages out of your current book.
b.    Take up meditation. I will leave this as optional, but I highly recommend it. There is no real wrong way to do it, the simplest is to sit somewhere comfortable, close your eyes, and focus on your breathing. For beginners, its recommended that you count on the inhale of every breathe from one to ten and restart at one any time you notice your mind drifting onto something else. Eventually you start to embrace any moment of silence because it gives you an opportunity to practice meditation however briefly and stay more in the moment.
c.     Spend time with people. Talk to them in person. Be they friends, family, coworkers, or strangers on the street. Real people are way more interesting than self satisfied strangers online.
d.    At work, if you find yourself with small periods of time where you don’t know what else to do and would otherwise check out the latest unprofitablestartup.io on Hacker News, take initiative and think for a second why you are in this state. Why do you not know what to do next? Are you blocked by someone else? Go find them and figure out what needs to be done to get them moving. Do you not have anything to work on? Check with your manager. Is your manager or not around or do you not have one? Work on that thing you talked about staying late and fixing a few weeks ago.
e.     For longer periods of time outside work, filling this time hole becomes more challenging. Much like you have a list of books you are reading, create a list of things you want to work on. This can be as small and simple as a household chore you have put off, or it may be learning a new language, writing a book, or writing a blog post about how much better your life is now that you rid yourselves of those evil time suck websites.

After your two weeks are up, take five minutes and reflect on how they have gone for you and your state of mind. Have you been more productive at work? Have you done things you don’t normally do? Have you finally gotten around to doing something you have been putting off for a long time? At this point you should make a decision whether or not to continue your self imposed exile from the online world or return to your procrastinating ways.

Here is my dirty little secret. I’m not perfect. For the last five months I’ve been alternating periods on and off the grid. I generally spend about three weeks off, than one to three weeks on. Mostly this is just because I am weak and there is always something that comes up that sucks me back in. Honestly I do find that during my period of time offline I am happier, I am more productive, and my life is generally improved. Eventually I hope to have the willpower to stay in my cold turkey state indefinitely, but like an arctangent approaching an asymptote I may forever more forward without ever fully reaching my goal. Better to try and fail than to not try at all.


Friday, July 22, 2011

Frustrations With Data Access on Android + Intro to OrmLite

Working in the .Net world during the day and doing some Android development in my spare time has a tendency to bias my opinion against Java. Don't get me wrong, I'll take Java over C/C++/Objective-C any day, but the lack of lambdas and delegates can get frustrating when you are spoiled by .Net's recent trend towards functional programming. This really becomes noticeable at the data access layer of Android applications where the API's for interacting with the onboard SQLite database can best be described as adequate.

Thankfully API problems, unlike core system architecture problems, are easily overcome by open source software and your fellow developers experiencing the same problems as you. 

After having just about enough inline SQL to drive me crazy I decided to investigate what Object Relational Mappers (ORM) are available for the Android OS. Since Android is based on its own implementation of the Java Virtual Machine there are a lot of Java applicable tools out there. 

The first and most popular you may run across when looking around is Hibernate. Quite possibly one of the older and more popular open source ORM's available, it was originally built for larger Java applications than a simple Android app. 

Another common one you will find is OrmLite. OrmLite is designed to be much more lightweight and smaller scale than Hibernate. This means that it does sacrifice some of the power that Hibernate has, but instead has simplicity and very small Jar files to add to your project.

Because of this I have arbitrarily started exploring OrmLite as my choice for Android development. Later on I may decide to go with Hibernate, but for now its OrmLite. 

So lets say you have run into this same problem and have decided that you too are sick of endless Cursor manipulation and are ready to spend more time writing Android apps and less time praying that the rawQuery statement you just wrote is valid SQL syntax.
  1. The first thing you need to do is add the Jar's to your project. Go to the OrmLite website and download the ormlite-core and ormlite-android jar files. 
  2. Add these to your project. Assuming you are using Eclipse, right click on the project name and choose Properties. Then under Java Build Path->Libraries choose Add External JARs... and select the two Jar's that you just downloaded.
  3. First you want to create the model class that you are going to be working with. OrmLite relies on Annotations to make sense of your class. Use the @DatabaseTable annotation to mark your class as a table, and the @DatabaseField annotations to mark your properties as fields. Here is an example of a super simple data class. In it you can see that I set the id field to be the primary key for the table:

    import com.j256.ormlite.table.DatabaseTable;
    import com.j256.ormlite.field.DatabaseField;
    
    @DatabaseTable(tableName="data")
    public class Data {
     
     @DatabaseField(id = true)
     private long id;
     @DatabaseField
     private String value;
     
     public Data() {
      
     }
     
     public Data(long id, String value) {
      this.id = id;
      this.value = value;
     }
     
     public long getId() {
      return this.id;
     }
     
     public String getValue() {
      return this.value;
     }
    }
    
  4. Now you need to create your database helper class with the verbosely named OrmLiteSqliteOpenHelper. If you have created SqliteOpenHelper classes before, this should feel very similar. Setup your constructor and override the onCreate and onUpgrade methods as you would a normal SqliteOpenHelper. One important note is that OrmLite relies on Data Access Objects to act as an intermediary layer on top of your database. This is what abstracts away the details you didn't want to deal with when you decided to go the ORM route. Because of this, it is recommended to give yourself a public method that returns yourself a Dao object specific to the type you are working with. The Dao is a generic class, where the first type is the type of the model you want to work with and the second type is the type of the property you marked as the id in your model. In my example I used a long for the id so I create a Dao<Data, Long>, but if you hadn't set an id property you should be able to use the Object type. Example class:

    import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
    import com.j256.ormlite.dao.Dao;
    import com.j256.ormlite.support.ConnectionSource;
    import com.j256.ormlite.table.TableUtils;
    
    public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    
     private static final String DATABASE_NAME = "test.db";
     private static final int DATABASE_VERSION = 1;
     
     private Dao mDao;
     
     public DatabaseHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
         mDao = null;
     }
     
     @Override
     public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
         try {
             TableUtils.createTable(connectionSource, Data.class);
         } catch(SQLException e) {
             Log.e(DatabaseHelper.class.getName(), "Can't create database.");
             throw new RuntimeException(e);
         }
     }
     
     @Override
     public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
         try {
             TableUtils.dropTable(connectionSource, Data.class, true);
      
             onCreate(db, connectionSource);
         } catch(SQLException e) {
              Log.e(DatabaseHelper.class.getName(), "Can't drop databases.");
             throw new RuntimeException(e);
         }
     }
     
     public Dao getDataDao() throws SQLException {
      if (mDao == null) {
       mDao = getDao(Data.class);
       
      }
      
      return mDao;
     }
     
    }
    
  5. The easiest way to start using your DatabaseHelper from your Activity is to create an OrmLiteBaseActivity. This gives you a few added methods to simplify things for you. For example it gives you the getHelper() method that returns an instance of the DatabaseHelper you wanted to work with. Since you went ahead and created a public method to return yourself and instance of the Dao you also now have an easy way to get at a Dao for doing your data manipulation. Example class:

    import java.sql.SQLException;
    import java.util.List;
    
    import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
    import com.j256.ormlite.dao.Dao;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class Main extends OrmLiteBaseActivity {
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            try {
         Dao dao = getHelper().getDataDao();
                Data d = new Data(1, "Hello World");  
         dao.create(d);
     } catch (SQLException e) {
         e.printStackTrace();
     }
      
     try {
         Dao laterDao = getHelper().getDataDao();
         List result = laterDao.queryForMatching(new Data(1, "Hello World"));
                TextView tv = new TextView(this);
         tv.setText(result.get(0).getValue());
         setContentView(tv);
     } catch (SQLException e) {
         e.printStackTrace();
     }  
            
        }
        
    }
    

Saturday, February 5, 2011

'unicode' object has no attribute '_meta'

It appears I have run across another annoying problem for which there is a lot of contradicting information. I'm working on a Django project and I ran into this error: 'unicode' object has no attribute '_meta'. What is really important is the context in which I received the error. There are a lot of different possible culprits and an even larger number of possible fixes.

The context within I was receiving this error was in a view function that was "supposed" to be returning a list of strings in json. If you have any experience with returning Django queryset results in json you probably immediately think of doing something like this:

#Not what you want!
from django.core import serializers 
from django.http import HttpResponse
from myproject.myapp.models import MyModel

def get_my_strings(request):
    some_models = MyModel.objects.filter(user=request.user)
    my_strings = []
    for s in my_strings:
        my_strings.append(some_models.some_string)
    #error will be thrown
    my_strings = serializers.serialize('json', my_strings) 
    return HttpResponse(my_strings, mimetype='application/json')

This is wrong.

What you actually want is to use the simplejson.dumps function to serialize your data.  Here is the correct form:

#Import simplejson instead
from django.utils import simplejson 
from django.http import HttpResponse
from myproject.myapp.models import MyModel

def get_my_strings(request):
    some_models = MyModel.objects.filter(user=request.user)
    my_strings = []
    for s in my_strings:
        my_strings.append(some_models.some_string)
    #Corrected!
    my_strings = simplejson.dumps(my_strings, indent=4) 
    return HttpResponse(my_strings, mimetype='application/json'

Tuesday, December 14, 2010

Unable to build: Could not find dx.jar file

I got to update to the latest version of the ADT tonight when I hit the weirdest error out of the blue:


Unable to build: the file dex.jar was not loaded from the SDK folder


After way too long of Googling and not coming up with an answer I finally found it on some really obscure mailing list archive site.

Solution:
Look in the project with the error and delete the default.properties file.  That is it.

Just make sure you have the latest versions of Eclipse and ADT when you do it, and that should be it.  Back to Android programming...