Categories
Uncategorized

Autosynthesis / I’m Really Into This / Everybody Happy When We Write Less Code

In an earlier post, I talked about a couple of changes to Objective-C that should reduce the amount of “yak shaving” you need to do while coding: syntaxes for NSNumber, NSArray and NSDictionary literals, as well as the new, shorter syntaxes for NSArray/NSMutableArray and NSDictionary/NSMutableDictionary item access.

Here’s the tl;dr version of this post: you no longer have to @synthesize properties! (Most of the time, anyway.)

From Public Variables to Getters and Setters to Properties

In the early days of object-oriented programming, you were supposed to make a class’ public attributes accessible through public variables. Access was pretty simple:

previousSpeed = myCar.speed;
myCar.speed = 50;

Then, it became a better idea to lock away all the variables and use getters and setters:

previousSpeed = [myCar getSpeed];
[myCar setSpeed:50];

It does the job, but it’s a little clunky.

Nowadays, the preferred way to expose class attributes in a number of languages is through public properties. With them, we’re back to accessing object attributes through this simple syntax:

previousSpeed = myCar.speed;
myCar.speed = 50;

What @synthesize Was For

Creating properties in Objective-C classes used to require statements in a couple of places. For a public property, you had to declare it in the interface (.h) file:

// Car.h
@interface Car : NSObject
@property int speed;
@end

The @property statement tells the compiler that you want to expose a property; in the case of the code above, the name of the property is speed. Properties are by default both readable and writeable. If you like to spell everything out very explicitly, you can by declaring the property this way:

@property (readwrite) int speed;

If for some reason you wanted speed to be read-only, you can declare the property this way:

@property (readonly) int speed;

With every property declaration comes the need for the underlying instance variables — ivars in Objective-C parlance — and the requisite getter and setter methods, which go in the implementation (.m) file. All this setup for each property can get a little tedious, and the @synthesize statement saves you from that tedium. Instead of having to declare the corresponding ivar and write those methods, @synthesize lets you do all that in a single line of code:

// Car.m
@synthesize speed;

By default, the name of the ivar created by @synthesize was the name of the corresponding property preceded by an underscore character. For example, the default name of the ivar behind a property named speed would be _speed. If you preferred, you could specify a different ivar name this way:

// Car.m
@synthesize speed=someOtherIvar;

The general rule was that if you only needed a simple getter and/or setter for a @property, use @synthesize.

Introducing Autosynthesis

The version of CLang (the Objective-C compiler) that comes with XCode versions 4.3 and later (the latest version is 4.5), supports autosynthesis, which automatically does the synthesizing for any class properties you declare in the header. If your @property needs only a simple getter and/or setter, you don’t need to have a corresponding @synthesize anymore. The compiler takes care of that for you.

Properties with autosynthesis work like they did with manual synthesis. You access them using the self.propertyName syntax, and the name of the ivar that gets generated is still the name of the property preceded by an underscore character.

Cases Where @synthesize is Still Useful

There are some cases where you’ll still want to use the @synthesize keyword, and this article in the blog Use Your Loaf does a good job explaining these cases. Such cases are a little more rare; most of the time, you can simply skip added @synthesize to your code because the compiler’s taking care of that for you!

In Case You Were Wondering…

The title for this article comes from the chorus of Shriekback’s 1985 alt-dance number, Nemesis:

Priests and cannibals
Prehistoric animals
Everybody happy as the dead come home

Categories
Uncategorized

Fighting Tooth and Nail for Third Place

3rd Place is a disturbing yet amusing coloring book really meant for adults.
Click the photo to find out more.

A conversation I had with a friend circa April 2011, shortly after I posted this article:

Friend: So why’d you leave? Was it the money?

Me: Are you kidding? The money’s awesome. In fact, the money’s making it hard to leave.

Friend: It was the incident*, was it?

Me: Actually, no. Even right after “the incident” [I made “air quotes” with my fingers while saying this], I was quite sure I was going to stick around for a while.

Friend: So what was it, then?

Me: A bunch of things. After everything that’s happened, I figured I was due for a change. Plus, there’s the whole being-the-Windows-Phone-guy thing. I’ve done too many 14-hour days and a lot of slogging around for what? A distant third place…if we’re lucky.

* The incident is a good story, best told in person over drinks.

A tooth-and-claw battle for a distant third place doesn’t seem to bother RIM CEO Thorsten Heins, who earlier today at BlackBerry Jam (where they also presented that cringe-worthy music video) said:

