The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,899,515
since: 19 Jan 2005

Double-Click and Code Syndrome (DCACS)

posted Fri 16 Mar 07

So I was cruising along, clicking around in Interface Builder making a pretty rudimentary application tonight. I wasn't particularly concerned with how cool the app was or what it did - I was interested in the act of building the interface itself. What I was finding was that it took quite a few more keystrokes and actions to rig up a simple event handler in IB than it does in Visual Studio. At first, this frustrated me, but then I took a step back and tried to look at it objectively.

Let's take a simple example, which is actually the challenge from Chapter 4 of Aaron Hillegass' book. The goal is to have a text entry field, a button to click, and a read-only text label that displays the count of characters in the string when you push the button. So, if you enter "test", then click the button, the label's text changes to "test has 4 characters.". Pretty simple.

To do it in Visual Studio, I drop the UI elements onto the page, double-click the button. The event handler is automatically rigged up and inserted into the code-behind for the form. I would then add a line of code like this:

myLabel.Text = string.Format("{0} has {1} characters.",
    textBox.Text, textBox.Text.Length);

To do the same thing in Interface Builder and Xcode, I would use the classes list in IB to create a subclass of NSObject called AppController, then I'd create an instance of it. Then, I'd create an outlet for the text input field, an outlet for the label (also a text field), and I'd create an action called countLetters: that has code that looks like this:

 

#import "AppController.h"

 

@implementation AppController

 

- (IBAction)countLetters:(id)sender

{

NSString *source = [textField stringValue];

NSString *formattedOutput = 

[NSString stringWithFormat:@"%@ has %d letters"

source, [source length]];

[countLabel setStringValue:formattedOutput];

}

@end

At first glance, especially if you're used to Visual Studio, you're probably thinking that this seems like a fairly lengthy process to go through just to rig up a single event handler. Those of us who have been using Visual Studio since "way back in the day" might be included to think: Why can't I just double-click and code? Habitually, we tend to simply reach for a control on the GUI, double-click it, and start writing the event handler.

At this point, many of us developers begin to exhibit what I call "Double Click and Code Syndrome (DCACS)". Through convention, habit, and convenience we have lost sight of the pure intent and purpose of the Model-View-Controller pattern and we shun the 30 extra seconds required to rig things up in Cocoa because...well, it takes 30 more seconds.

In Cocoa, the controller is something that you actively choose to create. You can have multiple controllers for a single window or view if you like. And here's the important part: the controllers themselves are loosely coupled from the GUI itself. You can, in theory, re-use the controller among multiple applications provided the controller's purpose remains the same in each application. 

The feeling I get from Visual Studio is that when you double-click on things in the GUI, you are dangling tightly coupled event handlers off of the view, when the code really should be in a designated controller. Surely with the right amount of patience, discipline, and good naming conventions you can make that environment "feel" more loosely coupled, re-usable, and controller-like.

What I'm wondering is: as I learn Cocoa and use Interface Builder and Xcode, how many more bad habits like DCACS am I going to have to unlearn in order to make coding on the Mac a more enjoyable, professional-quality experience? 

tags:              

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. James Gregurich left...
Sat 17 Mar 07 10:26 pm

I spent 4 years as a VB 6 developer. I now do Mac development. What you are going to find is that doing things RIGHT in a microsoft environment is harder than doing things RIGHT in the Cocoa/IB environment.

IF you wanted to do proper design with VB6 (which I assume has a similar mentality to VS.NET), you have to treat the code you create button handler as VIEW code. This means you have to manually write your view and wire it up to the controller manually. WIth IB, the view is done for you as a graphical process and then you wire it up the controller as a graphical process. So, the MS way will actually mean MORE code and MORE complication than the Apple way if you do proper design.

In my opinion, as a general statement, MS tools are designed to allow low skilled development labor to program. The Apple/NeXT tools are designed to allow seasoned professionals to more easily build well-architected systems.

-James


2. Harvey Schwick left...
Sun 18 Mar 07 12:27 am

"You can, in theory, re-use the controller among multiple applications provided the controller's purpose remains the same in each application."

C'mon, no one's ever going to do that, are they? I think you're spot on about MVC being a better pattern than DCAC. I don't think this is why though.


3. Jack Finelli left...
Sun 18 Mar 07 8:00 am

Some neat direct comparsions are also possible if you try to design "XP Style Controls - in Cocoa".

http://www.kaintek.com/?p=34


4. GI left...
Sun 18 Mar 07 1:23 pm

Think about what you have to do when you change the UI ?

A lot of controller have the same kind of method : stringValue, setStringValue: ....

Then you can modify the UI, change the widget and reconnect then.

I think that it is very bad habit to mix code and UI.

Hypercard had the same kind of power, setup application with widget component, double click and code ... Very nice for rapid small appli, prototype but for true big application, you need to make more serious architecture.


5. Kevin Hoffman left...
Mon 19 Mar 07 6:16 am

GI - I couldn't agree more. The bad habits that being able to double-click and immediately start coding generates are awful. As I've mentioned a few times before, learning Cocoa is actually forcing me to unlearn quite a few bad habits instilled by Visual Studio


6. l0ne left...
Thu 22 Mar 07 9:35 am :: http://millenomi.altervista.org/blog

Re: Harvey, "nobody does it" is simply wrong. Many of the interfaces Apple uses (ie iTunes, or iPhoto which is Cocoa) use shared controllers with multiple views, reusing them logically as needed. Cocoa even implements most of the controller layer in a reusable fashion (see bindings/NSController) exactly to help implement this kind of things!


7. Keith Duncan left...
Sun 25 Mar 07 5:40 pm :: http://web.mac.com/keith_duncan

"You can, in theory, re-use the controller among multiple applications provided the controller's purpose remains the same in each application."

Thats very true, infact it goes for any well written component of the MVC framework. I use different combinations of my models, custom views and controllers in various apps. Adhering to the MVC pattern allows for very 'portable' code throughout your applications and *can* reduce the volume of code you have to write for a new project.


8. Alex Gordon left...
Tue 27 Mar 07 2:03 pm

That's funny because I'm finding the EXACT opposite.

I started learning VS+C# a couple of days ago (I've been coding in cocoa for years now). I am finding the double click behaviour really annoying. In interface builder, if you want to change the label of a control you double click it and you get a text field up. In VS, I keep double clicking controls to change their label and I get a code editor up. So this is something I will have to unlearn when coding in VS.

"C'mon, no one's ever going to do that, are they? I think you're spot on about MVC being a better pattern than DCAC. I don't think this is why though." Yes, I do. And while doubleclicking might save you 30 seconds, modular MVC will save you a day or more of pain.


Tag Related Posts

Would you like to touch my mono?

Mon 23 Nov 09 2:59 P GMT-05

Upgrading your Leopard install to Java SE 6 64-Bit

Mon 12 Jan 09 1:38 P GMT-05
tags:            

Apple drops the iPhone NDA for Released Software

Wed 01 Oct 08 3:54 P GMT-05
tags:          

Cappuccino, Objective-J, and You

Wed 10 Sep 08 6:14 P GMT-05

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags: