Sunday, May 16, 2010

Model View Controller

Straight to the point: in a MVC architecture, do Model and View need to communicate directly, or all the communications should be mediated by the Controller? There are two schools of thought regarding this topic, and the question often generates little funny religion wars.
The best answer to this question which I have ever found is in the Cocoa Design Patterns document, available on the Apple dev center website (by the way, the document is a recommended reading regarding design patterns in general, whatever the language, platform or technology you use).

To sum up, the document describes both models, named "traditional model" (in which there is direct communication between model and view) and the "Cocoa model", in which the Controller always mediates between View and Model.

Here is the "Traditional" MVC model:

Here is the "Cocoa" MVC model:


One of the main arguments in defence of the second architecture is that it theoretically improves the reusability of View and Model, because they are totally decoupled.
Read Cocoa Design Patterns for a thorough discussion!

Iphone Application startup: the complexity behind the scenes.

Let' write an IPhone application which displays a single "Helloworld" label at the center of the display.
It's plenty of tutorials and books which explain how to do that: usually they explain how to use a View Based project template in X-Code, add a label with Interface Builder, and run the application.
Very easy.
But have a look to the set of files generated by Xcode while choosing the View based template: you will see a main method, an Application Delegate class, a ViewController class, a plist file, and two .xib files which define the main Window and the main View of the application.
Also, you will notice that the main .xib file is referenced in the plist file (as the "main nib" ..), and that it is made up of five objects: a UIApplication, a UI Responder, the Application Delegate, the View Controller and a Window Object. Also, some references between objects (or, more precisely "Outlets") are defined: the UIApplication contains a reference to the Delegate Object, which in turn contains references to both the ViewController and the Main window. Finally the ViewController is owner of the second .xib file, which defines the actual interface of our application and is connected to our ViewController class by the usual mechanism of Outlets and Actions.
Quite complicated for a simple HelloWorld application!

Fortunately, somebody (Bill Dudney) already made an effort to put all the pieces together and explain very clearly how they all work together: Demystifying iPhone App Startup
Really a great post, highly recommended!

Sunday, May 9, 2010

Did I write that code?!?

Preliminary warning: this post is not worth reading for you if you never make mistakes.


Illusory superiority is, according to Wikipedia's definition, "a cognitive bias that causes people to overestimate their positive qualities and abilities and to underestimate their negative qualities, relative to others"
It is that well-known, well documented phenomenon (also known as the above average effect) that makes so easy for us (T)IT professionals to judge and criticize other people's work. It's a fact of life.
In other words, we tend naturally to have a distorted perception of reality when it comes to evaluate the quality/effectiveness of our work compared to the quality/effectiveness of the work of our colleagues.
It is extremely therapeutic, in order to come back to reality, to periodically try to spot major flaws in your own work: poor design or shitty code, completely wrong approach to problems, wrong organizational choices. Try to be as cruel as possible (which means: as cruel as you would be if you were analyzing other people's work).
The epiphany will occur when you'll find yourself wondering, confused: "did I really write this code?" (or "did I really choose this process? did I really proposed this solution? did I really ignore this issue?", depending on what your actual work is).
Everybody makes mistakes, experience allows us to improve ourselves recognizing them, but this virtuous path requires humility, as written in almost all books of self improvement written by spiritual gurus, genuinely illuminated guides or simply people which love making money selling books about this kind of stuff (And they actually sell them: smart guys, definitely above the average).