Never type hint on arrays
Let's be controversial: In modern PHP, you should never type-hint an array.
Before you start throwing tomatoes, hear me out.
I am all for testing. Whether you always write unit tests in advance as Test Driven Development (TDD) advocates call for, write them after, write them alongside, a little of each, I don't care. Tests are your friend. You want tests, and specifically you want good tests.
There's a lot of opinions on what constitutes a "good" test, of course, and much is subjective to the type of code you're working on. However, since the release of PHP 7 I've found that while writing tests... I am never using a mocking library. In fact, I'm going to go as far and say that
You should never use a mocking library in PHP 7.
Before all of you gasp, clutch your pearls, and send ninja hit squads after me, let me justify that position.
Let's be controversial: In modern PHP, you should never type-hint an array.
Before you start throwing tomatoes, hear me out.
The other day I was working on some sample code to test out an idea that involved an object with an internal nested array. This is a pretty common pattern in PHP: You have some simple one-off internal data structure so you make an informal struct using PHP associative arrays. Maybe you document it in a docblock, or maybe you're a lazy jerk and you don't. (Fight me!) But really, who bothers with defining a class for something that simple?
But that got me wondering, is that common pattern really, you know, good? Are objects actually more expensive or harder to work with than arrays? Or, more to the point, is that true today on PHP 7 given all the optimizations that have happened over the years compared with the bad old days of PHP 4?
So like any good scientist I decided to test it: What I found will shock you!
Recently I was working on some PHP code (for the PHP-FIG) that involved code generation. Lots of systems these days are doing code generation (compiled dependency injection containers, ORM classes, etc.), but surprisingly I've avoided having to touch that code myself until now.
Of course, like any good developer I was writing tests for it as I went. That meant needing to test that the generated code was valid PHP syntax and did what I wanted it to do.
(Continue reading this post on Steemit.)
One reason to follow development mailing lists is you sometimes pick up on some very neat tricks. Here's one that I spotted on the PHP Internals list recently to simplify array iteration in PHP 7.
Earlier this week a fellow PHP public figure tweeted saying that I write great session proposals for conferences. After I finished blushing I reached out to him and we talked a bit about what it was he likes about them. (Whatever I'm doing right, I want to know to keep doing it!)
Based on that, I figured it would be educational to dissect how I go about writing session proposals for conferences and hope that conference organizers don't catch on to my tricks. :-)
Articles, blog posts, and Twitter debates around "best practices" abound. You can't swing a dead cat without bumping into some article espousing the benefits of designing and building software a certain way. (Side note: What kind of sick person are you that you're swinging a dead cat around? Stop that! You're desecrating the dead!)
What nearly all of these articles fail to convey is that "best practices" are contextually-dependent. Always. For one, they evolve over time; really, there is no such thing as a "best practice"; There are "leading practices", which is an inherently changing and growing definition as the technology and market changes and as we as an industry learn more and discover new and exciting ways to royally screw up.
Well, here we are again.
Most have by now seen the latest post from Dries Buytaert and Megan Sanicki about the reasons they saw fit to remove me from Drupal. Unfortunately, as has been the pattern it seems to raise even more questions than it answers, including from me.
This is a very uncomfortable time for Drupal. It seems there has been a great deal of concern and discomfort lurking just below the surface for a while, and recent events surrounding me have brought them all to the forefront at once. It's going to take some time to unravel it all. Hopefully it can be a learning process for many.
For the moment, though, there's one particular point I want to address, and particular people I want to address it to: Don't be an asshole, not even in my defense.
I did briefly speak with Dries in private recently about recent events. I cannot discuss details of our conversation, but there is one point he mentioned that I feel warrants an immediate response.
Since my last post Dries and Megan have issued a new joint statement on the ongoing mystery regarding my removal from DrupalCon and the gossip campaign against me. To date, I have focused my public posts mainly on my experiences, my beliefs, and the prejudice with which I have been treated by select members of the Drupal community and by select (if powerful) members of Drupal's leadership. However, I feel the latest joint post from Dries and Megan last Friday deserves a more direct response.
Some commenters noted that it doesn't add anything or reveal any additional information. I disagree. It makes implications and innuendo that are sufficiently untrue that I fear, if they have not crossed the line into libel, they certainly teeter on the precipice.