Thursday, August 1, 2013

Objective-C, Generics, and Parametric Typing

One question I see frequently asked and mis-understood:

Does Objective-C has Generics like Java or C#? 
The short answer is ...
No, Objective-C does not currently support parametric typing for collection elements.
However, this topic is more complex than the question or many existing answers admit..
Parametric-Typing for collections in Objective-C would not be the same as Generics in C#/Java. For example, it is unlikely you would ever see Objective-C add the capability to assure every object added to a collection IS an NSArray type or subtype. Instead, Objective-C could (and IMO should) have the ability to assure every object in a collection CONFORMS to a protocol/interface. (i.e. that it implements a set of required methods)
Why?
Objective-C is a language built on protocol (interface) compatibility, NOT subtyping relationships. That is, objects are compatible if they have all the right methods, we don't look at or care about their actual types. In fact, looking at actual types is a very very bad practice in Obj-C and in highly discouraged. This notion is sometimes called "Duck Typing", because if it quacks like a duck, it's a duck. We don't care if it literally inherited from some specific duck or not. This prevents you from being saddled by someone elses implementation hierarchy. -- The result is that as long as an object coming out of the list has a draw:: method it works, we don't actually care if it is a subclass of some specific JimmyDrawableBase object.
This not only makes code more reusable, but it also encourages a slightly different (more functional?) type of problem decomposition, because you can't rely on objects being derived from a given base class and thus having a bunch of your base-class implementation forced into them.
I personally think it would be nice for the Obj-C compiler to have parametric checking of PROTOCOL*CONFORMANCE*. That is, to make a NSMutableArray which requires all objects placed in it conform to a given protocol (i.e. have a given set of required methods).
Sometimes even this more-flexible protocol-conformance checking is resisted by dynamic programming folks, and with sound reasons. Programmers often have a way of over-specifying conformance requirements.
For example, you might require a list contain objects conforming to the NSArray protocol/interface, but you might ACTUALLY only call two of those methods. This is over-conformance. Someone who wishes to stick a compatible item in your array is forced to implement a ton of methods you are not actually calling -- at least not yet (see next).
Google Go tries to solve this problem by inferring structural compatibility. That is, if you call draw() on items coming out of a list, then the compiler assures everything going into a list contains a draw() method. If it does not contain a draw() method, it's a compiler error to put it into the list. This prevents the code from simply causing the same error to occur at runtime. The problem with this is that it only works for whole-program compilation. If Google-Go could compile modular DLLs (which it can't), then it would run into the problem that there isn't a way for me to say objects in the list need to support a specific interface of three methods, even though I'm not calling them today, because I might call them in the future.
Between those two solution likes the tradeoff and the truth.
Personally, I would like to see Objective-C add parametric protocol conformance, so I could ask the compiler to assure the contents of a particular collection always conform to a given set of protocols.
I would also like the compiler to help me avoid over-conformance. If I'm not calling methods in those protocols on objects, it should generate errors/warnings telling me so. If I want to keep them in the protocol even though I'm not using them, I should have to explicitly make a declaration for each method in the protocol that it "might be used in the future, so elements all need to supply it now". This at least makes the process of over-conformance require MORE work, instead of Java/C# where it requires less work.

Friday, June 27, 2008

Dissecting emotionally moving dance

You might not know it from the stream of techie blog posts, but I'm a giant fan of the hit talent shows American Idol, and So you think you can dance. Take a peek underneath why Katee and Joshua's performance to Jordin Sparks' No Air, designed by the amazing choreographer duo Tabitha and Napoleon D'Umo reaches your emotional core and is so moving.


Dance may impress us because of stunts, tricks, and gymnastics. We may gasp or chuckle, for the same reason we stare with our mouths open when Michael Jordan or Dominique Wilkins leave the ground to slam dunk. We simply don't understand how they can DO those amazing feats of talent and strength. However, this moving performance was more than strength and technical excellence. It was moving music; dancers becoming actors; and coreography design that uses synchronization of the action and music to create a mental 'strobe' effect, where we feel like the messages that only last a split second linger, and piece together a story.

