Declarative vs. Imperative: asking for soup vs. making soup

A little while ago I wrote a post about my concepts of declarative vs. imperative programming. I ended that article by saying, “I can’t wait to see what I’ll learn next.” Well, I’ve found another definition which has been very helpful to me lately. Also, I’m a little worried that it might actually be the opposite of my previous example. I guess that’s learning?

Declarative language describes what you want to happen, without necessarily explaining every step of the process to get there. “I want butternut squash soup.”

Imperative language describes every step toward making something happen. “I want a butternut squash that’s been cut in two, seeds removed, roasted, peeled, and blended with broth and spices.”

Of course, there’s quite a spectrum implied by those two words. How specific can we get in imperative language? “I want a butternut squash” already makes certain assumptions. How about, “I want an orange, long, vine grown winter squash”? This could go on all day. It really depends on the domain or the context in which your language will be used.

So back to programming. Most programming languages (at least the ones I’m familiar with) are decently imperative. With such a tool, you can use statements and conditions to tell the computer exactly what you want to happen and when.

On the other hand we have artifacts like HTML, the building-block of the World Wide Web. I know plenty of people who consider writing HTML to be using a programing language, and yet it actually falls much more on the declarative side of the spectrum. With HTML you describe what you want to happen to some content and each browser that reads that HTML must figure out how to do what you’ve asked. For example it may need to switch font weights if you’ve used a strong tag to wrap some text.

That said, declarative language relies on imperative language. Every declarative tool, like HTML, has been generated by humans wanting to simplify their work. They got tired of doing something repetitive and so they wrote a tool to convert it from a simpler form. Asking for soup is not very useful if no one in the room knows how to cook.

One more thought: writing HTML is declarative compared to, say, using JavaScript to edit the DOM. Yet there are other layers, like Markdown, which are in some ways even more declarative than HTML. It’s only in comparison that we can say that one thing is declarative and another is imperative, so always be on the lookout for other perspectives. And hey, that goes for the real world too.

Lightweight rendering of React to strings

For a recent project I needed to be able to render React components (or really, just React-like components) into plain HTML strings. Of course, React provides a mechanism to do this: renderToStaticMarkup in its ReactDOM library. But wow, that library is a really big dependency to import if I don’t want any of the DOM reconciliation stuff.

Also, it turns out I don’t even need React lifecycle events for my project. I don’t need component state either. I just want to render a bunch of stateless components with props and children. Something like this:

Well, maybe there’s a way to just import part of the rendering engine… but then I realized I had yet another requirement: I need to be able to modify the string version of every component as it is created. I can’t do that with React. I need a custom renderer.

Happily, with some experimentation I learned that it’s not that hard to create one! Below you can see my version of renderToString(). It accepts both stateless functional components and component classes.

Of course, the version below is a bit naive. I’m certain there’s many edge cases of rendering which it does not cover, and like I said above, it does not support state or lifecycle events at all. That said, it works very well for my own purposes and I learned a lot about how React components are put together in the doing!

The following also includes a full test suite to show how it works.

PHP Unit Testing and Pad Thai

As a follow-up to my last post about PHP unit testing, I recently gave a talk at WordCamp Boston about how to write testable WordPress plugins. You can see the talk slides here:

https://payton.codes/testable-wordpress-plugins/

You can also watch the talk here, although the audio quality isn’t great.

The premise of my talk was mainly that having testable code is a really great idea, even (and perhaps this is surprising) if there are no actual tests. Of course, without writing tests it’s hard to know for sure, but some experience will go a long way for future projects.

Even though this was at a WordCamp, my talk is not really WordPress specific. It really applies to any unit testing in PHP. In it, I gave an analogy to help my audience understand some of the concepts. Here’s that analogy:

Why have tests?

If you asked a friend to make you Pad Thai, you’ll probably get something edible, but maybe your friend isn’t such a good cook. If you don’t like the food, how can you tell what went wrong?

Dependencies

