iPhone

Best Nine Dollars a Month You’ll Ever Spend

I’ve been getting more and more into iOS programming recently. It is something that I’ve been doing more and more as a hobby, as well as landing a few paying consulting roles creating native iOS applications. I’ve kind of been “marinating” in iOS for some time, but about a year ago I started really getting serious and attempting to have an actual goal with regards to Objective-C and iOS.

One good FREE resource is the Introduction to iPhone series on TekPub with Ben Schierman. It was done a few years ago, and Rob Conery has now given it to the community. It is a little out of date, but you can’t beat the price and it will help you learn a few things.

I have a TekPub subscription, so from there I moved on to Show Me Objective-C, another Ben Schierman TekPub series. I really liked the way that Ben explained things, so I started doing a little cyber-stalking digging about him and I discovered his site over at NSScreencast.

Ben has some free videos there, as well. I downloaded and watched them all and learned a TON. His video about AFNetworking alone was worth the time. I noticed that he had a subscription package initially, but didn’t check back into it until I had watched all of the free videos. I’m glad I did. He offers full access to his videos (new ones weekly) for only $9.00 a month.

This is a BARGAIN!

I feel like – thanks to Ben – that I am an honest-to-goodness-real-life iPhone Developer now. I’m not scared of Xcode, I’m not mystified by Objective-C, nothing. With the videos he has to date, you can spend a weekend and gain an insane amount of confidence on the platform. I highly recommend it.

Goals

750 Words

Recently, I started using a site called 750 words. Its name basically sums up what it is. Basically, you sign up for the site and you just type in daily writings. It does analysis of your words and tells you all kinds of stats about what you’ve written, how long it takes you to write, how many interruptions you typically have, etc. According to the site, 750 words is about 3 printed pages of work, so you are making good-sized chunks of writing each day. Each entry is private, so you don’t have to worry about grammar or spelling (though you’d obviously want that to be improving as you went). Another neat feature is that the creator has made it very easy to export your writings. You own it, you shouldn’t have to worry about being locked in. I definitely appreciate that aspect of the site.

I started 37 days ago and I’ve completed 37 days. I’m a bit of a compulsive person, so once I build up a habit like this, it actually stresses me out to break it. I’m probably the poster child that the site’s founder had in mind when he created the site. Each month, there are also “challenges” that you can participate in. You sign up and commit to write something every day for that month. I’m 13 for 13 in December so far (obviously since I’ve written every day for the last 37). The site even lets you define a reward and a penalty for completing or not completing the challenge. There is no way to enforce it, it is just for fun.

The site is such a simple concept (the best ones are), but I’ve found it really helpful to get into a writing habit. At first, I would just write about my day. I haven’t had a private “general purpose” blog in a while, so I did that. However, that only lasted about 4 or 5 days before I grew tired of it. 3 pages of good stuff doesn’t happen to me EVERY DAY! My original goal was that I’d be able to free write and take notes and use those notes as primers for blog posts. I changed that goal a few weeks ago.

During TekPub‘s Black Friday Sale, I signed up for my own personal annual TekPub Subscription. Two years ago, my employer bought me one, but I work for myself now and I needed to invest a little more in my education. What I started doing was “live blogging” the videos as I watched them. This served three purposes.

  1. Complete my Daily 750 Words
  2. Watch a New TekPub Video Every Day
  3. Create Lots of Very Useful Notes For Future Blog Posts

To date, I’ve watched the entire TekPub Speed Series with Sam Saffron, the Node Series, and all of the Jon Skeet Stack Overflow Question of the Day TekPub TV videos, “live blogging” them all. Along the day, I have paused to journal some incredible days that I’ve had or some interesting projects that I was working on, but I always fall back to the TekPub videos as a great source of content. The bonus is that I’ve learned a great deal so far.

Today’s blog post is a little meta. This post itself is today’s 750 Words. No need to write 1500 words today when a little reuse can do. I’m a programmer, after all, appropriate reuse is the goal.

Why might 750Words.com be appropriate for you? Maybe you don’t have a blog, technical or personal. Perhaps you don’t see the use of writing all of these daily words with no intention of anyone ever reading it. I think for someone like that, the site still has merit. First, one of many developers’ greatest weaknesses is communication. I’ve seen it over and again that clients aren’t happy with the level of communication (particularly written communication) from their developers. And we all know that the best way to build a muscle is to work that muscle, so daily writing will help.

Secondly, you may discover that you have a love of writing. Maybe your words won’t ever get beyond the 750Words.com vault, but maybe you’ll write and find that you enjoy it. If 750 words really is about 3 printed pages, in 3 months, you could have about 270 pages of material. Assuming you kept a consistent theme or themes, you are actually a good deal of the way to having written a book. You could export your posts, edit them into a more coherent theme, and publish them to the world on the Kindle store.

Since book writing is something that I’ve always wanted to do at least once, I’ve certainly considered that route, but for now I’m just trying to build blog posts. Maybe one day soon, I’ll build that into a book. I don’t know now if it would be fiction or non-fiction, technical or not, for now I’m just enjoying the writing process.

So, if you can see any value in daily writing at all, head on over to 750 words and create an account and start writing. At the risk of being a cliché, the journey of 1000 miles starts with a single step. Do 750 Words today, take the first step.

Uncategorized

Implicit and Explicit Operators

Explicit SemanticsI came across an interesting article the other day. It was about a “forgotten” C# feature, the implicit and explicit operators. You can see the original article here. I do remember reading something about these operators years ago, but I don’t think that I’ve seen these operators at use in the wild, though I can definitely think of a ton of times that we needed it.

If you don’t know yet, the explicit and implicit operators help with converting one object of one type into another object of another type. The best use case for this isn’t for converting simple types like ints into doubles, but for converting more complex objects. One common example would be if you are using something like nHibernate or Entity Framework that generates classes for all of your database tables. However, if you are using WCF/Web Services/Remoting/Whatever, you might have different classes that need to exist before transmission.

Let’s pretend that we have the following classes:

namespace DataClasses
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int PersonId { get; set; }
    }
}

namespace ContractClasses
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int PersonId { get; set; }
    }
}

Now, we can see that the classes have the same names and identical declarations. Again, this is very simplified, but it is a real problem. This is one of the types of things that the excellent tool AutoMapper is used for. AutoMapper does this for you automatically, of course, but if you only need this for a few instances, you may not want to take a dependency. If you just try this code, you will get the error “Cannot implicitly convert type ‘DataClasses.Person’ to ‘ContractClasses.Person'”.

DataClasses.Person dcPerson = new DataClasses.Person() {FirstName = "Pete", LastName = "OnSoftware", PersonId = 7};
ContractClasses.Person ccPerson = dcPerson;

However, if you add this code into the DataClasses.Person class (either directly or with a partial), the above code now compiles.

public static implicit operator ContractClasses.Person (DataClasses.Person p)
{
    return new ContractClasses.Person()
               {
                   FirstName = p.FirstName,
                   LastName = p.LastName,
                   PersonId = p.PersonId
               };
}

That does make the code compile, but we are losing some of what is happening there. That is why the explicit operator exists. It allows you to not do a “hidden” convert, but instead make an explicit cast. If we change the code to look like this our code breaks again:

public static explicit operator ContractClasses.Person (DataClasses.Person p)
{
    return new ContractClasses.Person()
               {
                   FirstName = p.FirstName,
                   LastName = p.LastName,
                   PersonId = p.PersonId
               };
}

However, if we change our original code to this, we are back in business and much more readable.

DataClasses.Person dcPerson = new DataClasses.Person() {FirstName = "Pete", LastName = "OnSoftware", PersonId = 7};
ContractClasses.Person ccPerson = (ContractClasses.Person)dcPerson;

This isn’t limited to converting “identical” classes. Jeffrey T. Fritz, the author of the article above that inspired this post converted from an order class to a receipt class, for instance. I definitely believe that using explicit and implicit can make your code much more readable than using utility/conversion classes or extension methods to accomplish the same thing.

Code Tips

Git Error : (does not point to a valid object)

A friend of mine IMed me the other day to ask if I had ever seen an error like the one below:

error: unable to find e291a84831b445ba982539cc63a418126f0b5364
error: refs/heads/master does not point to a valid object!
fatal: Couldn't find remote ref master
fatal: The remote end hung up unexpectedly

