We recently hired in a new Junior Developer and it’s been interesting to see how well they understand the basic PHP functions but get a little lost when they use some of our more advanced OOP classes. If feels like forever ago I went off on a rant about how we teach programming in what feel like a backwards way to the way I work every day:

When I learned to program in an academic setting (over a decade ago), we learned the basics of programming first (if block, for loop, functions, etc.) and then object oriented programming. This makes sense because it allows you to quickly write programs that do something but I think it teaches you the “wrong” way to structure your code. I think because of this some of the programmers I’ve worked with have been really good at writing code that can be used in one place (and can be copied and pasted into others :-)) but haven’t always been good at writing code that can be unit tested and reused without duplication.

It’s caused me to think about learning PHP using TDD and what that would look like.

Return to Coding 101

When I learned how to program using C++ all those years ago I was given an example like the following (my guess is that this won’t compile so don’t even try it):

#include "iostream.h";

void main(){
    cout << "Hello World!" << endl;
    return;
}

If I recall correctly, the author of the book I was learning from then went into detail about what each line did but the amazing thing was that I was able to type this into an editor and it would compile. That’s all it took to get a program up and running.

After this we started learning about variables, control statements, and functions. It was only later that we learned about classes, inheritance, and polymorphism.

Only now I realize we learned about the very basic statements, then functions and how they’re composed of multiple statements, and finally classes which are composed of multiple functions. Small pieces build up to larger pieces. From a learning stand point this makes sense. It’s easier to get going quickly and maximize the “wow” of programming so we didn’t lose interest.

For a long time I really didn’t “get” how classes could make my life easier.

Inverting the Learning

So let’s look at what would be involved in learning PHP if we taught everything TDD first. We would have to invert the learning process of statements > functions > classes to classes > functions > statements. Practically, it would flatten our learning process from learn statements, then functions, then classes to just learn all three in one go.

Programming languages like C# force this concept on us because everything must be a class so I wonder if this is even a problem.

Comparing Hello Worlds

I think the most interesting part of any programming language is the “Hello World!” example. The basic PHP version is simple:

<?php
echo "Hello World!";

Hard to get simpler than that.

Now let’s look at a classed version:

<?php
class Application
{
    public static function run()
    {
        echo "Hello World!";
    }
}

Application::run();

This is the simplist version of this I could create. If we were doing it the best practice way we would move the class into it’s own file, use an autoloader, setup DocBlocks, and the biggest problem with this is that it’s not easily testable.

Building Up

Let’s think about this some more. We could use the code above as our learning application and build on it. By doing so we can teach best practices:

  1. Install composer and use it’s autoloader
  2. Break the entry point and the class into separate files
  3. Rewrite the hello world example to use unit tests

It makes our hello world example MUCH longer and harder to understand but I almost wonder if it’s worth it.