Thursday, September 30, 2010

Monkey patching in PHP

Don't do this! It's only curiosity that drove me to try.
A monkey patch is a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, etc.) without altering the original source code. -- Wikipedia
Till tried to do some monkey patching a while ago, using PHP namespaces. Basically, if you use a PHP function inside a namespace, the original function will be called if an override is not defined. In the example, calling strlen() inside of a namespace would refer to the NamespaceName\strlen() function, and if it not exists, it would fall back to PHP's native one.
The problem was that you actually have to namespace the original code. It would not be monkey patching anymore: if I could modify that code, I would simply change the function calls.
For example, we may want to monkey patch the following code:
$str = 'mystring';

echo "This should eight, but it's not: " . strlen($str) . "\n"; // 6

So what do we do? Basically, this evil black magic:
namespace monkeypatching;
function strlen()
    return 6;

$code = file_get_contents('monkeypatched.php');
$code  = 'namespace monkeypatching; ?> ' . $code;
echo $code;
which prints out:
This should eight, but it's not: 6
Of course this infringes every single SOLID and software engineering principle. Again, don't code like this, we are not in Ruby.
In general, methods and functions definitions are part of the mandatory global state of an application, and so once the loading phase has finished they should be immutable. This is done at the loading time, so it may be acceptable. But also dangerous: it's the poor man's version of polymorphism.

Wednesday, September 29, 2010

Grouply and connect with me

I have been receiving several emails with contact requests on Grouply.
I registered there, but Grouply required then my Yahoo! username and password to work, which I'll never enter on a 3rd party application (it's a very evil and dangerous habit.) If only they used OAuth or OpenID... (is the latter dead?)

I actually do not follow many mailing lists in this period, apart from zf-contributor. So if you want to connect with me, I'm on Twitter, and you will be sure that I read tweet where I am mentioned every day.
I do not use Facebook instead - except for people that I have already met out of the Internet (conferences, work, university). This blog and Twitter are really the best ways to chat for me.

Tuesday, September 28, 2010

Find the intruder

 1. We can connect the set-top box to the tv first, and when it goes online it will bypass the Scart signal to override both the Vcr and the Dvd player. For switching between those two instead, we'll use this Scart switch.
2. This MySQL database, which is populated by the turnstile at entrance of the school when students enter the building the first time, should be synchronized with the custome e-school software. We can easily make a PHP script which is run daily and selects the right rows and inserts them in the right place.
3. We cannot fix the ship's engines in time, and the shields cannot survive the lava flow, under which we will be trapped, for long. But we can to use the impending explosion itself as our ticket out: the volcano's cataclysm will launch us, and by using the shields for some instants, we can engage the hyperdrive for a split second to get them into orbit, where we can be saved.
4. You can boot from the Ubuntu 10.04 live cd and get a running system from here, then check if your modem is working. You'll install it over the disk with Windows XP, which is now too slow to use, only if there are no connectivity problems. Besides that, you can also install Ubuntu and leave an empty partition for reinstalling Windows on it later."
Now detect which ones are real engineers quotes (mine, actually) and which comes from Stargate Atlantis. After a summer marathon of three series I started sound like Rodney McKay when explaining engineering topics to naive people.
I guess that's why we (engineers) are almost always sci-fi aficionados: in science fiction engineers save lives and travel between galaxies in every episode, while we are limited to build some indeed very useful web application. :)

Monday, September 27, 2010

Speaking at PHP Barcelona 2010

I'll be presenting my talk Architecture and testability in its English version at the PHP Barcelona Conference. I gave the Italian version of this presentation at phpDay in May.

This is the abstract:
Testing a web application may seem an hard task, but nowadays it is a fundamental part of the development process that ensures the application does not experience regressions, and it is free to grow incrementally. My point is that testability is a property which is desiderable not only for the ease of building an automated test suite, but also for the positive effects on the overall architecture that maintaining an application testable at the unit level produces. For example, being able to test a class in isolation is a sign of enough decoupling of it from the other collaborators, while keeping a limit on test cases length forces the production classes to be cohesive in functionalities.
The treated topics range from Dependency Injection to the Law of Demeter to the inherent evil Singletons and static classes. I'm a practical guy, so I will show you code; but not so much that it isn't readable.
This is an occasion for everyone who wanted to see my talk at phpDay, but wasn't there, to get a nice wrap-up about why making your code testable improves its design.
By the way, the conference will take place on the 29th and 30th of October, and a two-day ticket costs only 60€, of which I receive nothing. Hope to see you in Barcelona. :)

