Test Driven Development and Positive Reinforcement

I love writing code using test driven development. But this will not be a post about how to test drive your code. There are plenty of very good articles available and, of course, the Kent Beck book. I will reference the obligatory Wikipedia definition, though.

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.

This will not be a post about how TDD improves code quality and design. These topics have been well covered. This post is much more personal.

Let’s walk through a typical TDD session: I write a test. I watch the test fail. I implement code. I watch the test pass.

At this point, I’m engaged and excited about moving on to the next test. I know that I’m on the right path and it feels good. I repeat the cycle in short intervals, each one leaving me increasingly more satisfied with what I’ve accomplished. Ballooning test counts give me a sense of how far I’m progressing. I often get into a state of flow and can lose myself in the code.

These micro goals and feedback loops have a fascinating way of reinforcing our behavior. Video game designers have been exploiting the power of positive reinforcement for years. Gamers keep playing in order to get that next win or next reward. With TDD, each passing test is a “win” and I want to keep going. I’m doubly motivated if I implement some code and the test is still failing. This is Albert Bandura’s self-efficacy – “the measure of one’s own ability to complete tasks and reach goals” – in action.

Research on game theory helps us understand why this works:

The flow we experience when playing a great game is a prime example of how we can condition other parts of our lives. Because of this, flow has become central to game theory. Good games that are responsive to player ability and game difficulty are framed as microcosms of optimal experience. They give the player a sense that their skills and abilities are adequate for coping with the challenges presented, and are based around “a goal- directed, rule-bound action system that provides clear clues as to how well one is performing”

TDD truly is about so much more than testing. The feedback loop inherent in TDD feeds us with positive reinforcement that our minds crave. The quality and design improvements that TDD lead to are nice, as well :)

Why is common sense not very common?

I’m sitting around, waiting for my hellfire chili to finish cooking and I came across this gem written by Ron Jeffries. If you’re involved in any way with software development, this is a phenomenal read.

His second paragraph starts off with this:

Most of us were taught to write down all our requirements at the very beginning of the project. There are only three things wrong with this: “requirements,” “the very beginning,” and “all.” At the very beginning, we know less about our project than we’ll ever know again. This is the worst possible moment to be making firm decisions about what we “require.”

And he follows that up with:

Then we demand that the developers “estimate” when they’ll be done with all this stuff. They, too, know less about this product than they ever will again, and they don’t understand most of these requirements very well.

To me, this is common sense. We don’t ask students to take an exam before learning the material. That’s not practical. Yet big requirements, long term projections, and unrealistic dates show up everywhere. Of course, the folks funding software projects want to know how much they need to invest and when they’ll get something for their investment. We’re not manufacturing a car, though. There’s no blueprint when we’re developing a new product. Why isn’t this common sense in software projects?

Tonight I wanted something spicy. The chili recipe I mentioned is like a set of requirements – this is how to make a spicy dish. I’ve never made this recipe before, so I decided to experiment with the heat level. I knew from previous experience that habanero peppers are hot. Really hot. In fact, six habaneros sounded like it may melt my face off. So I added what I thought I needed, measured the heat level, then adjusted.

I’ve posted before that I believe what we really want is to minimize the cost of change. When teams struggle to change direction, the tendency is to lock in requirements and delivery dates. If a simple change in requirements leads to a huge change in design, product teams will be instructed to define everything they want before development starts. “Changing requirements will cause us to miss our deadline” we’re told. Of course, as Ron points out, these requirements are rarely correct because they are defined when we know the least about what we’re building. Projects end up taking much longer than anyone wants and deliver features that nobody wants. So the lesson is that software developers need to make it simple to accommodate change.

Naturally, Ron is much more eloquent:

And you need to know how to build software that is soft enough, malleable enough, to grow smoothly as it needs to.

Working in short iterations frees you to change direction. To incorporate feedback. To learn. To build software that people want instead of software that visionaries think people want.

This simply makes sense to me.

And by the way, the chili was fantastic. It’s a deep burn, but delicious. If I made it exactly as the recipe stated I would have likely thrown the whole pot in the garbage and cooked up a frozen pizza.

What’s up with phpScheduleIt 2.4?

It’s been nearly 5 months since the 2.3 release of phpScheduleIt and I’m sure some are wondering what’s going on. Well, a lot of time has been spent working with BrickHost in setting up a cloud-based hosting solution for phpScheduleIt. This will be a turn-key solution with full support for a great price.

But development towards a 2.4 release is in progress! The main focus of 2.4 is a comprehensive public API. This will allow developers to easily integrate with phpScheduleIt and paves the way for mobile versions of the application. In addition, here are the other features that will be part of 2.4

GUI based configuration editor. No more need to open up the config file for basic changes.
config

Daily layouts. Full support for different schedule layouts for each day of the week.
daily

WordPress authentication plugin. Single sign-on between phpScheduleIt and WordPress.

I’m also planning to include reservation reminder emails and ReCAPTCHA support. With a little luck and a lot of free time, we should be shipping 2.4 soon.

Stay tuned!

Extending phpScheduleIt – Writing a Pre-Reservation Plugin

My previous post walked through configuring and enabling plugins in phpScheduleIt. That’s great… but what if we need something custom to our environment? The most popular support requests I get are for changing different workflows – especially around the reservation process. Today I’m going to dive into how to write plugins for the pre reservation events. If you know some PHP and a bit of object oriented design, this should be pretty straightforward.

Note: The version of phpScheduleIt at the time of writing is 2.2. While the exact details explained in this post may not apply to future versions, the core concepts will.

Getting Started

phpScheduleIt’s plugin architecture is largely convention-based. This means that a few rules apply to all plugins.

  • The plugin package structure must contain the following:
    • A directory named after the plugin, containing all plugin code.
    • A file within that directory named after the plugin.
    • If there is a configuration file needed, it must be named after the plugin, with ending with the suffix .config.php.
  • The plugin class name must must match the file name (without the .php suffix).
  • Plugins follow the decorator pattern.
    • The plugin must extend or inherit the base class or interface. It must also accept it as the first argument to the constructor (we’ll go into the different classes and interfaces available for each plugin type later).

For example, if we’re writing a PreReservation plugin named PreReservationExample, we would create a directory named PreReservationExample. Within it, we create a file named PreReservationExample.php. This file would contain a single class named PreReservationExample, which would extend the base plugin class and accept an instance of that class as the first constructor argument.

OK, enough with the abstract examples. Let’s get into some code.

Pre-Reservation Classes and Interfaces

The pre-reservation plugin allows you to do all sorts of fun things like custom validations, reservation adjustments, notifications and so on. The base class for PreReservation plugins is a PreReservationFactory. Let’s take a look at the interface.

interface IPreReservationFactory
{
	/**
	 * @param UserSession $userSession
	 * @return IReservationValidationService
	 */
	public function CreatePreAddService(UserSession $userSession);

	/**
	 * @param UserSession $userSession
	 * @return IReservationValidationService
	 */
	public function CreatePreUpdateService(UserSession $userSession);

	/**
	 * @param UserSession $userSession
	 * @return IReservationValidationService
	 */
	public function CreatePreDeleteService(UserSession $userSession);
}

Simple enough, but this gives us the power to hook into events before adding, updating, or deleting a reservation. The only argument to each of these functions is a UserSession, which gives you some insight into who is making each of these calls. Each one of these functions returns an instance of an IReservationValidationService. Let’s look at that interface.

interface IReservationValidationService
{
	/**
	 * @param ReservationSeries|ExistingReservationSeries $series
	 * @return IReservationValidationResult
	 */
	public function Validate($series);
}

Even easier! This has one function: Validate(), which accepts either a ReservationSeries (during add) or an ExistingReservationSeries (during update and delete). This is executed during the add/update/delete event and returns an instance of an IReservationValidationResult. Continuing down the path, we see that this interface is also pretty simple.

interface IReservationValidationResult
{
	/**
	 * @return bool
	 */
	public function CanBeSaved();

	/**
	 * @return array[int]string
	 */
	public function GetErrors();

	/**
	 * @return array[int]string
	 */
	public function GetWarnings(); 
}

All of these classes and interfaces can be found in /lib/Application/Reservation/Validation

Putting It All Together

Let’s say that for add and update we want to enforce some rules based on the value of a custom attribute that we added. For delete, we’re happy with the default behavior. Since we already explained how to create the plugin structure for a plugin named PreReservationExample, let’s stick with that name. So the contents of /plugins/PreReservation/PreReservationExample/PreReservationExample.php would look something like this:

class PreReservationExample extends PreReservationFactory
{
    /**
     * @var PreReservationFactory
     */
    private $factoryToDecorate;

    public function __construct(PreReservationFactory $factoryToDecorate)
    {
        $this->factoryToDecorate = $factoryToDecorate;
    }

    public function CreatePreAddService(UserSession $userSession)
    {
        $base = $this->factoryToDecorate->CreatePreAddService($userSession);
        return new PreReservationExampleValidation($base);
    }

    public function CreatePreUpdateService(UserSession $userSession)
    {
        $base =  $this->factoryToDecorate->CreatePreUpdateService($userSession);
        return new PreReservationExampleValidation($base);
    }

    public function CreatePreDeleteService(UserSession $userSession)
    {
        return $this->factoryToDecorate->CreatePreDeleteService($userSession);
    }
}

Let’s step through this. Our class extends the PreReservationFactory. It also accepts an instance of a PreReservationFactory as the only argument to the constructor with a parameter named $factoryToDecorate. This will be a fully instantiated object, loaded up with all the rules needed during add/update/delete. Our constructor just holds onto that instance as a private field.

Each of the Create* functions are then responsible for returning a service to be used before their respective event. In our case, CreatePreAddService() and CreatePreUpdateService() get the base validation service, then add their validation service onto it. Since we don’t have any custom rules for delete, we simply return the base service.

This is an important point. To extend the base PreReservation behavior, we need to return the base service, or a decorated version of that service. If we want to completely replace the base behavior, simply return your own custom object.

Now comes the fun part, our implementation of CreatePreAddService() decorates the existing instance. We do this by getting the result of the base CreatePreAddService() function, then decorating it with our own validation object. We end up returning an instance of a PreReservationExampleValidation, giving it that base service to wrap. Now, we’ll need to create the PreReservationExampleValidation to handle our custom rule when adding and updating a reservation.

We start off by adding a new file to our plugin directory named PreReservationExampleValidation.php. This contains a single class which must implement IReservationValidationService

class PreReservationExampleValidation implements IReservationValidationService
{
	/**
	 * @var IReservationValidationService
	 */
	private $serviceToDecorate;

	public function __construct(IReservationValidationService $serviceToDecorate)
	{
		$this->serviceToDecorate = $serviceToDecorate;
	}

	/**
	 * @param ReservationSeries|ExistingReservationSeries $series
	 * @return IReservationValidationResult
	 */
	public function Validate($series)
	{
		$result = $this->serviceToDecorate->Validate($series);

		// don't bother validating this rule if others have failed
		if (!$result->CanBeSaved())
		{
			return $result;
		}

		return $this->EvaluateCustomRule($series);
	}

	/**
	 * @param ReservationSeries $series
	 * @return bool
	 */
	private function EvaluateCustomRule($series)
	{
		// make your custom checks here
		$configFile = Configuration::Instance()->File('PreReservationExample');
		$maxValue = $configFile->GetKey('custom.attribute.max.value');
		$customAttributeId = $configFile->GetKey('custom.attribute.id');

		$attributeValue = $series->GetAttributeValue($customAttributeId);

		$isValid = $attributeValue <= $maxValue;

		if ($isValid)
		{
			return new ReservationValidationResult();
		}

		return new ReservationValidationResult(false, "Value of custom attribute cannot be greater than $maxValue");
	}
}

This class is also a decorator, so it accepts a class to decorate as the first parameter to the constructor. Next, lets look at the Validate() method. The first thing we do is call down to our decorated Validate() method. This runs all of the existing PreReservation steps. Then, we check to see if we're in a valid state by checking the value of $result->CanBeSaved(). If everything is OK to this point, then we go ahead and run our custom validation.

Walking through the EvaluateCustomRule() function, we first get our plugin's configuration file, reading settings for the maximum value we allow and the id of the custom attribute we are interested in. Then, we get the current value of that attribute from the ReservationSeries. Finally, if the value is less than or equal to our configured maximum, we simply return a new ReservationValidationResult. This indicates that there are no issues. If the value provided exceeds our maximum, then we return a ReservationValidationResult, passing in false to indicate the failure and an error message to explain what exactly failed. The error message will be displayed to the user.

Configuring Your Plugin

If you wanted to use phpScheduleIt's provided configuration API, you would just need to create and register your config file. This would change the constructor of the PreReservationFactory to something like this:

public function __construct(PreReservationFactory $factoryToDecorate)
{
	$this->factoryToDecorate = $factoryToDecorate;

	require_once(dirname(__FILE__) . '/PreReservationExample.config.php');

	Configuration::Instance()->Register(
				dirname(__FILE__) . '/PreReservationExample.config.php',
				'PreReservationExample');
}

And here is our config file:

$conf['settings']['custom.attribute.max.value'] = '100';
$conf['settings']['custom.attribute.id'] = '3';

We won't go into the details of configuration here, but it's enough to say that configuration files are structured as arrays. All settings are part of the $conf['settings'] array.

Wrapping It Up

As with any plugin, you'll need to tell phpScheduleIt that you want to use it by setting the plugin value in /config/config.php. Our value would be: $conf['settings']['plugins']['PreReservation'] = 'PreReservationExample';.

Hopefully this gives you a taste of what's possible. This is a powerful customization point and there's almost no limit to what you can do here. Also remember that you have all of phpScheudleIt's internal API available to you. Explore it and use it.

You can find the source code for this example in /plugins/PreReservation/PreReservationExample

Happy Coding!

Extending phpScheduleIt: Understanding and Installing Plugins

One thing I really wanted to focus on when writing version 2 of phpScheduleIt was finding ways to keep the application open for extensibility. This led me to creating a pluggable framework that can be hooked into for certain operations. In this post I’ll explain a bit of the plugin structure and walk through how to configure and use plugins in phpScheduleIt.

Note: The version of phpScheduleIt at the time of writing is 2.2. While the exact details explained in this post may not apply to future versions, the core concepts will.

Plugin Overview

Let’s first explore how plugins are structured. phpScheduleIt ships with a /plugins directory. Within that are subdirectories for each supported plugin type. phpScheduleIt’s plugin architecture is mainly convention based. Each subdirectory within a plugin type represents a plugin. Taking the Authentication plugin type for example, we have ActiveDirectory, Drupal, and so on available to use for our authentication provider. The Authentication plugins directory currently looks like this:

Configuring and Activating a Packaged Plugin

phpScheduleIt comes packaged with a a few Authentication plugins, so we’ll continue using that for our example. Each plugin directory most likely will contain a default configuration file named something like subdirectory.config.dist.php. For example, ActiveDirectory will contain a file named ActiveDirectory.config.dist.php.

The first step is to remove the .dist part of the default configuration file. In our case, the file would end up being named ActiveDirectory.config.php.

Next, you need to adjust any configuration values to match your environment. For Active Directory, you may need to change domain controllers or administrative credentials.

Finally, you need to tell phpScheduleIt which plugin you want to use. Open up phpScheduleIt’s config file, located in /config/config.php and find the plugins section. By default, it looks like this:

$conf['settings']['plugins']['Authentication'] = '';
$conf['settings']['plugins']['Authorization'] = '';
$conf['settings']['plugins']['Permission'] = '';
$conf['settings']['plugins']['PostRegistration'] = '';
$conf['settings']['plugins']['PreReservation'] = '';
$conf['settings']['plugins']['PostReservation'] = '';

To use your configured plugin, just set the plugin value to the plugin name. Sticking with ActiveDirectory for Authorization, you’ll end up with this:

$conf['settings']['plugins']['Authentication'] = 'ActiveDirectory ';

And that’s it! phpScheduleIt will now use Active Directory when users log in.

Using a 3rd Party Plugin

The process for using a 3rd party plugin is nearly the same as the using a packaged plugin. The one difference is that the plugin needs to be ‘installed’ – which merely includes copying the full plugin directory into the plugin type directory. For example, if you are installing an Authentication plugin named FooAuth, simply copy the full FooAuth directory into /plugins/Authentication

Summary

Hopefully this process is nice and easy. Next we’ll talk about how to write our own plugins, starting with a pre-reservation plugin.

Flexibility and Configuration

I’ve run into numerous folks from both the business and technical sides of the world who want flexibility built into software. Developers and managers alike spew out statements like “We want to leave our options open”, “We’ll need to support that one day”, or my favorite “The business doesn’t know what they want”. As developers, we’re constantly faced with pressure to build things faster. Architects and technical gurus ingeniously figure that nothing is faster than making a simple configuration change. “Make it flexible. We don’t want to rewrite anything.” And with that we lay the first brick on the road to the inner-platform effect.

What we really want to do is minimize the cost of change.

This is why agile software practices were created. We want to be able to accommodate change, even late in the development process after significant progress. Some software developers take this far too literally, though. Accommodating change does not mean making every last detail configurable or worse yet, building massively over-architected systems in hopes of supporting yet-to-be-defined requirements.

Think about how long it takes to design and build all of those configuration points. Think about all of the permutations that need to be tested. At the end of the day, how many of those options will even be used? Remember, we’re making our systems and architectures flexible explicitly because we don’t know what we’ll use. By the time this infinitely flexible software is built something new will come along and we’ll realize in horror that we guessed wrong.

Flexibility means you design your software in a loosely coupled manner. Dependencies are injected instead of instantiated inline. You write classes that are 50 lines instead of 5000 lines. The SOLID principles become your best friend. Your software is simple, discoverable and, most importantly, testable. You can make changes swiftly and with confidence that the software is still fit for purpose. To me, this is true flexibility.

Why phpScheduleIt?

Every so often I’ll be asked why I work on phpScheduleIt. I’m not profiting from the project and the time I spend developing, supporting and on general housekeeping adds up quickly. The short answer is that the time I spend is fun and rewarding.

I’m a technologist at heart. I have been for many years. I crave learning and exploring new technologies. My day job gives me the opportunity to build some very cool software as part of an extremely talented team (more on this in a future post). The main drawback for writing software for someone else, though, is that freedom of design and technologies can be limited by deadlines and corporate decisions.

You’ll typically hear that open source developers are in it to “scratch an itch.” I’d completely agree. Building a calendaring application – with all the supporting functionality like permissions, administration and such – is a big job. There are plenty of technical challenges when working with dates and times and scheduling, especially across multiple timezones and cultures. These are completely different challenges than I face day-to-day, which makes them a lot of fun. And besides the code, I have an opportunity to envision and build a user interface to hide all of those technical challenges.

In addition to working with different technologies and design philosophies and so on, phpScheduleIt is an avenue for me to give back. I use a lot of open source tools and have benefited greatly over the years by looking at code. This application lets me take what I’ve learned and share it with others. My genuine hope is that phpScheduleIt is useful as an application to an organization or as a learning tool to an individual.

phpScheduleIt affords me freedoms I can’t get anywhere else. Don’t get me wrong… I get to work on plenty of emerging and fun technologies at my day job. But phpScheduleIt is a different type of fun.

Welcome!

It happened. I’ve finally jumped in to the world of blogging. My hope here is to teach, learn and have some fun.

I’m planning on sticking to technical topics, but I make no guarantees. It’s safe to expect a heavy dose of Agile software development, PHP & .NET code, design and tools, and plenty of phpScheduleIt.

You can also follow me on Twitter at @nickkorbel

Finally, please check out BrickHost. They’ve been an incredible partner of my phpScheduleIt project for many years now.