The functional if

The if statement is a low level flow construct in most imperative programming languages. It is traditionally used to do things like this:

if (some condition is true) {
    execute branch 1 of code
} else {
    execute branch 2 of code
}

where different branches of code represent different execution paths your program could take. It is basically a specific conditional case of a GOTO statement – if some condition holds go to this line; otherwise, go to that line. The common objectives of those branches are:

  • execute code with different side effects
  • assign different values to existing or new variables

I’m going to show how in these common cases you can write more expressive, less error prone code that to certain degree resembles the functional style. The examples will be in PHP, although other languages that support anonymous functions would probably allow similar technique as well.

Let’s look at the difference between procedural vs the functional if.

The procedural style:

$a = 20;
$b = 10;
if ($my_condition) {
    $x = $a + $b;
} else {
    $x = $a - $b;
}

Equivalent functional style, using the ternary operator – http://php.net/manual/en/language.operators.comparison.php:

$x = $my_condition ? $a + $b : $a - $b;

Notice that the right hand side of this line is an expression, not a statement. It always returns a value and, consequently, the $x will always have a value assigned as a result. While in the procedural style, we have to explicitly set it in each branch.

This is important because in procedural style:

  • logic is more error prone, we may forget to assign correct value to $x during certain execution path and not notice
  • programmer is tempted to set other variables in some or all of these exection paths that are used outside of the if block logic
  • more generally, statements execute in current lexical scope, which encourages logic with implicit dependencies on its context

These are all bad things that lead to (1) bugs during runtime, (2) bad code that can’t be easily read, understood and modified.

So, to avoid this, we could do something like this instead:

if ($my_condition) {
    $x = my_func_1($a, $b);
} else {
    $x = my_func_2($a, $b);
}

function my_func_1($a, $b) {
    return $a + $b;
}

function my_func_2($a, $b) {
    return $a - $b;
}

This is better, but a few disadvantages of doing this are:

  • it still suffers from multiple assignment issue
  • it potentially pollutes the current (which may be global) namespace with functions that may not be reusable and makes the code too verbose
  • the if statement doesn’t enforce this pattern, it must be explicitly used everywhere
  • it encourages programmer to create a single myfunc($a, $b) and move the procedural if logic there instead

Ideally, we’d have a way to:

  • be able to create local variables that don’t affect context
  • provide proper isolation for the logic
  • provide pattern that we can re-use in other places

Basically, we would want a flexible, multi-line ternary operator, or “the functional if.”

In functional programming languages, this is the only way. For example, in Erlang I can say:

X = if my_condition() -> A + B; true -> A - B end.

In fact, if I try to assign (or more precisely, pattern match) the X variable inside the if blocks, I’ll get a compiler warning.

In Lisp:

(setf X (if (my-condition) (+ a b) (- a b)))

But most imperative and procedural programming languages do not provide such features. So how close can we get to this in PHP? Let’s try this syntax:

$x = X::if(
    $my_condition,
    function($a, $b) { return $a + $b; },
    [$a, $b],
    function($a, $b) { return $a - $b; },
    [$a, $b]
);

Here we call X::if() static function that takes 5 arguments:

  • condition that evaluates to true or false
  • anonymous function to execute and return its result if the condition is true
  • array of arguments to pass to true anonymous function
  • anonymous function to execute and return its result if the condition is false
  • array of arguments to pass to false anonymous function

So, how would the X::if() look like? It’s actually pretty simple 3 lines of code:

class X
{
    /**
     * Depending on the condition, executes corresponding function with arguments.
     *
     * @param bool $condition Condition that evaluates to true or false
     * @param callable $true Callable that gets executed if $condition is true
     * @param array $true_args Arguments that get passed to $true callable
     * @param callable $false Callable taht gets executed if $condition is false
     * @param array $false_args Arguments that get passed to $false callable
     *
     * @return mixed result of either $true or $false callable execution
     */
    public static function if(
        $condition,
        callable $true,
        array $true_args,
        callable $false,
        array $false_args
    ) {
        $fun = $my_condition ? $true : $false;
        $args = $my_condition ? $true_args : $false_args;
        return call_user_func_array($fun, $args);
    }
}

Now if you can convince your team to get behind this pattern and somehow avoid the WTF moment every time someone looks at it, you might even avoid few bugs and hopefully make your code more readable and easier to modify and reason about.

Advertisements

PHP Sessions in Erlang Mnesia

Motivation

  • create a very simple first Erlang/OTP application
  • link to conventional web development

I decided one of the simplest things to do would be to create a session storage for PHP in Mnesia. But that, in doing so, I would also create an extremely simple key value store wrapper around Mnesia which would track access times.

Disclaimer

Do NOT use this in production, or anywhere where it’s important. If you do, you’re crazy because:

  • it has not been tested in production or production-like environment
  • as I mentioned, this is my first Erlang application
  • connection is made to a single Erlang node, and there is no failover mechanism if that fails
  • session garbage collection is not implemented

Good Stuff

OK, the disclaimer is out of the way, let’s get to the good stuff! Here’s what I did and how.

Tools used

  • Erlang runtime and development headers
  • PHP >=5.3 (>=5.4 preferred) binary and development headers
  • Apache
  • mypeb
  • kvstore
  • lib360

Architecture

[ PHP ] <–> [ mypeb ] <–> [ Erlang ] <–> [ kvstore ] <–> [ Mnesia ]

Instructions

  • download and install prerequisites as best done in your environment
    • Erlang runtime and development headers
    • Apache
    • PHP >=5.3 binary and development headers
  • download and compile mypeb
    • git clone git://github.com/videlalvaro/mypeb.git
    • cd mypeb
    • phpize
    • ./configure
    • make
    • sudo make install
    • php -m | grep peb
    • Note 1: the last command is a test to verify peb module loads successfully
    • Note 2: you might need to specify --with-erllib and --with-erlinc options with ./configure command if they are not automatically found
  • download, compile and run kvstore
  • download lib360
    • git clone git@github.com:unix1/spoof.git
  • add sample PHP script to your web server and test everything
    • download sample gist
    • replace the path in require_once() to wherever you downloaded lib360
    • replace the your-erlang-cookie-here with the contents of ~/.erlang.cookie file
    • access the file through the web server and have fun!

Missing

These are things that I thought of but didn’t bother implementing that I might someday add:

  • make kvstore more configurable and easy to install/start/stop
    • kvstore currently fails to stop (you have to abort)
    • it could use rebar and more automated scripts instead of manual commands
    • support for configurable multiple table names
    • make access time tracking optional
    • implement other storage backends
  • account for failover when primary node connection fails from PHP
  • PHP session garbage collection
  • implement kvstore access through lib360 database layer

Suggestions/Contributions/Feedback

If you have any feedback, feel free to give it via github, blog comments, etc. as appropriate. Enjoy!