Objective-C discourages good OO design/code?

I started learning Objective-C when Apple released the iPhone SDK over a year ago, and started programming in it seriously at the beginning of this year. While there are many things I like about Objective-C as a OO language, there is one thing that continuously bother me.

One of the four main tenant of object-oriented design is Encapsulation. Meaning, the inner working of an object is hidden from public view.

In Objective-C, an instance method can be declared in the implementation file (.m file) in the following ways:

  • Implement the method without declaring it in the header file. This is (almost) equivalent to private method in C#/Java.
  • Declare the method signature in the header file, and implement the method in the .m file. This is like declaring a method public in C#/Java.

So how does this discourages me from writing good OO code with respect to encapsulation?

If I choose the first option, I have two choices. Either I implement the method before its first usage which does no good with readability re Uncle Bob’s Clean Code‘s newspaper metaphor, OR implement it after and put up with the compiler warning about the method call may not exist.

To get the freedom of placing the method anywhere in the .m file, I have to choose the second option and declare the method signature in the header file. The downside of this is that now the method is exposed as part of the class public interface and break encapsulation. (Yes, I know that the method can still be called without the header file declaration. Again, a compiler warning greets you.)

All three options are undesirable to me. It is really a case of pick my poison! Right now, I choose option one and put the method before first usage. Readability suffers because I like reading methods after the usage but at least the header file is clean and represents the intended public interface.

Update: Martin Pilkington makes a suggestion to me via Twitter. I’ll have to try it out and see.

Update #2: Someone else on Twitter also suggests using Extension to solve this issue. The Apple’s documentation here (at the end of the page) shows how an extension of a class can be used to define private method, separated from the main class interface definition.

My initial feel? Pretty inelegant workaround to an inherited problem of Objective-C legacy linkage to C. No thanks, I’ll stick with declaring private methods before usage.

Philadelphia Experiment

Leah and I went to Philadelphia on a weekend trip this past weekend. For me, it was two days of experiments. All of which are for our trip to Naples/Pompeii later in July.

Canon 50D
The 50D is a vast improvement over my old 10D, bought way back in 2003. The increase in pixel count is obvious, but it is other much more subtle features that impress me. First, the quick startup time from sleep mode. The 10D takes about 5-10 seconds to wake up, thus sometimes I would miss shots. The 50D wakes up and ready to shoot almost instantaneously. The LCD screen is of course larger but it is the colour rendition that makes me take notice. With 10D, the LCD is always too bright and colour too saturated. The screen is still slightly too bright but the colour is almost spot on so preview is actually accurate. The low(er) noise level on high ISO settings makes it, for the first time for me at least, viable to shoot indoor with ISO1600 without worrying about unusable image. Images are still noisy but at least it is manageable.
Kata DR 467
I've been using a camera hipbag inside my Oakley backpack on trips for a few years now. Mainly so it is not obvious that I'm carrying an expensive digital camera. While that worked fairly well, it was awkward to store and retrieve the camera as I have two bag openings to deal with. Alex Lindsay (from PixelCorp and MacBreak Weekly) on Twitter recommended Kata DR-467 for travelling with DSLR. I looked online as well as touched it for real in B&H before deciding to buy it. After this weekend, I was so glad I bought it! I can't believe I didn't get it earlier. The slide out compartment at the bottom for the camera and lenses are the best feature, making it very easy to store/retrieve camera on the go.
GeoCoder
I've always wanted to geotag my photos so this trip was a perfect opportunity to utilise the built-in GPS in my iPhone to try that. I found on the AppStore this free app, GeoCoder, which claims to make geotagging easy. It was pretty easy to use and I just needed to remember to start a 'recording' in GeoCoder at the beginning of the day and stop it at the end. I was afraid that it would drain my battery quickly but it turned out not to be the case.
I was pretty excited about this whole geotagging thing until I got home and tried to use the GPX files GeoCoder produces in Google Earth. Turns out the iPhone GPS receiver is not sensitive enough to pick up GPS signal when the phone is in my pants pocket! So all I have are a handful of GPS locations whenever I took my phone out for a quick Twitter check or the iPhone happened to have enough signal.
So GeoCoder is out for me for the time being.
For the Naples/Pompeii trip, I have considered using one of my iPhone case and strap it to the Kata's shoulder strap in order to get decent GPS signal for the iPhone. Or I can purchase one of the cheaper GPS recorder and use that instead. I'd probably still need to expose the recorder to the sky somehow though.

Read and post comments

|

Send to a friend

Website Built with WordPress.com.

Up ↑

%d bloggers like this: