Taking XCode Unit Tests For A Spin

I need to learn Objective-C and iOS development for my current work project.  I’ve tried some tutorial apps in XCode but I always end up doing more copy-paste and reading instead of actually learning by doing and stumbling through.

So I decided to learn out in the open by creating a GitHub project, explore-ios, and try to make a simple tip calculator.  I would’ve done a flashlight app, but a) Apple has shut that market down with the built-in flashlight in control center, and b) I wanted something with some basic logic that I could write unit tests for.  Oh, and c) the UI will have to be crazy simple.

I really love automated testing.  Code without tests makes me…uncomfortable.  Thankfully, when I started a new iOS project and selected the utility app template, I got a test section for free!  It looked promising:

File Explorer with Unit Tests Highlighted

And it comes with a pretty basic test file:

Gotcha #1 – When I went to add my first test of my own though, I ran into my first big “gotcha” – by convention, XCode finds test methods by a prefix convention; all of your test methods have to start with “test.”  This was a tremendously frustrating find, since coming from .NET and NUnit / XUnit testing, I’m used to prefixing tests with “can” or “should” or something similar.  And there don’t appear to be any docs on the subject from my Googling, just some basic references here and here.

You also don’t have to declare your test methods in the implementation section of your test class, which surprised me, but I suspect that will make more sense as my knowledge of Objective-C grows.

So I moved on.  I wrote a test first to take a 20% tip from $20, and in the spirit of TDD just had my new TipCalculator class return a hard-coded 4 before building out the rest of my tests.

Gotcha #2 – Your tests actually require a reloading of your application in the iOS simulator every time, AND the best way to execute your tests is with the ⌘U shortcut as opposed to the Test Navigator UI.

Gotcha #3 – Once I figured out test execution, I hit one last surprise, but this one at least gave me a hint at what the problem was.  In using the XCTAssertEqual operation, you aren’t getting any implicit casting and comparison done for you – if the result is a double, you need to specify the expectation as the same type.  Again, I’m assuming this has nothing to do with XCTAssertEqual and everything to do with the language, but I’m still learning.

The message you get though is quite amusing – I was beginning to doubt my basic understanding of math:

Failure with Implicit Cast

But a quick explicit cast clears everything up:

Success With Explicit Cast

So that’s it!  I hope these gotchas save you the time it took me to get up and running with unit testing in XCode 5.

Hell is a Bureaucracy

 C.S. Lewis's "The Screwtape Letters"

I was particularly struck by this passage in the preface to C.S. Lewis’s “The Screwtape Letters” – a work where Lewis imagines Hell as a bureaucracy of demon tempters and junior tempters who feed off of one another as well as their human “assignments”:

We must picture Hell as a state where everyone is perpetually concerned about his own dignity and advancement, where everyone has a grievance, and where everyone lives the deadly serious passions of envy, self-importance, and resentment.

…Everyone wishes everyone else’s discrediting, demotion, and ruin; everyone is an expert in the confidential report, the pretended alliance, the stab in the back.  Over all this their good manners, their expressions of grave respect, their ‘tributes’ to one another’s invaluable services form a thin crust.  Every now and then it gets punctured, and the scalding lava of their hatred spurts out.

Reading this as a working adult made me shiver a bit, both in how I’ve seen its parallel in real offices with real people, and in how I’ve perpetuated it myself.

It’s easy as a programmer (or as anyone, really) to have this myopic view of the world.  It’s all about me and my small area and everything else is an affront to its survival.  Other people don’t get it, or if they did, they wouldn’t be trying to bring me down all the time.  They’re out to get me.  They just aren’t as good as me.  Etcetera.

Many of the largest technical challenges at companies, in many ways, are breakdowns of broken human nature at scale.  Systems fail or are poorly planned because it’s easier to code than to communicate (i.e., I’m smart enough to figure it out on my own, right?).  Dead processes, products or technologies are defended to the death because they are a part of the kingdom someone has worked so hard to build – and even the thought of changing it brings the subtle implication that the king of that kingdom is wrong.