Be sure to have the video up. Here is a higher quality version, though the times are slightly offset from my writeup below.

Let's take a look at how many perfectly timed movements Katie and Joshua hit to sell it home. These are dance elements and acting synchronized to the music with sub-second precision. If it seems impossible to unravel this kind of detail, you should know I had help, in the form of the Enounce MySpeed plugin, that lets me change the speed of flash-playback to slow it down. I assure you, your brain works fast enough to process and understand all this information as it happens, even if you didn't realize it.

  • 0:09 - joshua's kiss on her forehead says he cares
  • 0:14 - katie opens the letter to full length
  • 0:16 - katie turns her head and her eyes land on joshua, her movement says disbelief
    • katie stands and walks away to console herself
    • joshua's head turn shows he sympathetically wants to follow her, he tells us to follow her, but he also shows he cannot. she is alone
  • 0:22 - joshua's head turn snaps back into his own world to let us shift our full attention to katie as her sadness over the letter explodes
  • 0:22-0:34 - Katie's rage
    • 0:22-0:26 - rapid sub-second synchroized submarks show her anxiousness.
      • 0:22: - right leg pump
      • 0:23: - left leg pump
      • 0:23: - right leg pump
      • 0:24: - left leg pump and turn
      • 0:25: - paper toss
    • 0:27 - katie's chestpump, lyrics "you took my breath away", and her facial expression are telling us she's terribly upset about the news
    • 0:29 - katie's two handed point is directed at "you" in the audience, synchronizing in your mind the word "you" with the song's lyrical "you". If your brain was distracted watching some random motion she did, you wouldn't process the lyric, it would be background words... She just told you to listen, she told you she's speaking (to joshua), and her words she wants us to understand are "losing you is like living in a world with no air". It's the synchronization of the events that makes it possible for us to pay attention to both.
    • 0.29: - 0:34 - katie continues her emotion
    • 0:33 - katie's violent body movement mimics the look of someone being punched or hit in the stomach. We see it's the message in the letter is hurting her.
  • 0:34 - our attention turns back to Joshua as he begins to move again, timed with Chris Brown's verse in the song, to show us Chris is speaking his words
  • 0:34 - 0:42 - joshua makes his way to katie, his movements are not directly to, but face us, as the lyrics explain "i'm her alone, don't wanna leave".
    • 0:31 - 0:32 - His movements are side to side, as if he's trying to find her, making his way to her.
  • 0:42 - joshua reaches katie and reaches around her. However, he doesn't just hug her, his hands grab her hands. We can tell his embrace is gentle from her reaction, but there is something more from the way he's grabbing her wrists.
  • 0:42-0:45 - Timed with the music he forcefully manipulates her, again causing a synchonization between him making her move and the lyrical phrase "make you understand". His movements have sub-second timing, perfectly synchronized with the music beats. Notice how katie's not looking at us at all. We can't see her face, it's looking down to the floor and hidden behind her hair. We're listening to Joshua. This is my favorite sequence in the dance.
    • 0:43.4 he lifts her arms a little
    • 0:43.9 he lifts her arms further
    • 0:44.3 he pulls her arms together (her head falls, showing us it's not working)
    • 0:45.2 he pulls her right leg out
    • 0:45.6 he pulls her right arm up
    • 0.46.2 he pulls her right arm down
  • 0:46.2 - katie reacts, he face comes into view. We don't see a 'dance move', we see her face. She's acting, and we recognize the emotion on it instantly. Whether you realized or not, your brain noticed. Look closely. She's in agony.
  • 0:46.6 - Katie begins to move again. She moves her arms out, and Joshua's fast arm movements look like they were 'throw' outwards. Together the two of them show us that she 'broke free' and he's not able to 'make' her understand anything.
  • 0:48:52 - Chris Brown's lyrics and Jordin Sparks' begin, but both of them are moving this time. We know what this looks like, it looks like a conversation. One where they are both paying attention to eachother. Where they both care. Watch their perfectly timed synchronized movements.
  • 0:51 - 0:53 Watch katie's face. Despite the two of them dancing together in perfect rythem, her face locks on Joshua's. Again, she's not just dancing, she's acting. More than any other part of the dance she's showing us that her attention is focused on him and she's speaking to him. "..how do you expect me, to live alone with just me.."
  • 0:54 - katie is laid back, and joshua lifts her up and rotates her over her shoulder as the lyrics call out "'cause my world revolves around you..". I don't think most of us has as strong a connection between seeing that motion and the world 'revolve', but it's a similar effect to what occured above, with "you" and "make".
  • 0:57 - katie pushes joshua to the floor, synchronized with the word "hard", in the lyrical phrase "it's so hard for me"
  • 0.58 - Joshua's back leg is up in the air and his face is pressed into the floor, showing us he's hearing her message. She presses down on his leg, softly lifting him from the floor back up during the word 'breathe'. This is my second favorite element of the performance. This move is fluid and perfectly timed, and involves both of them participating precisely. The soft upward motion reminds us of breathing, a soft upward motion of the chest as we inhale. 0:59 - 1:20 - duo dancing resumes as the conversation as the lyrics include both Jordin Sparks and Chris Brown singing in unison.
  • 1:20 1:22 - katie has subtly moved herself back to joshua for my third favorite move of the performance, the embrace.
    • 1:20 - Their heads are perfectly aligned as they tilt together. To us they appear to be tilting in the same direction, but remember, this is a conversation between the two of them. Imagine katie is turned around facing him. When do you tilt your head to the side? Look at the clip and see if you see it. They tilt their heads to the side because they are going to hug.
    • 1:21 - She turns her head around to look directly at Joshua, and moves her hand across. His hand is waiting for hers, and they embrace.
    • 1:22 - She starts facing us for the performance, but his hands are around her waist. We see him hugging her.
  • 1:23 - Are you ready for it? The big shocker? He turns her around and lays her down (on his leg). Look at this position carefully. He not only loves her, he's making love to her. Yes, at 1:23, that pose isn't just some random dance move in the middle of some music. It means sex. (sorry kids).
  • 1:24 - He pulls her up close to him, pulls himself inside her as she reaches the climax
  • 1:25 - She slides down and away from him. Their lovemaking is completed.
  • 1:26 - still holding hands she twirls. for just a moment she's actually happy
  • 1:27 - She shows her face. We've seen it before. We recognize it. It's agony. She's come back from the fantasy of lovemaking and has remembered the reality of the situation.
  • 1:28 - she bursts out, wanting to return to her emotions. However, this time instead of letting her go, he holds onto her. He stops her from entering her rage.
  • 1:29 - She's limp and falling to the floor as he pulls her back to him.
  • 1:30 - He shares his strength, and lifts her up off the floor. Tells her it will be okay, that he's taking care of her.
  • 1:32 - They separate, and she's standing up on her own again. She's not in a rage.
  • 1:33 - He begins to walk across, she watches him, but she's accepted it
  • 1:34 - As he passes by, she takes the sleeve of his shirt, and removes it to keep a piece of him with her
  • 1:38 - He looks at her, both accepting it, and suprised. He's accepting it, and accepting her keeping a piece of him.
  • We feel exhausted from their turmoil, but content that they resolved it.

It took me about two hours to dissect this video. Imagine how long it took them to perfect it. I realized quite a few things that I didn't catch while watching it at normal speed. However, you can be sure my brain caught them every time. That is what made the peformance so moving.

Katee and Josuha, if you are listening, I'm in awe. Thank you for sharing with us such an amazing performance. All that work you put into it paid off. We noticed every movement you did, and your perfection worked to bring us to an emotional state that we loved!

Tabitha and Napoleon D'Umo, if you are listening, I'm in awe. When they later praised your work as 'lyrical hiphop' it was an understatement. This is choreography, and screenwriting, and storytelling, and hiphop with detail at so many levels. Wow.

Friday, February 20, 2004

100% pure java? I'd rather 100% viable Java.

 In the Feburary 2004 issue of Java Developer's Journal, Joe Ottinger makes a soft case for Java staying pure and against SWT. Whenever I see an article supporting the 100% pure Java mindset, I softly whisper to myself 'you just don't get it'. When I'm writing software, nearly everything is negotiable. The language, the development environment, the team size, the development process, the budet, the delivery date. Everything EXCEPT the user-experience. If one of the factors gets in the way of the user experience, it's out. This isn't about what Java UI looks like or how it deploys. This is about whether Java is a viable way to develop great applications.


This is an area where I don't understand the pure-Java perspective. This isn't a matter of whether my application has a native UI or not. This is about whether Java is going to let me build the best application I can or not. If not, it can go swim in the bit-bucket. I'll write it in C#, VB, Delphi, C++, C, or even assembly if that is what is necessary.

This isn't just my philosophy, this is a pattern in all high-volume software markets, because the development cost is so small compared to software margins. Imagine a discussion about writing XYZApp in Java. First they might talk about how they could save 50% of their developer resources. A quick calculation would reveal that this is 0.4% of their revenue on the product. Then they would discuss all the ways that the resultant UI and performance would suffer. Someone would proove that they could aleviate most of the problems by writing a new low-level library. Just as the discussion is nearing viability, someone from Sun would carry in the 100% pure Java worship alter and tell them they shouldn't build a native library. The team would pull back out their list of required UI compromises and after the marketing folks explained that they will lose 30% marketshare for being so clunky, they would bury the Java discussion.

This happens across the industry. I've been involved in discussions like this a few times. You can recreate one of the highest profile scenerios by just inserting the words 'MSWord', 'JDirect', and 'Microsoft' in the proper places.

This is what Java is up-against. It is not up against weird internal SWT vs Swing philosophical discussions, or Java vs C# debates. Java is up against a viability wall.

SWT folks have felt the pain, and are trying to make Java viable as one of the ways to write the best Windows gui application. The fact that any of the Java community resists this is unfathomable to me. It is tantamount to saying, 'we don't want Java to meet your goals because they are wrong, our goals are better'. To which I say, 'great, then Java is a religion not a development tool and I don't have time for religion, I have an application to ship'.

SWT is really only one of the many hurdles. The next hurdle is garbage collection pauses. Until Java can deliver bounded maximum pause times, it will remain unacceptable for many applications. It's tempting to belittle this is as only affecting a small number of applications, but if you try to use something as simple as a Java word processor or file manager you will see that it affects almost all interactive GUI apps.

I recently rejected Java for a large high-performance web application and used Python/C instead because of problems with
Java garbage collection pauses. Python is dog-slow, and it's pretty ironic that the resulting application meets our requirements in Python but didn't in Java. This is a testament to the simplicity of reference counting and the power of a large core library written in C.

--

Sometime around 1990, Modula-2+ delivered a strong typed garbage collected environment. Since then I've been anxious to move all my application development over to a similar system. I'll do so as soon as that environment gives me a viable way to develop best-of-class
applications. However, the reality is that, even today, with modern
environments like Java and C#, this isn't possible.

This is the battle Java is fighting. The battle for viability.

--

A few responses to comments:

- I have tried every set of garbage collection options there are on Sun's JVM and JRockit. JRockit claims to have lower pause times, but the application itself is perceptably more jerky. Sun's alternate collectors do not solve the problem.

- The maximum acceptable page-render time in my application is 0.5s, which means the maximum acceptable garbage collection is closer to 0.2s. My tests yeilded 300MB heaps with 1s+ worst case pause times. In real-world scenerios the heaps would be bigger. The problem with most of the different GC strategies is that they address average case GC times more than worst case.

- I am not willing to compromise my idea of what a 'best of class' GUI application is like many of you are. I'm not going to list off all the ways in which Swing does not act like a great Windows app, it's a waste of my time. I'll just list the first one. Real-time window resize does not work. When Sun has fixed that one, I'll give you the next 30.

- If you want an example of a Python/C application and the constraints, take a look at one of my recent projects. eGroups.com, now Yahoo! Groups. Pageviews per day in the hundreds of millions, tens of terrabytes of data. No GC pauses.