Why I prefer C# Development over Objective-C
I spent the bulk of 2012 and and all of 2013 developing on iOS in Objective-C and found it pretty decent. As I got more and more experienced with it, I began needing to do more and more complex stuff. Things like using semaphores and digging in to some of the lower level C functions in Foundation.
I have since moved back to using C#, and find my self enjoying development a lot more. The C# language and .NET framework have come a long way since 2005 when I first started using it. There are some things in Objective-C that just drove me crazy. Things like string literals for your Predicates.
For example, to filter an array by a persons age in objective-c with a predicate, you do the following:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Age > %d", 21];
NSArray *results = [self.people filteredArrayUsingPredicate:predicate];
Filtering an array in C# is a lot easier.
var results = this.People
.Where(p => p.Age > 21);
So much cleaner! There are things like this all throughout the .NET framework that are just really simple compared to Objective-C, with the exact same result.
Another example is running something on a background thread. In objective-C, if I have three methods and I want to run one out of the three in the background, I have to do this:
[self myFoo];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self heavyStuff];
dispatch_async(dispatch_get_main_queue(), ^{
[self myBar];
});
});
While in C#, I can do this:
this.Foo();
Task.Run(this.HeavyStuff);
this.MyBar;
And I have ran the heavy method on another thread. Doing call backs in objective-c are pretty close to the same between the two languages. In objective-c:
- (void)syncSelectedTags:(NSArray *)tags withFailure:(void (^)(NSError *))failure {
if (!tags) {
NSDictionary *syncErrorInfo = @{ @"Failure" : @"Could not begin tag synchronization",
@"Reason" : @"A synchronization process is already running. Can not run more than one." };
NSError *syncError = [NSError errorWithDomain:@"com.AllocateThis.LifeBlog.LBTagRepository.syncWithCompletion" code:0 userInfo:syncErrorInfo];
failure(syncError);
}
}
In C# the code looks like:
SyncSelectedTags(List<Tag> tags, Func<NullReferenceException> failure)
{
if (tags == null)
{
string message = string.Format(
"{0}\n{1}",
"Could not begin tag synchronization.",
"A synchronization process is already running. Can not run more than one.");
var syncError = new NullReferenceException(message);
failure(syncError);
}
}
I really have gotten in to C# a lot more lately and really enjoy coding in it again. The framework and language is just so much cleaner and less verbose which makes readability much better and lets me focus more on just getting my product done rather than writing a lot of support code to perform a single action.
I did take a few things away from my time in Objective-C and that was making sure I adhere to standards better. In C# I wrote the code how I wanted to write it, and when I returned I've forced myself to adhere to the langauge standards. My source code has improved dramatically over the last 6 months. It's really nice being back in it.