It’s so easy to lose hope and become disillusioned with both ourselves and the dark bureaucratic machinery at work in almost every organization.  Every organization is made of broken people: us.

Mississippi Missions Trip 04-10 March 2006 (248)

The answer to this is to follow Jesus, and learn from how he approached bureaucracy and hierarchy.  Just look at how he approached his kingdom – though fully God, humbling himself to dirty, menial, beautiful service like in John 13 (emphasis mine):

He had loved his disciples during his ministry on earth, and now he loved them to the very end…He got up from the table, took off his robe, wrapped a towel around his waist, and poured water into a basin.  Then he began to wash the disciples’ feet, drying them with the towel he had around him.

…After washing their feet, he put on his robe again and sat down and asked, “Do you understand what I was doing?  You call me ‘Teacher’ and ‘Lord,’ and you are right, because that’s what I am.  And since I, your Lord and Teacher, have washed your feet, you ought to wash each other’s feet.  I have given you an example to follow.  Do as I have done to you.”

Five Things I Learned About Lean UX and Development

Agile has really done a number on me. I can’t see software development the same way anymore. Deliver value early, often, in small batches. Optimize for the flow of feature delivery, not utilization of a person.

Now I’ve had the chance to engage with a complimentary methodology: lean (or “lean startup”).

After weeks of applying lean principles to a business problem, I’ve got some basic takeaways for the lean-curious:

1.  Agile = efficiency. Lean = effectiveness.

Agile is all about efficiency. Removing waste from production and optimizing the flow of goodness. Prioritizing the work we need to do. But agile doesn’t ask the question: is this even the right product?

That’s where lean comes in. Lean asks us to challenge all of our assumptions by testing BEFORE we build.

And when you get into it, we all have a LOT of assumptions about what will make great software. And too often, we incur the full cost of building that software and making it perfect, only to learn from customers at the very end that it’s not really what they wanted at all.

2.  Test assumptions with a minimally-viabile prototype

The developer in me hates and loves this one at the same time. If you assume that customers will totally love your idea, sketch up the idea and show it to a few people first instead of coding it. That last part is hard (the not coding part).

The basic principle is: build a prototype to the lowest level of fidelity (i.e., cost) to validate your assumptions.

Some amazing tools for this are:

  • Balsamiq. This is amazing for a non-designer type like me to communicate design ideas in a reasonable-looking way.
  • Usertesting.com. This site allows you to test web-accessable prototypes out easily and get feedback from real people outside your usual sphere.
  • Prototype on Paper. If you’re prototyping a mobile application, this free (!) iPhone app lets you knit pictures of sketches together and even makes a small website for you and your users to navigate by clicking hotspots on each sketch. Crazy cool.
  • The cloud. Once you move into HTML prototypes, look at Azure / Amazon / Heroku / App Harbor to quickly get feedback on clickable prototypes.  I used the heck out of my MSDN Subscription credits on Azure and it worked beautifully.
3.  Test weekly

Every week, there should be some new ideas or assumptions to validate. Get some people, somewhere, that aren’t on your team, to bounce the prototypes off of. It’s amazing what you’ll uncover. Building software isn’t a special forces operation. Don’t go dark. Make testing a part of your weekly cadence.

Lean Weekly Cadence
4.  Gather and scatter

This is particularly important for the other introverts out there who, like me, get drained by all-day meetings and constant debates over features and ideas.

There is a huge temptation to think if you just spent another hour at the whiteboard, you’d come up with an idea so perfect it would be impervious to refinement or customer feedback. You can’t. Get an idea, scatter to work on it, test it, reflect on the findings.

5.  Frame the problem, not a possible solution

Another way to say this is: focus on what customers need, not what they might possibly want.

If you start with a problem, you can come up with some metrics to measure progress towards a solution. If you start with a solution, any metrics could end up supporting your conclusion.  Which is kind of the whole point of lean – validating that our solution ideas meet some need or produce some meaningful outcome.

Lots of this was inspired by a wonderful book called Lean UX, which you should check out if you want to dig deeper.

Lean_UX

 

Go make stuff!

Have you started a website yet?