Defining your dependencies is important. “I want Pad Thai” vs. “I want Pad Thai, made with the following ingredients” will help. Even better, “I want Pad Thai, made with the following ingredients, ordered from this store, using this recipe”.

Mocks (AKA Stubs)

Using mocks is like asking the store to only sell your friend a particular brand of noodles when he calls. If the dish still tastes bad, you know it wasn’t because he got the wrong noodles.

What is a Spy?

Using spies is like giving your friend your phone number instead of the store’s phone number so that when he calls the store to order noodles, he calls you instead. That way you can verify that he’s ordering the right thing. If he calls and orders wheat noodles instead of rice noodles, you’ve found a bug with your friend’s recipe.

I also wrote Payton’s Testable Precepts: 10 guidelines I use when writing code that help make my code testable (as well as more clean, more flexible, and more readable). Here’s a list of those Precepts. See the slides for reasons and examples.

  1. Always use classes, never global functions.
  2. Don’t use static functions except as factories.
  3. Use instance variables as constants.
  4. Use filters to pass data indirectly.
  5. Use verbs in all function names.
  6. Keep functions below eight lines and indentation below four levels.
  7. Consider all possible inputs and outputs.
  8. Whenever possible, write tests first.
  9. Don’t test the code from other libraries, but test the inputs and outputs.
  10. Write only one assertion per test.

 

 

Temple of the Ruby Sands

A year ago I published a Dungeons & Dragons adventure titled The Stonecutter Samurai which was written to share with my friends and co-workers at Automattic. This year I wrote another adventure, Temple of the Ruby Sands!  As it has now been played a total of seven times and I’ve cleaned up the stats and wording, I’d like to share it with you.

The game is a one-shot adventure for a group of 2nd-level characters and runs in about 3-4 hours (I’m getting better at making them short!). It was designed to be fun for brand new players and experienced die rollers alike. It should even have enough info be run by new DMs (although you may want to get to know the D&D 5e rules a bit first)!

Here is the teaser text:

An ancient temple, buried deep in the desert, has recently been excavated by an eccentric Dwarven archeologist. Unfortunately, the excavation site is surrounded by blood-red sand that many locals claim to be cursed, and the archaeologist’s last team was killed when they discovered a series of deadly traps within. Now he desperately wants to hire a new team to return and claim the temple’s treasure, but is there something more to this ancient ruin than meets the eye?

If you feel like braving the Temple of the Ruby Sands, you can download the adventure here.

(Awesome formatting courtesy of The Homebrewery.)

Higher Order Components and Pie Recipes

Higher Order Components (“HOCs”) are the latest hotness to come out of the JavaScript idea world and land in our apps. React apps, anyway.

I really want to write: Higher Order Components are just wrappers, but that would be simplifying the concept too much. Wouldn’t it? Maybe.

Anyway, they’re wrappers. Keep your actual Component simple as pie – it takes ingredients as props and turns them into rendered flaky crust – and have the wrapper handle getting the data from wherever, massaging it, and jamming it in.

This makes the underlying Component simpler to understand, debug, and ultimately reuse elsewhere if needed. Also testing! It’s way easier to test when the data piece is separate.

After thinking about it for a while you may realize that this concept is not new. Keep a pie-making machine abstract and separate from the ingredient machine? Where have we seen this before? Functions! (Ok, other places too.) And lo, this seems to be where React is heading (has already arrived?): Components as functions.

A Component, ideally, should take its props, nothing else (except maybe more Components) and turn those props into output. The output should be the same no matter what as long as the props are the same. It can use libraries to process the data it already has, but not get new data. I mean, that was an idea that React was built upon. But hey, I’d say that’s also the definition of a good function.

So if we think about our Components as functions, then we can apply function strategies to them. Mine basically boil down to: can this be done in fewer lines by extracting logic into a sub-function? Repeat as necessary until my functions are thin little beautiful pancakes. Maybe you have different strategies. Try them on Components!

