programming

PHP Mode Strings with Stat (and Stream Wrappers)

This article explains how to work with mode strings in stream wrappers, and in low-level calls to PHP's stat() functions. It will be helpful to those writing stream wrappers that must respond to functions like:

  • is_dir/is_file
  • file_exists
  • is_readable/is_writable
  • stat

Stream wrappers must supply the right stat arrays for these, and the most confusing value that must be set (for non-C programmers, at least) is the mode string.

I start off with a very basic intro to mode strings, and then dive down into the details of setting mode strings.

Documenting PHP with Doxygen: The Pros and Cons

DoxygenDoxygenIt's been a few years, now, since I gave up using PHPDocumentor to document my PHP projects. I switched to Doxygen, an automated documentation tool that supports a wide variety of languages, including PHP. While PHPDocumentor enjoys broad support in the PHP community, Doxygen, too, is well entrenched. (Drupal uses it.)

I recently began a new project from scratch, and it gave me an opportunity to once again turn a hard gaze upon Doxygen. After some careful reflection on my experiences developing this new medium-sized library and documenting it with Doxygen, here are what I see as Doxygen's strong and weak points when it comes to PHP API documentation.

Atoum: A Different Unit Testing Framework for PHP

For a long time, it has seemed that when it comes to unit testing in PHP, there is only one game in town: PHPUnit. And in all fairness, it's popularity is justified. It is a good framework, and can generally handle standard use cases.

But recently I found Atoum. Not being one to start small, I began using it on a large (soon to be Open Source) project that I was writing from scratch.

Was it love or infatuation?

Atoum has some great out-of-the-box features:

  • Support for PHP namespaces.
  • Each test can be run directly.
  • The CLI output is clean and intuitive.
  • I love the method chaining support.

Syntax Checking for Drupal in VIM

SyntasticSyntasticVim (VI Improved) is a powerful text editor that comes standard on most versions of Linux, OS X, BSD, and other UNIXes. With thousands of add-ons, console and GUI versions, and a fully scriptable environment, you can transform a humble text editor into a powerful development tool. In fact, there are several Drupal add-ons for vim.

In this article, I explain how to turn on syntax checking for PHP, adding code style validation along with error checking. We do this with three tools: The Syntastic Vim plugin, the PHP CodeSniffer PEAR package, and the Drupal Code Sniffer project from Drupal.org.

Is Size Code's Worst Enemy?

The Drupal codebase upon which I work is now over a million lines of code (excluding whitespace and comments). It sounds impressive. But the reality of the matter is that the combination of lots of code and the Drupal way of doing things makes it not impressive, but a maintenance nightmare. Nobody on the current team knows what all of this code does or what it is for. Even limiting things to the custom modules, there still is no longer any member of the team who knows the code well. This, of course, isn't a criticism of the team or even of the platform, but a reflection on what happens when a codebase balloons over the years.

Reading Steve Yegge's post entitled Code's Worst Enemy hit home the concern I have with our code -- and with Drupal in general. (Update 10/29/2011: Steve Yegge's the guy who accidentally posted the Amazon/Services rant on Google+, and who unintentionally "quit his job" in the middle of his presentation at OSCON.)

I suggest reading the entire blog post on its own, but here are several salient details that need explicit mention, and that have a Drupal context:

  • While some languages (Java) may exacerbate the problem, clearly ballooning code can happen in any language. And with a semi-opaque execution sequence (as we have in Drupal), the problem can be compounded by the fact that one cannot determine at a glance what code might be executed on a given execution. To know what code will be executed on a given request, you must know not just core and your own modules, but all of the installed modules.
  • Design Patterns might deserve a measure of skepticism. Steve's point is that relying upon them can introduce needless complexity. He uses Dependency Injection as an example. Too often, design patterns are introduced for their own sake or because they look similar to what we want to accomplish. But then the need to (re-)architect in terms of the pattern sometimes overshadows the original goal of accomplishing a task.
  • Copy-and-Paste (CAP) code is bad. Obviously. But because all of Drupal is a public API, I often see developers choosing to CAP code from function body to function body because they think that is more elegant than providing highly-contextual stand-alone functions that might be mistaken by other developers as "generally useful". (No, prefixing functions with underscores is NOT a good alternative. Lately, I've been encouraging developers to underscore all functions that aren't hooks or constructed callbacks because it's too easy to get hook/namespace collisions otherwise.)
  • Unfortunately, Steve doesn't talk about YAGNI ("You Ain't Gonna Need It") as a good design principle, but the converse of YAGNI -- that tendency to attempt to solve all possible cases before there are any actual cases -- is a dangerous tendency in software developerment that must be countered in the name of simplicity and maintainability.

Good PHP: Coding Standards and Why You Should Follow Them

In hindsight, I'm surprised how long it took me to develop a strong appreciation of code formatting standards. It's not that I haven't followed them all along (most IDEs and editors do the lion's share of that for you). What surprises me is that I never really appreciated the value of following them. But managing a codebase of over a million lines makes it readily apparent that coding standards are a big boon -- and that lapses in those standards adversely impact the entire team.

The primary reason for coding standards is this: humans are worse at syntax parsing than machines are. Coding standards exist to make the code easier for humans to work with, and they do this by making the code more amenable to visual scanning.

There are four benefits to be gained from following coding standards: reduction of bugs, preventing new bugs, lowering the learning curve, and easing long term maintenance. I discuss them below.

JavaScript, Node.js, and the Flying Wedge Anti-Pattern

The "Flying Wedge" is the name of a military formation in which troops would march into battle in a V-shaped formation. That same V-shaped "formation" unfortunately has a tendency to show itself in JavaScript and CoffeScript. And in the vast majority of cases, it is bad practice.

Code that's easy to read, modify, re-use, and debug is better code. A convention that hampers those goals without offering an overriding benefit is an anti-pattern. That, in a nutshell, is why the flying wedge ought to be avoided.

Here's an explanation of the anti-pattern, along with some suggestions for avoiding it.

Node.js Debugging with the Built-in Debugger

Node debuggerNode debuggerThe HTTP version of the app worked, but the commandline version did not. What went wrong? It was hard to say. The application simply hung, unresponsive. Try/catch and event handlers didn't find anything wrong, and my typical console.log() approach wasn't cutting it, either. I need to fire up Node.js's command line debugger.

This is a short tutorial explaining how to use the debugger that comes built-in to Node.js. It explains how to use the command line debugger to set breakpoints , step through the code, and analyze the debugging output. By the time you're done reading, you should be able to quickly debug your own code.

The Basics

  • Node has a built-in debugger
  • It can be executed using node debug your_scriptfile.js
  • In your app, you can set breakpoints using debugger;

Now let's look at each step of debugging....

Node.js: Five Things Every PHP Developer Should Know

I recently started working on a few Node.js applications. Coming most recently from PHP (and Drupal in particular), I found the transition to Node.js to be surprisingly easy. Pleasurable, in fact. But I had to learn to think differently about a few things.

Below I list the five things I think every PHP developer should know about Node.js.

Five Reasons for Doing Drupal Development on a VM

In years past, I used to do my development on a local machine, and then push my work to a remote server for testing. About two years ago, though, I switched my environment. I began using virtual machines instead of physical servers. Configuring them for Drupal, I could do my Drupal development locally, and then do advanced testing on my virtual machine.

In this article, I give five reasons why I believe Drupal development can be enhanced through using VMs.

Syndicate content