Why not?

I’m blown away by all the great options out there.  Here are just a few, that I have messed with:

  • AppHarbor – really awesome, simple .NET hosting that starts out free and scales cost as you scale out.  I’ve got a very simple chord transposer up called ChordFlow, which I hope to expand on in the very near future.
  • Heroku – in my recent branching out into Ruby on Rails, Heroku made deployment and hosting a real pleasure.  It was kind of unclear for awhile that you can host a single instance for free (like AppHarbor), but once I figured the menu out it was very pleasant.  I built a very basic “thermometer” to track my good friend Andrew’s weight loss progress, called LossThermometer.
  • Azure – I had been skeptical of Azure’s offerings prior to their huge preview update in the past year, but now it’s pretty awesome.  I actually created this WordPress site through their “gallery,” which was < 5 minutes of setup and approximately 2 – 3 hours of trying to select an appropriate theme.  You can host all kinds of stuff up there, from .NET to node.js, spin up your own VMs, data services, etc.

There’s tons of stuff out there.  So go on – start building!

Azure and WordPress

Software Craftsmanship and Staying Current

I’ve encountered the following question a few times both in and outside of the office:

“How do you keep up on the latest technology and practices?”

This is a question that means different things coming from different people.  The first angle is mechanical: it’s basically asking for directions.  How does learning things in this arena work?  Are there any secrets?  Technology spirit guides?  The emphasis is on “how.”

The second angle is motivational.  It’s a curiosity about what drives another person.  Why would you spend time doing that?  What drives you?  Are you crazy?  The emphasis is on “why.”

I thought it would be interesting to share a bit about “how” because I believe the “why” should be apparent, particularly if you are already here reading this blog.  The “why” is why there are craftsman in any field; because they have a connection with the objective value of the craft.  It is worth learning more and driving into deep for its own sake.

So, how does learning work for me?  It’s definitely not a secret technique.

Blogs

This is basically uni-directional discipleship.  It’s recognizing that you have a gap of growth in your life and that there are others who have crossed that gap and have wonderful things to impart to you.  They have no idea about you (usually) but you can follow their journey from afar.

Here are some of my absolute favorites:
  • Scott Hanselman – if you read only one technical blog, read this one.  The ratio of posts that have directly led to adopting and awesome new tool / practice / technique / mindset to total posts is amazingly high for me.
  • Jeff Atwood – less about code, and more around the meta of programming and hardware.  Makes you think while making you laugh.
  • Eric Lippert – if you’re ever feeling smugly intelligent, read this blog.  A super genius C# language designer who is both humble and humbling to read.  And his blog text is all purple, which is fun.

There are also two ways I tend to consume these blogs:

  • Google Reader – great for my primary use case of aggregating blogs and reading only unread entries.
  • Flipboard – a more beautiful way to browse any content and share it easily.
Flipboard

Podcasts

  • Hanselminutes – for the same reasons I enjoy his blog.
  • .NET Rocks! – Richard Campbell (who I have had the pleasure of seeing twice at TechEd) and Carl Franklin are two extremely entertaining technology experts who produce a prolific amount of material.  Even if I only listened to their podcast, which is full of excellent material, I’d have a hard time keeping up.

There are tons of ways to listen to podcasts, but I want to highlight one that I love, called Pocket Casts.  It’s a beautiful app that has some great podcast-specific features, and makes it much more pleasurable to manage lots of podcasts:

PocketCasts
Community
  • Conferences – See if you can go (or have your employer send you) to a technology conference.  They’re great for exposure both to new concepts and often the broader picture that stitches them together.  A few favorites I have been to are Microsoft’s TechEd, Richmond Code Camp and Innovate Virginia.
  • .NET User Groups – I try to encourage folks to get out and visit our local .NET user group here in Richmond.  It’s a great, easy, free way to get exposed to new concepts and meet some people using the same technology to solve different problems in the area.  Of course there are user groups for all sorts of things???I keep imagining I’ll drop in on the SQL Server or Ruby user groups in the area, but I’m not quite there yet.
Tumblr Architecture

Experience