Saturday, September 25, 2010

Mentioned in Clean code talk

I have been mentioned in the Clean code talk by @avalanche123 as a source of informations, and with this quote:
"Refactoring is a natural part of coding. You don't explain to management why you need to write while and for loops" -- source
Since I tweeted this phrase in quotes ("), we can assume it is not an original work of mine. I guess I heard it in some presentation, probably of Uncle Bob. What kept me from including the source on Twitter was the 140 character limit.

My Master's Degree specialization is in Data Engineering, and finding quotes in videos is one of the common task for multimedia search engines we study and research on. Maybe one day we will be able to find this phrase's source video with a single click, in the same way as we google a term today.

Weekly roundup: back from Siena

Original articles
Here are my article for this week on Web Builder Zone.
How to set up the Pomodoro Technique in your office, which introduces the Pomodoro Technique as a tool for time management even for teams and not only for freelancers.
Practical PHP Patterns: Layer Supertype, which talks about the common practice of extracting superclass, which is not always beneficial as it seems at a first glance.
Why a Pomodoro helps you getting in the zone answers the common questions on getting in the zone while using the Pomodoro Technique.
Practical PHP Patterns: Registry, which presents the Registry as implemented by Zend Framework 1.x and its inherent issues.

Siena, the city of Palio
After four weeks as a Software Architect in Siena, I'm back ready to begin my new university courses. I'll continue working for Allbus remotely for some hours a week.
The experience has been quite interesting, as it was essentially what I am used to do on an open source project, applied to a commercial one. This is another benefit of contributing to open source: it prepares you for the challenges of your work.
Besides that, living in Siena for some weeks was delightful, because of the city's beauty and population density. Walking to work instead of driving a car was particularly unusual.

Saturday, September 18, 2010

Weekly roundup: reaching the conceptual contours

This week at Allbus we have been extracting methods, interfaces and classes all the time. We are hitting the point when the refactoring is paying off and we can finally remove old, duplicated and ugly code, never called in our new routines.
Never stop refactoring and eliminating duplication. The answer to How many lines of code have you written today? should be something like -100.

Here are the articles for this week on Web Builder Zone.
Is graceful degradation dead?, which discusses the issues in accessibility of popular websites and enteprise applications.
Practical PHP Patterns: Mapper, which introduces the sibling pattern of Gateway.
Practical PHP Patterns: Separated Interface is about the management of dependencies by careful placement of interfaces.
From Subversion to Git in a morning describes how we did the migration, comprehending svn:externals conversion to submodules, in a few hours.

Saturday, September 11, 2010

Weekly roundup: new academic year

After a vacation on Tuscany's beaches where I was totally disconnected from the Internet and work-related topics, I'm ready to start a brand new year, first with two weeks of consulting business in Siena and then with the Master's Degree program at Politecnico di Milano. I will also attend the Alta Scuola Politecnica program, an additional set of courses for the students selected in the top 7.5% ones of every ordinary course.

While I was away, some original articles of mine have been published, and they are listed here.
Selenium is not a panacea, which outlines the common issues encountered while using Selenium for acceptance testing of complex JavaScript-intensive applications.
Practical PHP Patterns: Database Session State, which discusses the storage of session data into a database compared with the more common solutions of using cookies or the server's memory.
Practical PHP Patterns: Gateway, which is a basic pattern for consuming an external resource in an object-oriented, testable way.
What paradigm should PHP applications embrace? is a poll which compares procedural, object-oriented and functional PHP functionalities. Feel free to express your vote.

Sunday, September 05, 2010

Weekly roundup: vacation time

I'm leaving again in Tuscany next week, but this time for vacation on the Tirrenian Sea. You'll continue to see my articles published on Web Builder Zone anyway.

Here are my original articles for this week.
The different kinds of testing. You know, all those buzzwords such as functional, unit, acceptance, integration testing have a precise meaning.
Practical PHP Patterns: Client Session State, an explanation on how to persist information on the client via cookies, forms or URLs.
Practical PHP Patterns: Server Session State, which you can contrast with the earlier post on Client Session State.
How to build a Kanban board, a practical tutorial with photographies.

Image credits.