Saturday, July 31, 2010

Weekly roundup: vacations

I'm programming vacations, looking for a beach in Italy to go in September. Taking a break after a long period of working overtime due to university courses will be important to recoup energy.
You may have noticed I have returned publishing original articles also to this personal blog thanks to the free time I have now.

Here are the articles of mine published elsewhere on the web during this week.

Firebug is beautiful
Practical PHP Patterns: Transform View
A Dojo primer
Practical PHP Patterns: Two Step View

PHP 5.3.3 and 5.2.14 are out

Wednesday, July 28, 2010

Missing the point (OOP in scripting languages)

Yesterday I came across a question: Do Web-Scripting Languages Really Need OOP?
Here's my answer: only if you want to do more than an Hello World script (which is paradoxically how old school programmers measure the utility of a language.)
I'll express some of my thoughts without compromises, which will be up to you.

The author is equilibrated, as he saw the design simplifications that object-oriented programming introduces. However:
But our main point is that use of PHP constructs for OOP is a very “tradeoffy” and pragmatic decision, which we have often seen made more on the basis of religion or fashion. If you are comfy with OO, this kind of syntax is there for you; and if you work in a group that has decided to write in that style, you may want to let the majority rule. If you decide not to go OO, however, be strong—we urge you not to be swayed by the moral-superiority arguments you may hear from people who disdain your five-line procedural script in favor of their ten-line OO script that does exactly the same task.
This is not about 5 lines vs. 10 lines. If your projects are that long, go ahead. But hasn't the fact that every PHP framework is object-oriented told you something? The web is evolving from displaying documents to executing applications in the browser. The complexity will only increase in the future. When do you encounter 5-line script anymore?

What worries me are some of the comments:
However, in the PHP community there are very strong tendencies to cargo cult programming. Private methods and attributes make sense in Java, from where the idea was copied, but it adds little structural integrity in scripting languages (that's why Python eschewed them). But still, many PHP coders consider it "right OOP" to mark as many attributes as private as possible; neverminding that it only shadows underdesigned interfaces.
Private properties are available for encapsulation, not for structural integrity. I mark items as private so that I can change them in the future while only looking at the source code of their own class, and never worrying about external dependencies.
The likewise silly namespace\syntax also gets quite overused already. Deeply nested namespaces make sense in Java, but add little of the pleaded name conflict protection in PHP. It's just another syntax construct that gets used purposeless because it's hip. 
Namespaces are good because they remove duplication: you refer to the full class name (which can be quite long, such as Zend_Controller_Action_Helper or Zend_View_Helper_Url) only one time, at the top of your source file.

I'll give you one single reason to switch from procedural to object-oriented programming: seams.
In plain old structured programming, functions refer to other hardcoded functions, in a rigid graph. In OOP, object are (should be) wired at runtime, during their creation phase. It's this mechanism that makes practices like unit testing and polymorphism possible.

Monday, July 26, 2010

From static class to real object, and then from inheritance to composition

I'm refactoring Scisr, an automated command line refactoring tool, because it uses static classes as collaborators for the various Operation objects (Rename File, Rename Class, ...). In my refactoring, the static classes are becoming objects one at the time, and I inject those objects into the Operation ones.

Because of the quantity of collaborators, I am at the point where constructors are taking 5-6-7 arguments. But by no means this is a failure of Dependency Injection.

Using static classes means hiding the dependencies under the carpet, because you'll never know which objects access a static class from the signature of its constructor or of its setters. You must dig and look for :: operators (in PHP; in Java, for [A-Z]{1}[a-z]+\. or something like that; I'm glad Rasmus used a different operator for calling static methods).

Thus this refactoring uncovered a new code smell: the Operation objects have too much dependencies. Probably this is because common functionalities were refactored in abstract base classes instead of intermediate objects. This solution has great limitations, because the collaborators of an abstract class still must pass from the subclass constructor.
Furthermore, abstract base classes are not orthogonal: you can only inherit from one of them. Different functionalities end up in the same superclass only for convenience and not for cohesion.
The next step will be changing the design from this:
to this:
Possibly with more than one Collaborator, one for each use case where a subset of the old collaborators Db... were injected together. With this solution I hope to eliminate the explosion of the constructors of Operation classes.

Saturday, July 24, 2010

Weekly roundup: graduation week

Here are my original articles published elsewhere this week, between refining slides and receiving my scroll.

Practical PHP Patterns: Front Controller
JSP are more than templates
Practical PHP Patterns: Template View
Exceptional JavaScript

PHPDOCX: generating Word documents from PHP
ORMs and relational databases: powerful tools or dumb ideas?

I am Bachelor of Science and here's my thesis

Some updates on what's been going on here.
July 22th was the graduation day for the Bachelor's programs of Politecnico di Milano. I gave my presentation on my thesis project -  a multimedia search engine for musical videos which annotates songs with parameters like language, artist, lyrics and predominant emotion.

Video walkthrough (1:43)

Creative Commons LicenseMy thesis is now also available under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. It is in Italian, because the policy of Politecnico di Milano is to keep discussions in Italian for the Bachelor's programs (while many Master's programs are entirely taught and managed in English). The thesis contains many examples on Test-Driven Development and build automation, describing my experience with tools like JUnit, HttpUnit, Selenium, Ant, Equinox & OSGi.

Saturday, July 17, 2010

Weekly roundup: talk preparation

My next talk will be at graduation day on July 22th, in the Como campus of Politecnico di Milano. My long run towards the bachelor's degree is coming to and end and I still have to think about what to do with all the free time I'll gain now (apart working and open source).

Here are my original articles for this week.

Practical PHP Patterns: Repository
Web applications as enterprise software
Practical PHP Patterns: Page Controller
The absolute minimum you'll ever have to know about session persistence on the web

Ext4Yii, bridging PHP and JavaScript frameworks together

Sunday, July 11, 2010

Weekly roundup: returning to a personal project

This week I made some commits to a personal project of mine, NakedPhp, which aims to produce an implementation of the Naked Objects pattern (direct manipulation of domain objects via a generated user interface). It's quite nice to see that I was able to resume coding immediately thanks to the project's test suite, which covers both the acceptance and the unit levels. Tests are one of the best example of documentation you can maintain without hassle.

Here are my original articles for the past week.
Practical PHP Patterns: Metadata Mapping
Software engineering in the rail system
Practical PHP Patterns: Query Object
How to be a worse programmer, which has gained 17,000 views at the time of this writing.

Saturday, July 03, 2010

Weekly roundup: towards the PHP world

Next week I will hopefully be able to return to PHP, after having taken my last exam of the bachelor program at Politecnico di Milano next Monday, and waiting for the graduation day on July 22th.
My thesis is complete, as it needs only an abstract and a cover. I will probably publish it in the future, as it contains a good panoramic of acceptance-functional-integration-unit test practices (in Italian).
In the meantime, I continue writing as planned for DZone. Here are my original articles published this week.

Practical PHP Patterns: Concrete Table Inheritance
Web services in Java
Practical PHP Patterns: Inheritance Mapping
Web MVC in Java (without frameworks)