Finally (and this is the most important part) work on fun projects!  Projects that engage you.  Any project you work on, seek to elevate it.  How can you make it easier to change in the future?  How can you make your codebase a joy to work in?  Can you help the developers who come after you be less depressed?  Is there a new pattern or practice you believe would help a project?  Try it out!  The only way to really learn the stuff you listen to is to apply it in your own life.

TextMate

TextMate, brought to you by GitHub!

Open source is pretty cool.

If you have a Mac and have been cheap (like me) in the code / text editor department, you might’ve held out on buying something like TextMate, which usually runs about 50 bucks.

Textmate_2

Well today – no more!  You can go download, build, fork and enhance TextMate for free to your heart’s content, all over at GitHub.  You can find the main page here, with some pretty nice build instructions.

Of course, you could also give Sublime Text 2 a whirl, it seems to be what all the cool kids are using (you can evaluate it for free with no time limit, but it’s $60 to register).  So far on Windows I’m still a big fan of Notepad++ for all things non-Visual Studio.

I’m getting things going now.  Wish me luck!

Building TextMate from the command line

TechEd 2012 Highlights: Git deploy for Azure

This will be the first in a few quick hits about TechEd 2012.  It’s been about a month and a half and I think I’ve finally settled on a few things that have stuck with me.

First off is Windows Azure.  I used to be totally unimpressed and uninspired with this platform.  It seemed like you had to code against some very Azure – specific things (e.g., Azure Service Bus, its key-value storage mechanism, etc) to gain some elasticity and scalability.

Now they’ve really expanded their offering, allowing full VM instances to be deployed and scaled in the cloud, virtual network segments, and Git deployment (on top of their previously existing hosted services, and many other features).  In this post, I’ll just touch briefly on the Git deployment piece.

Git is a distributed version control system that developed out of the disparate nature of Linux development.  I’ve grown to love its speed, ability to work disconnected, branching model, and some other little things about it (e.g., .gitignore files instead of svn:ignore attributes).  I hear it is somewhat magical with its merging capabilities, though that’s not something I’ve experienced quite yet.

Building a website with Azure and Git is kind of scarily easy.  Here’s all you have to do:
  1. Use the new Preview Portal to “Quick Create a website”
    Azure Quick Create
  2. This will spin up a new site instance with some default Azure content in it.  On the management view for that website, you’ll find a git URL.  This is where your source repository is hosted!  It will be something likehttps://%5Byourusername%5D@%5Byourwebsitename%5D.scm.azurewebsites.net/.git
    Planning Poker
  3. Use Git to “clone” this repository locally.
  4. Start adding / committing files for your awesome website to your local repository
  5. When you’re ready, use the “git push origin” command to get your changes back up to Azure.  This will automatically trigger a build / deployment to your hosting area.
    Git Deploy Active
  6. Sit back, have a drink, marvel at teh futures

Also, if you’re looking for a super-easy way to get rolling with Git on Windows, I can’t recommend GitHub for Windows enough.  Very clean / cool looking, super easy installation and updates, and pre-configures Git with several methods of interaction (Metro UI, bash command line, PowerShell with posh-git).

Updating / Installing Ruby in Lion

You know how they say you should learn a new language every year?  Well, I’m woefully behind on that.  I’m hoping this will be a year to finally get my head around Objective-C and Ruby to make up for lost time.

So anyway, I sat down tonight to install Ruby and actually do something.  I didn’t get to the “do something” part. But here’s how I got ruby to report the latest version (1.9.3) instead of what it came with (1.8.7).

Yay I got Ruby On Rails On Lion!
I’m on a MacBook Pro running OSX 10.7.3 (Lion).  After Googling in the dark for awhile about finding some sort of one-click installer, I finally came across a couple of helpful things:
  • RubyGems – apparently this is like NuGet (a package management system for software libraries and their dependencies), if you are coming from the .NET developer world like me.
  • RVM – “Ruby Version Manager.”  Apparently this is how all the cool kids install Ruby.  I shunned this advice I found in my early Googling, trying to use sudo and RubyGems to update it.  Which just yielded cryptic errors about missing header files.
  • Homebrew – You end up needing this for an obscure SSL management library.  All will become clear soon.  This has nothing to do with Wii Homebrew, which I love.