We have a clear shot at being the number three platform on the market. We’re not just another open platform on the market, we are BlackBerry.”

“Why go for the gold when you can go for the bronze?” said no one, ever.

To be fair, later in the article, it says:

Asked why he wasn’t aiming for one or two, Heins said “you climb a mountain step by step.”

That’s actually pretty reasonable. The next 18 months will tell the rest of the story, but the trends aren’t with them right now, according to Dan Frommer, who reminds us how far RIM has fallen with this graph:

Maybe RIM can borrow a page from AVIS’ book and come up with a slogan like “We’re number three, so we try harder”

Categories
Uncategorized

The iPhone 5 and Scratchy Show (or: You’re Holding It Wrong 2.0 and Design for Deterioration)

The iPhone 5 and Scratchy Show

Click the photo to see the source.

Just like as the white zone at the airport is for loading and unloading only, my left pocket is strictly just for my iPhone (still a 4S — I got it in November, and I’ve no compelling reason to upgrade yet). That way, there’s considerably less chance for it to get scratched. Even so, I keep my iPhone in a case, even if Darth Gruber disapproves.

Click the photo to see the source.

With the iPhone 5, Apple ditched the “glass sandwich” design in favour of aluminum. The problem is that customers have noticed — and remember, it been available for less than a week — is that it scuffs, scratches and even chips a little too easily.

Click the photo to see the source.

Apple Senior VP Marketing Phil Schiller, when asked via email by a customer if there were any plans to fix this problem, replied:

Any aluminum product may scratch or chip with use, exposing its natural silver color. That is normal.

He must be channeling the spirit of Steve Jobs — this is “You’re holding it wrong”, version 2.0.

Design for Deterioration

All this reminds me of an article by Khoi Vinh in his blog Subtraction, in which he talks about designed deterioration. He waxes poetic about his cast iron skillet and other things that look better when worn or used heavily:

…I have a US$20 cast iron skillet that I bought several years ago from a restaurant supply shop in downtown Manhattan. I’ve cooked hundreds of meals with it, and over time it has developed a coating from oil and food — the manufacturers call it ‘seasoning.’ It’s a little unbecoming when you think about it; in fact, though I clean it, it’s a dirty piece of cookware, and it resembles its original, store-bought state not at all.

…After cooking in it and cleaning it up, I’ve spent a lot of time just looking it over, marveling at how its very deterioration has been incorporated into the design of the object, at how it’s gotten more attractive — less ignorant — the more I use it. I’m not particularly sentimental about much in my kitchen, but I would be heartbroken if you took away this iron skillet…

He compares it to his technological goodies, for which the opposite is true:

I mention these things because I’ve noticed recently that the concept of what we might call designed deterioration is fairly anathema to digital hardware. The objects we purchase from purveyors of digital technology are conceived only up to the point of sale; the inevitable nicks, scratches, weathering, and fading they will encounter is not factored in at all. The result is that as they see more use, their ignorance may recede, but they wear it poorly. They don’t age gracefully.

Looking at the digital technology I own, what moderate deterioration to be found — dents in my laptop, a gash in the side of a laser printer I own, the accumulated grime on my computer keyboard — doesn’t make these items more desirable at all. In fact, when I see the way the corner on my aluminum PowerBook has been warped due to a nasty fall from a chair, I cringe. Through this obvious, glaring example of use, of accumulated knowledge, the object itself hasn’t attained an additional whit of beauty.

In fact, the damage is actually quite repulsive. This is because the laptop was conceived by Jonathan Ive based on an assumption that it would remain perfect forever. There was no designed deterioration factored in whatsoever, and so no real thought was given to how the laptop might change with use. Marks of knowledge, like the warped corner, aren’t meant to be embraced, but rather denied.

He also astutely observes that if you designed things like iPhones to look better with age and use, you’re discouraging people from upgrading to a newer model.

Categories
Uncategorized

“RIM Speedwagon” Tells Developers in a Music Video That They’ll “Keep On Loving You”; Maybe It’s Time to Declare a Moratorium on These Things