I had not, but it seemed plain enough. It appeared that the head of the master branch was pointing to a commit that didn’t actually exist in the repository. I don’t know for sure how this happened, but my friend’s team suspected a disrupted internet connection on push as one theory.

I Googled the error and found suggestions like “make a new remote” and “clone to a new branch, push, delete master, rename new branch”. This seemed like just too much work. There had to be an easier solution.

I was unable to clone the remote to my own machine (I got that same error on trying) and the team was in another state, so – short of a screensharing session – I couldn’t easily work with them on the problem.

I had the developer who had done the last known valid commit before this error send me the most recent 5 items output from the “git log” command and got the following (edited for privacy):

commit b65f24a64e78b38d193aa545d7b184fe26330a4c
Author: Joe Developer <joed@somewhere.com>
Date:   Fri Jul 27 10:05:53 2012 -0400

    Moved foo.jar to libs folder

commit 32b15424509881760667a77b615cc91e8e31afb9
Author: Joe Developer <joed@somewhere.com>
Date:   Thu Jul 26 21:45:46 2012 -0400

    Load swf files

commit bfac8d86c20ebbcac22af4e599e5815b0586f3d0
Author: Joe Developer <joed@somewhere.com>
Date:   Thu Jul 26 19:18:25 2012 -0400

    Navigation bug fixes

commit 60c5ff87435861157e56d948e09c63ad2f4db520
Author: Jane Developer <janed@somewhere.com>
Date:   Thu Jul 26 15:52:36 2012 -0400

    post merge

commit 1a97d137a51c6cd34825e4c9bc705620dfff7712
Author: Jane Developer <janed@somewhere.com>
Date:   Thu Jul 26 15:24:40 2012 -0400

    initial commit

Because Git is based in the file system, I could literally navigate to the remote file system and go to the ProjectName.git folder and into the refs/heads/ folder and find the master file (no extension). Inside was one string, the offending e291a84831b445ba982539cc63a418126f0b5364. I just replaced that string with the hash of the latest valid commit – b65f24a64e78b38d193aa545d7b184fe26330a4c – and then saved.

After that, I could clone the repo and the developers could pull, merge, and push their own changes. They were using DropBox in this instance as a “poor man’s remote” and upon further reflection, I have to wonder if that is what caused the conflict. Perhaps there was a problem with DropBox syncing all of the files and maybe a race condition at some point that resulted in a corruption? I know DropBox isn’t a perfect “remote server” solution, but a lot of people use it for certain circumstances, so this might be something to look out for.

If anyone else has seen this error and has a better fix than the one described here, then please leave a comment and let me know.

Code Tips

C# Extension Methods on Null Objects

Here is something that I just learned yesterday that I didn’t know. This was one of those fun things where I knew every piece of the puzzle, but had never “realized” or “made the connection” between all of them.

If you aren’t sure what extension methods are, I wrote a blog post about them back in 2008 that you can check out here.

Here is an example for today:

    public static class ExtensionMethods
    {
        public static bool IsEmptyStringArray(this string[] input)
        {
            if (input == null) return true;

            return !input.Any();
        }
    }

What I’ve done is just create a method that allows you to call .IsEmptyStringArray() on any string array to find out if it has any items in it. I realize that this is a fairly useless example, but it is contrived for the sake of the demonstration.

Now, if I call a “framework” method on a null string array, I get an error. So, doing something like this:

string[] nullArray = null;
var hasItems = nullArray.Any();

Results in the error “Unhandled Exception: System.ArgumentNullException: Value cannot be null.”

However, I *CAN* call my extension method on that null array.

string[] nullArray = null;
var hasItems = !nullArray.IsEmptyStringArray();
Console.WriteLine(hasItems);

This code produces the following result:
Result of calling an extension method on a null string array

How does that work? This is what I had neglected to put together in my mind. When you write an extension method, what actually gets compiled is this:

call bool CodeSandbox2010.ExtensionMethods::IsEmptyStringArray(string[])

The “syntactic sugar” part is that you aren’t actually calling a method on the null object at all. You are just calling your method and passing in the parameter, just like any other method. I really like that because it gives you a concise way to write your code without the same null check over and over and over again throughout your codebase. You can just check in the method and then get on with what you’re doing.