<disclaimer>I know nothing about Ruby (yet).  I’m not good at this.  I’m rusty at the command line.  You’ll probably read the steps I took and laugh and then suggest a simpler way.  Actually that would be awesome.  Please do that.  I’m just hoping this saves someone the time it took me to figure this out.</disclaimer>

So first off…holy cow.  I see why there’s a kickstarter project that already has almost double its asking pledge to make this easy.

Alright…now that that’s out of the way.

I’ll spare you all the dead ends I went down (and oh, there were many).  Remember…this is so when I type ‘ruby -v’ I see 1.9.3 (actually, to be clear, 1.9.3p194) instead of 1.8.7 out of the box with Lion.  Here’s what ended up working:

  1. Install Xcode
  2. Install the Command Line Tools in Xcode Preferences > Downloads
  3. Start doing the stuff on this page for RVM.  Note that I utterly ignored its warning to read all subsections under RVM.  I’ll get to that someday, I promise.
  4. Now while you were doing step #3, you might’ve said to yourself “pssh.  Requirements.  Who needs those?”  You do.  For just the straight Ruby install, it says you need Libksba.  Because, of course you do.  It’s libksba!
  5. To actually get Libksba, I found the easiest (for me, only) way to get it to work was using Homebrew.  This appears to be some sort of package / dependency manager distinct from RubyGems.
  6. Run `brew doctor` and do what it says (my only deviation was not remediating /usr/local permissions, but I did switch the Xcode path).  If you did steps #1 and #2 here, some of these will be taken care of.  For the rest, this article helped me out a lot.
  7. You’re almost home!  Now type `rvm install 1.9.3` and have a drink.  Because, man, you earned it.

…so…now what?

Bootstrap, from Twitter: How to get your website’s style off the ground

*blows dust off of blog* Hey! There’s a blog here! Let’s start using it again.

Thinking about writing a website? Got a cool idea but know your web design evokes 90’s-era Geocities pages? Well, you (and I) are in luck, friends. There are tons of “bootstrap” styles that you can snag out there that consist of some basic JavaScript and CSS, but I want to highlight one that I have used both privately and professionally: Bootstrap, from Twitter. Amusingly, as I sit down to write this, they’ve actually pushed a major update that’s changed the basic structure. So, we’ll be learning together.

Let’s fire up Visual Studio 2010 Express and create the very standard MVC3 startup project:

VS2010 New MVC3 Project

OK, so now we have a project. Let’s head on over to the Twitter Bootstrap site and download a ZIP of all the good stuff:

Bootstrap from Twitter

Unpack that, and we have three folders: img, css, and js. I’m just going to drop the img and css folders straight into “content” – I scanned the CSS and noted that it wants the img folder to live in a side-by-side directory, so this just makes it easier. I dropped the bootstrap.js file into scripts:

Solution Explorer for Twitter Bootstrap MVC

You’ll also noticed I snagged jQuery 1.7.1 – I pulled this in via NuGet (a topic for another post).

So now what? Does it just work? Is it magic! Yes! Wait…

Untweaked Twitter Bootstrap MVC

OK so that looks like crap.  What went wrong?  I thought you said this was easy?!  I hate your blog!

So now that you’re using this (or any other) bootstrap “template” of styles, you’ll have to make sure your HTML uses all the expected style classes in the right places. Microsoft’s default “Site.css” that comes in the basic MVC startup project likes things one way, which is clearly not the way Bootstrap from Twitter likes it.

I spent a couple minutes rearranging the main Razor layout. Ta-da!  Underwhelming, but still, it proves that you can get up and running really quickly:

Fancier Twitter Bootstrap MVC

And here’s the code in case you’re following along:

Please go and mess around on their site to see all the awesome things that you get for free with this – fancy alert messages, modals, list navigation, fluid layouts.  And you can even fork the project yourself on GitHub and take it any direction you like.

Have fun!