This will be the first in a series of posts where I document the process of writing an iOS 5 app using Xcode. I've been playing around here and there for about a year. I want to take it to the next level though. I'm going to write an app around storing, tracking and sharing home-brewing recipes.

As I write my app, I'm going to move through the decision-making process, high-lighting what I'm thinking about and why I'm making the decisions I'm making.

I've obviously already made a couple decisions so I'll go ahead and document those here.
  1. Why a home-brewing app? Because I love home-brewing. :-) (Even though I don't do it anymore.... see: the size of my NYC apartment.) I also think there's a gap in the market here... be it a somewhat niche market.
  2. Why native iOS? Well, my current 9-6 job is native Android development. I want to explore something outside of this. I thought about doing an HTML5 or jquery-based mobile website. And, I appreciate the fact that you reach more users this way. It's just that I'm less interested in exploring these right now. I want to play around with Xcode, especially some of the latest features.  
So, off we go! 



Ahem... In business enterprise software. I will put forward the case that 95% of the time there is an alternative to a design involving abstract classes that will leave you with DRY, well-tested code and without the inherent risk that comes with inheritance. (That risk being a big jumbled spider web of parent-classes and subclasses that inevitable happens after x number of developers touch it.)

Back when I learned how to do OO in large codes bases.... Oh those 6 long years ago... I worked in London and was influenced by a few very awesome developers. We were doing C# and I remember this team where the leaders said 'No abstract classes.' And the code was beautiful. It might be said that we all just really cared. But, I have worked on very few teams where the majority of coders didn't care. We always care and strive for beautiful code. Anyway, today I find myself back in the US and working in the Java world, doing android development. And I find abstract class use everywhere. Everyone embracing it. And honestly, I see it lead to a tangled mess 95% of the time.

I see the appeal. You reduce duplication. If you were to use an interface instead of an abstract class, you would have several classes implementing the same interface but what about the common functionality. That's what an abstract class gets you. You can pop the common stuff in the abstract base class.

I rarely see a problematic situation at the time of initialing setting this up. It's normally beautiful. You say 'Yeah! Look at that beautiful inheritance. This is great. No duplication.' DRY, right?

The problem comes when you have to come along later and add a little functionality here or there. Often additional layers get added into the hierarchy and you sprinkle the common functionality where they belong. So maybe you started with 1 abstract class and 4 classes extending. You come along a month or 2 later and there's 1 base abstract class, two other abstract classes extending this and a slew of concrete classes extending any of these 3 abstract classes.

If this doesn't sound scary, maybe it's never happened to you. Let me just overview the pain I feel when working with this sort of thing is.

1. It's nightmare for a developer who's unfamiliar with the codebase. Who's overriding what? Who's implementing what? Who's responsibility is what?
2. It's hard to unit test. Who do you test? Really, you have to test all the concrete classes completely. This includes the functionality they implement themselves and the functionality they get from any of their parents. You could say.. well, write unit tests for the parents and then don't retest in the subclasses. The issue here is that you can't test that the subclass is specifically using the functionality from the parent through interaction based testing (mocking and stubbing). To be sure it's using the right code, you have to unit test the specific functionality for that code.. wherever it is. So, you either have duplicate tests or incomplete test coverage.
3. It's often highly brittle to change. This is because it's inevitable under tested and often not completed understood by those augmenting functionality.

I can't say that inheritance is never right. In fact it often feels right. And if you're working in a small team where you can discuss design frequently catch these things turning into issues... maybe it's worthwhile embracing it more. My feeling is that if you aren't working on a small team.. and/or you have even a minimal level of turnover among you're developers. This problem will almost certainly rear it's head.

What is the answer?? Let's turn back to the things taught by the gang of 4 and that super genius, Martin Fowler.  There must be some wisdom there! I'll dive into this next. I just want to reread a few chapters and articles first.


I've been developing in Android for the last 5 months. Aside from the normal learning curve when learning a new technology, my team and I have found Android's fragments a bit of a headache. The android documentation is great. But there are a few things they don't explicitly mention that I really would have found useful to know or think about up front.

There are two options for using fragments, really. At least when looking at it from a high level. And I didn't realize the benefits or drawbacks of either of these... or the implications.

Option 1- Implement it and manage it yourself. 

This is what I did first. In this sense it's still a sort of miniature activity. 
There are a few ways you can do this.

  1. Define it as a fragment in the layout that your activity inflates.
  2. In doing this, you can inject it into your activity (if you're using roboguice) or pull it out into your activity as you would any other layout component to operate on it or delegate to it.
  3. Use the fragment manager to load the fragment in a FragmentTransaction and then load it into your layout based on some id (within a LinearLayout or something).
This would allow you to toggle the fragment for a given section of your activities layout.

The first way I went was a little of both of these. As it seemed to fit the bill and I didn't know any betting. I have since found that managing many fragments within an activity in the above fashion does not scale well. Also, the thing mentioned in the first option... where you can grab your fragment from the activity and delegate to it. That turns out to also be a mess. Ideally, you want your fragment to live in it's own little world. Without having to talk to it's activity or whoever is holding it.

Option 2 - Use Android components 

(i.e. ActionBar, Menus, etc..) 

What happened in my situation was that the number of fragments I needed on my screen was growing. There was more and more stuff going on and now I needed to manage the interactions myself. There was realization at some point that... 'Hey, those Android docs going on and on about the Android way of doing things... maybe they can help here!' So enter a few useful components.


Action bars and menus manage Fragments for you! You just hook into the behavior they provide and tell them which Fragment and when. It also allows for an experience your user is used to from other Android apps.

This is pretty much the code used in the Android API demos code. But you get the idea...

ActionBar

It's much easier to keep the Fragment separated from the Activity. Thus decoupling and simplifying. For more code I suggest to see the API demos. They're very useful.

Conclusion


As normal, it really depends on what you want to do. If you have a complex interaction between many different components on your screen, I really feel now that using the suggested Android approaches is easiest. If you just need one little fragment... going with the hook it up yourself approach may be best. But knowing that these are the two option paths may help in making the decision.

Branching is definitely one of the powerhouse aspects of Git. This will be a brief post on how to create, checkout and commit to a new branch. And then merge the changes back into your master branch.... Should you need to.

Example. Spiking a potential technical solution to a future, unimplemented feature. (indicated by 'feature_x' below)
Visualizing branching (image from gitguru.com)

$ git checkout -b feature_x
Switched to a new branch "feature_x"

What's happened here is you've told Git to create a new representation of your project off the main development or working branch. Everyone else can continue to work on that branch, commit things to it, push those commits, etc... You can also keep working on that main branch and pulling down updates on it as well.

View all your branches (The star indicates the branch you're currently on):

$ git branch -a
* feature_x
  master

If you're main development branch is called 'master' this is how you move back and forth between branches your two branches.

$ git checkout master 
Switched to branch 'master'

$ git checkout feature_x
Switched to branch 'feature_x'

Now, at the moment your branch is stored only locally on your machine. If you want someone else to be able to view it or commit to it, you need to push it to your remote repository or 'origin'.

$ git push origin feature_x

Ok, let's talk about how you work on this new branch and move things between it and master.

First, check the new branch back out

$ git checkout feature_x

Switched to branch 'feature_x'

Now, write some code.... la la la... write some more code.....

You can see what you've done so far with

$ git status

# On branch feature_x
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# new_file.rb


Now, add the files to the staging area and commit the code into the git directory for this branch.

$ git add .
$ git commit -m"JY - my code changes"
[feature_x 2c4674c] JY - my code changes
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file.rb


In my earlier post, I talked about how the git DB keeps track of your git directory. When you have multiple branches you can imagine that that DB is tracking each different branch as a different git directory. One way of viewing these directories is using 'git log'.

$ git log
commit 2c4674cf17fd0b7a950076023c810e9796400e0a
Author: John Doe <john.doe@gmail.com>
Date:   Fri Sep 28 12:14:57 2011 -0400

    JY - my code changes
.
.
.

You can checkout master again and run the same command there to confirm that it is, in fact, only on your feature_x branch. But one thing to notice when looking at these commits listed under the git log is that each one has unique identifier associated with it. I'll wrap this post up by showing how you can pull commits from one branch to another by using the cherry-pick command. 

$ git checkout master
Switched to branch 'feature_x'
$ git cherry-pick 2c4674cf17fd0b7a950076023c810e9796400e0a
[master efc5626] JY - my code changes
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file.rb 

You can see that that commit has been added to master by doing another 'git log'. Notice that the commit has a different unique identifier. So, it's unique to it's branch within the git directory/db.

$ git log
commit efc562669b7dd993c0a4088144d6f008fde66c0e
Author: Julie Yaunches <julie.yaunches@thoughtworks.com>
Date:   Fri Sep 28 12:14:57 2012 -0400

    JY - my code changes
.
.
.

I've been using git for the last few years... but I've noticed recently that I've gotten markedly better at it. I think that's because I had to. Anyway, I find the clarity and possibilities opened up by understanding it more deeply very cool. So, in furthering this understanding... I will attempt to explain what I see as the core components and differences between it and other version-control systems.

VCS Rethought - Snapshots not file-system changes


Git sort of rethought version-control instead of just using the same technique as it's predecessors. 
Other version-control systems worked based off of file-system changes.... and tracking these changes over time. Git works based on snapshots. Every time you commit, Git takes a picture of what all your files look like at that moment and stores a reference to that snapshot. It only does this for files that have changed, for unchanged files, it just stores a link to the previous identical file it has already stored. I think in understanding this, it's best to visualize Git as a mini filesystem. 

Local Storage - Intelligent, compressed database


Git does almost everything locally. You can diff a file against the same version of it 2 weeks back without a network connection. The reason for this is that it stores everything, all it's snapshots etc... in it's own intelligent, compressed database. You may say... wow, how big does that get? It actually scales  much better in terms of disk space use than other VCSs. The larger/longer the project the bigger the difference. For the Mozilla project, Git is 30x smaller than SVN.

Three Directories, Three File States


Important: Git has 3 main states that your file changes can be in at any time: Committed, Modified, Staged. And, at any given moment 1 file may exist in all three of these states for you to access and adjust. How does it do this? By basically tracking these changes using 3 different directories: the working directory, the staging area, and the git directory

(Borrowed from Pro Git by Scott Chacon)


The git directory - This is essentially the git database that stores all the info and snapshots of your project. This is what you copied onto your local filesystem when you first cloned the remote repository (from Github or wherever) for your project.
The working directory - This is one version of the project that's been checked out from the git directory for you to work on. This is where your initial file changes, additions go. 
The staging area - When you're happy with some work, but not quite ready to commit it, you can stage it. You do this by adding it to Git. [> git add /dir/filename.rb] or [> git add .]