Usually I see HOCs being talked about in reference to Redux and the global state tree, but my favorite thing is: who cares where the data comes from? Nest those Components and let them remain simple. The HOC could pull data from many sources, even just from a JSON file.

My new rule of thumb is: whenever my Component starts getting too many props (that are not directly related to its concept), when I am pulling data into my Component from a library, or when I find myself caching data in a Component’s state, there’s a strong chance I could use an HOC to smooth things out.

Give it a try! You may be surprised at how fun it can be.

Power and Love

Here’s a quote by Martin Luther King Jr. that I learned today and I really feel compelled to share:

Power without love is reckless and abusive, and love without power is sentimental and anemic. Power at its best is love implementing the demands of justice, and justice at its best is power correcting everything that stands against love.

Test spies in PHP

I think that I wrote my first unit tests in Ruby with RSpec, back in the day. But I learned most of my testing knowledge from working with mocha and chai in JavaScript. One of the things that I learned from these tools is that being able to express your test logic in clear, nearly-natural language brings a real comfort to the often complicated life of the test-writer.

For example, here’s a line straight from the chai home page:

expect(tea).to.have.property('flavors').with.length(3);

It’s so lovely.

When writing tests to follow the flow of code from one module to the next, it’s often useful to have Test Spies around to “spy” on the code and tell you what’s happening when. To borrow the definition from my favorite test spy library, sinon:

A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls. A test spy can be an anonymous function or it can wrap an existing function.

When brought together with sinon-chai, this allows for very expressive syntax:

expect(mySpy).to.have.been.calledWith("foo");

Because I work a lot with WordPress, I also write a lot of unit tests in PHP. There, the defacto standard is PHPUnit, which has… less natural language. That’s fine, though, its assertions are usually perfectly readable. What it seems to be lacking, for me anyway, is Spies.

Certainly there are some great ways to use PHPUnit’s built-in mocking tools, like this (hidden) behavior, but Spies are hardly a first-class citizen. And besides, even writing this feels confusing to me:

$target = $this->getMock('TargetClass');
$target->expects($spy = $this->any())->method('doSomething');

$target->doSomething("foo");
$target->doSomething("bar");

$invocations = $spy->getInvocations();

$this->assertEquals(2, count($invocations));

I would much rather write:

$target = mock_object_for('TargetClass');
$spy = $target->spy_on_method('doSomething');

$target->doSomething("foo");
$target->doSomething("bar");

expect_spy($spy)->to_have_been_called->twice();

So I ended up writing a Test Spy library for PHP. I call it Spies. My intent was to have an easy and readable way to create Test Spies, Stubs, and Mocks, and also a clear way to write expectations for them.

Creating a Spy is as simple as calling \Spies\make_spy(); (although there’s many other ways to create them). You can then call the spy and ask it questions, like this:

$spy = \Spies\make_spy();
$spy( 'hello', 'world' );

$spy->was_called(); // Returns true
$spy->was_called_times( 1 ); // Returns true
$spy->was_called_times( 2 ); // Returns false
$spy->get_times_called(); // Returns 1
$spy->was_called_with( 'hello', 'world' ); // Returns true
$spy->was_called_with( 'goodbye', 'world' ); // Returns false
$spy->get_call( 0 )->get_args(); // Returns ['hello', 'world']

Here’s a more complete example of using Spies to mock an object and test its behavior:

You can install Spies using Composer by typing composer require sirbrillig/spies in your project.

I owe quite a lot to the great library WP_Mock by 10up. Many of the concepts in Spies were inspired directly by the way that WP_Mock works, like creating global functions. WP_Mock serves a slightly different purpose, though. It’s a layer on top of Mockery, which in turn is a layer on top of PHP’s own testing tools. Also, it mocks a few things by default, like filters and actions.

Spies is intended to be more generally useful, and also to make the distinction between mocking and setting expectations more clear by breaking away from PHP’s built-in mocking concepts.

I welcome comments and suggestions at the Github page! As always, I hope it ends up being useful to other people.