9to5 Mac have pointed to a “LOLWTF” video by “RIM Speedwagon”, a band consisting of some RIM VPs including Alec Saunders (VP Developer Relations and Ecosystem. When I was at Microsoft as a Windows Phone Champ, I used to say “Well, at least my job is easier than Alec’s.”

Done to the tune of REO Speedwagon’s 1981 hit, Keep On Loving You, the song in the video is a message to developers: just hang on a little longer, and we promise that you won’t be disappointed.

I don’t think it was a good idea to pick a tune that was already retro when many of today’s mobile developers had yet to be conceived, but perhaps they’re aiming at the enterprise developer demographic, who are an older crowd. The song may resonate with those of us who grew up in the ’80s, but to the millennial crowd, it may cement BlackBerry as “the old folks’ phone”. I remember growing up in the ’80s and being annoyed at all the ’60’s nostalgia that the then-guardians of pop culture were shoving down our throats; surely it must be like that for the young’uns today as we expose them to old MTV era tropes.

Still, I have to say that cheese-tastic as RIM Speedwagon’s song is, it’s still a damned sight better than the Bruce Springsteen homage in this internal “Sell more Windows Vista!” Microsoft video aimed at their sales team:

Come to think of it, I think the only techie-focused music video featuring an ’80s tune that ever made any kind of good splash was Here Comes Another Bubble:

I think it’s time to declare a moratorium on these things.

Categories
Uncategorized

Mobile Developer News Roundup: Appcelerator/IDC Survey of Mobile Devs, 5 Reasons Why Responsive Design Isn’t Worth It, and Android Can Be Beautiful

Results from Appcelerator’s and IDC’s Mobile Developer Survey

In the Seattle Times’ column Microsoft Pri0, Janet Tu points to the latest quarterly survey report of over 5,500 mobile developers conducted by Appcelerator and IDC. Among their findings:

  • The developers surveyed were disappointed with building HTML5 mobile apps, citing poor user experience, performance and monetization.
  • One third of the developers surveyed said they were “very interested” in developing for Windows 8. The interest comes from the promise of being able to write applications that would work on both the desktop and on the soon-to-come Surface tablets.
  • Fewer than 10% of the developers surveyed said they were “very interested” in developing for BlackBerry. In January 2011, this number was 40%.
  • The developers surveyed believed that by 2015, they’ll be writing apps for more than just phones and tablets, but all manner of devices: TVs, connected cars, game consoles, smartwatches, Google glasses and so on.

Links

5 Reasons Why Responsive Design Isn’t Worth It


In 5 Reasons Why Responsive Design is Not Worth It, Tom Ewer argues that in many circumstances, there’s no need for a web site to be responsive since the desktop version works just fine. The five reasons mentioned in his article’s title are:

  1. It defeats user expectation, since the site can look quite different on a mobile device, hiding UI elements that the user might expect.
  2. It costs more and takes longer. This one’s self-explanatory.
  3. Non-responsive designs usually work. In many cases, all you have to do is rotate your phone to landscape mode.
  4. There is often no load time benefit. I don’t even consider this one of the benefits of responsive design, but some people do.
  5. It’s a compromise. “It is a subjective decision by the designer that the desktop display will not be optimal on mobile devices, followed up by a subjective decision as to what will be.”

Android Niceties Shows That Android Apps Can Be Beautiful

Android Niceties is a Tumblr that showcases “screenshots encompassing some of the best, most beautiful looking Android apps”. If you’re an Android developer or designer, you’ll want to visit this site often for inspiration.

Categories
Uncategorized

What the iPhone 20 and Galaxy S 23 Might Look Like Together

First came What the iPhone 20 Might Look Like. Now, this:

Categories
Uncategorized

Objective-C’s New NSNumber, NSArray and NSDictionary Syntaxes Mean Less “Yak Shaving” for iOS and OS X Developers


Yak shaving is a term used to describe a seemingly pointless activity that you actually have to do in order to get a larger task done, or as Jeremy Brown put it:

You see, yak shaving is what you are doing when you’re doing some stupid, fiddly little task that bears no obvious relationship to what you’re supposed to be working on, but yet a chain of twelve causal relations links what you’re doing to the original meta-task.

The term “shaving a yak” was used in the 1950 film Sunset Boulevard, but it’s more likely that its use in programming comes from Ren and Stimpy:

Many programming languages, especially those that pre-date modern scripting languages, often call on the programmer to do some yak shaving, and Objective-C is no exception. With XCode 4.5 and its new Clang compiler, you get some nice bits of syntactic sugar that I’m certain will make you say “Finally!” and save you from a fair bit of yak shaving.

NSNumber Literals

If you’ve been coding in Objective-C even just a little bit, you’ve probably come across NSString, the non-mutable string type used when developing for OS X’s Foundation framework. If you have, it’s likely that you’ve seen assignments that look like this:

NSString *myString = @"Hello, world!";

The @ is a handy bit of shorthand that specifies to the compiler that Hello, world! is an NSString literal. Without this bit of syntactic sugar, we’d have to write the above line of code like so:

NSString *myString = [NSString stringWithCString:"Hello, world!"];

Until now, there’s been no such syntactic sugar for NSNumber, Foundation’s type for wrapping numerical values in an object. It’s handy for doing things like wrapping numbers so that you can store them in collection classes like NSArray and NSDictionary, which can only store objects.

Until the current version of Objective-C (which comes with XCode 4.5), here’s how you’d assign NSNumbers given some numeric literals:

NSNumber *meaningOfLife = [NSNumber numberWithInt:42];
NSNumber *ussReliantPrefixCode = [NSNumber numberWithUnsignedInt:16309];
NSNumber *floatPi = [NSNumber numberWithFloat:3.14159];
NSNumber *doublePi = [NSNumber numberWithFloat:3.14159265358979];
NSNumber *avogadrosNumber = [NSNumber numberWithDouble:6.02214129E+23];

That’s a lot of work just to simply box a simple number type into an object. Luckily for us, Objective-C now supports NSNumber literals. Just as with NSString literals, NSNumber literals are preceded with the @ sign.

The code below uses NSNumber literals and is equivalent of the code above, but requires less typing and is easier to read:

NSNumber *meaningOfLife = @42;
NSNumber *ussReliantPrefixCode = @16309U;
NSNumber *floatPi = @3.14159F;
NSNumber *doublePi = @3.14159265358979;
NSNumber *avogadrosNumber = @6.02214129E+23;

Array Literals, NSArray, and NSMutableArray

Initializing NSArray and Accessing Its Elements the Old Way

If you’re coming to Objective-C from languages like JavaScript, Python or Ruby, you’re used to doing array assignments using literals.

Suppose you wanted to create an array containing the first names of the members of the Stark family from Game of Thrones. Here’s how you’d do it in Python and Ruby (and in JavaScript, as well, although you’d probably want to place the var keyword before starkFamily):

starkFamily = [
    "Eddard",
    "Catelin",
    "Robb",
    "Sansa",
    "Arya",
    "Bran",
    "Rickon"
]

There used to be no such thing as an NSArray literal. Here’s how you’d create the array above using Objective-C and NSArray — using the arrayWithObjects: method, which expects a comma-delimited list of objects terminated with nil.

NSArray *starkFamily = [NSArray arrayWithObjects:
    @"Eddard",
    @"Catelin",
    @"Robb",
    @"Sansa",
    @"Arya",
    @"Bran",
    @"Rickon",
    nil
];

It’s not that much more typing, but you have to remember to mark the end of the list with nil.

What’s far more unwieldy is array access. In JavaScript, Python and Ruby, if you wanted to access the element of starkFamily whose index is 2, you’d do it this way:

starkFamily[2]

In earlier versions of Objective-C, here’s how you’d access that element:

[starkFamily objectAtIndex:2]

Wow, that’s clunky. Even with XCode’s autocomplete feature (the analogue of Visual Studio’s Intellisense), it’s still a lot of typing for something as simple as array element access.

Initializing NSArray and Accessing Its Elements the New Way

With the latest version of Objective-C, we have NSArray literals. Here’s how you’d initialize the starkFamily array using an NSArray literal:

NSArray *starkFamily = @[
    @"Eddard",
    @"Catelin",
    @"Robb",
    @"Sansa",
    @"Arya",
    @"Bran",
    @"Rickon"
];

NSArray literals look almost like JavaScript, Python and Ruby array literals. The big difference is NSArray literals begin with a @ character, just as NSString and NSNumber literals do.

As for accessing elements from NSArrays you can now do so using a more familiar notation:

starkFamily[2]

Finally! I much prefer myArray[index] over [myArray objectAtIndex:index].

Initializing NSMutableArrays Using Array Literals

NSArray is an immutable type; once initialized, you can’t reassign, add, or remove any of its elements. NSMutableArray, a subclass of NSArray, is mutable. Here’s how you initialize an NSMutableArray using an NSArray literal:

NSMutableArray *starkFamily = [@[
    @"Eddard",
    @"Catelin",
    @"Robb",
    @"Sansa",
    @"Arya",
    @"Bran",
    @"Rickon"
] mutableCopy];

In the code above, we create an NSArray using an array literal and invoke NSArray‘s mutableCopy: method on it. The result is an NSMutableArray containing the array literal’s values.

As with NSArray, you can now access elements of an NSMutableArray using standard array notation…

starkFamily[2]

…and, of course, since the array is mutable, you can do stuff like this:

starkFamily[2] = @"Tony";
[starkFamily removeObjectAtIndex:0];

Dictionary Literals, NSDictionary, and NSMutableDictionary

Initializing NSDictionary and Accessing Its Elements the Old Way

Different programming languages use different terms for what’s roughly the same thing: an object that stores values which you can look up using keys. JavaScript keeps it simple by using objects for this task, Ruby has hashes and Python has dictionaries.

Consider the following Python dictionary definition:

importantNumbers = { 
  "Meaning of life" : 42, 
  "USS Reliant prefix code" : 16309, 
  "Single-precision pi" : 3.14159, 
  "Double-precision pi" : 3.14159265358979, 
  "Avogadro's Number" : 6.0221415E+23
}

Ruby’s hash syntax is similar:

importantNumbers = { 
    "Meaning of life" => 42,
    "USS Reliant prefix code" => 16309,
    "Single-precision pi" => 3.14159,
    "Double-precision pi" => 3.14159265358979,
    "Avogadro's Number" => 6.0221415E+23
}

If you wanted to access the number associated with the key Meaning of life in either Ruby or Python, you’d do it this way:

importantNumbers["Meaning of life"]

In Objective-C, the equivalent structure is the NSDictionary class. Here’s the old-school Objective-C equivalent to the code above. Since NSDictionary stores only objects, we have to wrap the numbers in NSNumbers using the new literal syntax:

NSDictionary *importantNumbers = [NSDictionary dictionaryWithObjectsAndKeys:
    @42, @"Meaning of life",
    @16309U, @"USS Reliant prefix code",
    @3.14159F, @"Single-precision pi",
    @3.14159265358979, @"Double-precision pi",
    @6.0221415E+23, @"Avogadro's Number",
    nil];

Note that the list have to provide the dictionaryWithObjectsAndKeys: method lists the value first and the key second, which is the reverse of how most other programming languages do it. As with NSArray‘s arrayWithObjects: method, we have to mark the end of the list with nil.

If you thought the code above was a bit much, imagine what it would look like without NSNumber literals!

As for accessing values in the dictionary, here’s the old way of doing it:

[importantNumbers objectForKey:@"Meaning of life"]

That’s a lot of typing just to get to a value.

Initializing NSDictionary and Accessing Its Elements the New Way

Luckily, the Objective-C that you get with XCode 4.5 gives us dictionary literals. Here’s how you define importantNumbers now:

NSDictionary *importantNumbers = @{
    @"Meaning of life" : @42,
    @"USS Reliant prefix code" : @16309U,
    @"Single-precision pi" : @3.14159F,
    @"Double-precision pi" : @3.14159265358979,
    @"Avogadro's Number" : @6.0221415E+23
};

That’s much better. Languages like JavaScript, Python and Ruby have standardized curly braces ({ and }) for specifying dictionary-style collections, and it’s nice to see this syntax in Objective-C. The order is also what we expect: key first, value second. And finally, no need to use nil to terminate the list.

The syntax for accessing a value from an NSDictionary is also much simpler:

importantNumbers[@"Meaning of life"]

Much better.

Initializing NSMutableDictionary Using Dictionary Literals

Like NSArray, NSDictionary isn’t mutable, but it has a mutable subclass with a similar name. It’s NSMutableDictionary, and like NSArray, you can use a literal and the mutableCopy: method to create one:

NSMutableDictionary *importantNumbers = [@{
    @"Meaning of life" : @42,
    @"USS Reliant prefix code" : @16309U,
    @"Single-precision pi" : @3.14159F,
    @"Double-precision pi" : @3.14159265358979,
    @"Avogadro's Number" : @6.0221415E+23
} mutableCopy];

As with NSDictionary, you can access elements of an NSMutableDictionary using the new syntax:

importantNumbers[@"Meaning of life"]

And since it’s mutable, you can make changes:

importantNumbers[@"Meaning of life"] = @43;
[importantNumbers setObject:@2012 forKey:@"iOS 6 launch year"];

Available Now, and Not Just for iOS 6

The latest version of XCode, 4.5, includes the latest Clang compiler, which supports these new syntaxes. Since they’re syntactic sugar and not part of any additions or revisions to the Foundation framework, you can use them in any iOS project.

Better still, if you’ve got old projects that you’d like to update to use these new syntaxes but dread having to do so manually, XCode 4.5 has a feature you’ll like. Under the Edit menu, you can select Refactor, and inside that submenu is the Convert to Modern Objective-C… command.