Which Is Better A or B?

It often surprises me that people who spend their whole day focusing on details, software developers, so often ask question in broad and generic terms.  Here’s some that I’ve heard over the years:

  • Why should I learn Object Oriented Programming?  (I have been writing software for a while.  Long enough to remember the days before we discovered that OO was the only right way to write software.)
  • Won’t having a VM with Garbage Collection be really slow?  (This question came up a lot when Java was just being rolled out.)
  • Isn’t C# just Java with different keywords? (When C# was first rolling out.)
  • Why should I learn functional programming?  (One of the more recent vague and generic question I’ve gotten tons of.)

And today:

  • Will I be more productive if I write a website in Elixir/Phoenix than if I write it in (Web Framework X)?

Now consider some of the implied assumption in those questions.  All other things being equal will B (new technology) be better than A (which I’m already using)?

Remember we are discussing human beings performing a human activity–writing software.  Of anything humanity has ever tried to measure, quantify and generally treat in an empirical way, software development seems to be one of the least amenable to analysis.  Just some of the implied parameters of some of those questions above are:

a.) Is B more productive than A?

How do you measure “productive”?  Economists and experts on the subject of productivity don’t agree about measuring productivity in terms of hard goods (cars, appliances, etc.) so how should we measure productivity in terms of something we can’t even really measure?

b.) Will B allow me to create better software than A?

Define “better”.  Better in what sense?  Less errors?  Runs faster?  Less source code?  Easier for maintenance developers to understand?  More secure against hacking?

The point is this: while we wish we could answer the question of whether a new technology is better than an existing technology at all there’s so much ambiguity in so many facets of what we’re discussing there’s no way to even frame these questions in a way they can be discussed intelligently.  So for goodness sake–look for yourself and make your own decision.

Railway Oriented Programming In Elixir

A while back Scott Wlaschin posted a great blog post on what he called “Railway Oriented Programming.”  It’s a great treatment of a common problem in adopting functional programming; how to handle a chain of functions when one of the functions in the chain may return an error.  Of course stopping the chain when the first error is returned isn’t a difficult issue; the difficulty arises in structuring the code such that the code can continue running all the functions until and unless an error is encountered.  It’s a great read and a powerful idea.

So today someone asked a question on StackOverflow about how to accomplish basically that same idea in Elixir.  A little tricky to translate Scott’s ideas to Elixir because in his article his code is specified in F# and he created a special type to capture the success or failure of a function. F# is statically typed; elixir is dynamically typed which is, of course, a substantial difference.

At any rate, I saw the question and didn’t see that Saša Jurić had already posted a great answer.  So I posted my answer and then noticed his.  I mention this because I want to be clear; I wasn’t trying to steal anyone’s thunder or trying to enhance my rep or anything like that.  I just got excited; “hey, I know the answer to that question!” so I just rushed off and hacked together some demonstration code without bothering to look any further.  I am pleased to see that the answer I came up with was pretty much the same as Saša’s; I consider him an extremely smart developer and coming up with an answer pretty similar to his  independently makes me feel a bit more confident of my own skills.

The TL;DR version is that you need to construct the functions to be applied in such a way that you can carry forward an indication of either success or failure along with the value to be checked.

defmodule Password do

  defp password_long_enough?({:ok = _atom, p}) do
    if(String.length(p) > 6) do
      {:ok, p}
    else
      {:error,p}
    end
  end

  defp starts_with_letter?({:ok = _atom, p}) do
   if(String.printable?(String.first(p))) do
     {:ok, p}
   else
     {:error,p}
   end      
  end


  def password_valid?(p) do
    {:ok, _} = password_long_enough?({:ok,p}) 
    |> starts_with_letter?
  end

end

And one would use it like so:

iex(7)> Password.password_valid?("ties")
** (FunctionClauseError) no function clause matching in Password.starts_with_letter?/1
    so_test.exs:11: Password.starts_with_letter?({:error, "ties"})
    so_test.exs:21: Password.password_valid?/1
iex(7)> Password.password_valid?("tiesandsixletters")
{:ok, "tiesandsixletters"}
iex(8)> Password.password_valid?("\x{0000}tiesandsixletters")
** (MatchError) no match of right hand side value: {:error, <<0, 116, 105, 101, 115, 97, 110, 100, 115, 105, 120, 108, 101, 116, 116, 101, 114, 115>>}
 so_test.exs:21: Password.password_valid?/1
iex(8)>

A couple of comments on the code.  Someone might ask why password_long_enough?  gets a parameter of a tuple of atom and string. Since it’s the first function in the chain it’s not really needed; all it needs is the string.  I constructed in this way for a few reasons:

  1. It’s more parallel to the other function
  2. The functions are really intended to be rules which should be checked so there should not be an order dependency. That is, they should be interchangeable.

Basically by adding any predicate function that we wish given that it takes a tuple of an atom and a string and the atom is matched against :ok, we can add any arbitrary set of tests we wish to add.

As with most things on this blog, I write this down as much for my own reference as for the edification of others who might read it.  I’m pretty sure at some point in the future, I will need to validate a value through a set of predicate functions and I’ll look at this blog post to see how I can do it.


EDIT: I was reminded of a great blog post written up by Zohaib Rauf on precisely this same idea.  Well worth reading.

Configuring A WordPress Site On EC2

There’s an excellent and practical write-up on configuring an EC2 container to host a WordPress site here.  But there are a few minor amplifications I’d add.  So I thought I’d write them up here.

1.) As some folks noted in the comments, you should assign the Elastic IP address before you install WordPress.  Everything with WP just seems to work much better when you do it in that order.

2.) You’ll want to configure your instance to autostart both Apache and Mysql.  Credit where it’s due: I got this information from here.

  • sudo /sbin/chkconfig –add mysqld
  • sudo /sbin/chkconfig –list mysqld
  • sudo /sbin/chkconfig mysqld on
  • sudo /sbin/chkconfig –add httpd
  • sudo /sbin/chkconfig –list httpd
  • sudo /sbin/chkconfig httpd on

You really don’t need the –list step in either case.  It’s just a way to insure you’ve got chkconfig command right. Note that those commands are specific to the CentOS image that Amazon provides.  If you’re hosting a different flavor of Linux, you’ll need to figure out the commands for your flavor.

Other than those two minor corrections, his guide is sound and well-written.

Interesting And Unexpected Benefit Of Pattern Matching

So recently I’ve been working on automating some interactions with a website.  Elixir’s Hound library is a great help, of course.  But I also ran across an interesting and unexpected benefit to pattern matching that greatly simplified some code.

I was trying to dynamically build a URL to pass parameters. The URL would be in the form:

http://www.exampledomain.com?param1=p&param2=q&param3=r

Whenever I see code like this, it’s always a little tricky because you want to add the & at the end of each parameter except the last.  This always used to entail writing a special case in the loop to test if the index of the current element was the maximum index.  But with pattern matching and recursion there’s a much simpler and cleaner solution.  Like this:

defp add_params_to_url(url,[%{:name => name, :value => value}]) when is_binary(url) do     #1
 "#{url}#{name}=#{value}"
end
defp add_params_to_url(url,[%{:name => name, :value => value} | t]) when is_binary(url) do   #2
 url = "#{url}#{name}=#{value}&"
 add_params_to_url(url,t)
end
defp add_params_to_url(url,[]) when is_binary(url), do: url   #3

This is Elixir for those unfamiliar with it.  Basically it’s three function clauses.  The first clause (#1) is hit if only one element is in the list passed into the function. The second clause (#2) is hit if multiple elements are present in the list and the third clause (#3) is hit if the list is empty.

So this is how this works.  When I call add_params_to_url, Elixir will automatically try to match the correct function call dependent on which list I pass.  It will also stop at the first function clause which matches so that other function clauses will not be evaluated.

If I pass a list of URL params which has only one element, clause 1 is matched and the parameter and value are appended and I’m done.  The URL already has the ? on the end, of course.  If I pass a list of URL params with mutliple elements, clause 1 doesn’t match so Elixir jumps down to clause 2 and that matches.  Clause 2 takes the first element from the list tacks it on to the list of parameters with a & at the end and then recursively calls itself with the tail of the list.  If the tail of the list contains more than one parameter, clause  1 will once again fail to match and clause 2 will match again.  If it contains only one parameter, clause 1 is matched.  Because clause 1 doesn’t call itself recursively, clause 3 should never be matched; it’s actually more of a guard case in case someone calls the function with an empty parameter list by mistake.

This, to me, is a small bit of genius.  Much simpler to get exactly the effect I want, that is not having an extraneous character tacked to the end of the string, and it’s nice and simple.

By the way, it occurs to me that I might be able to use a reduce or a fold function to achieve this same effect.  The issue there is that it’s less apparent what it is that I’m trying to do and I also run into the same problem–that is, how do I deal with that last element in the list so I don’t get my separator appended?

Rebar3 Beta1 on Chocolatey NuGet

I had a need for the Erlang build tool rebar for a project I was trying to start on; a bit of pen testing via Elixir.  I was going to pull down the rebar3 script from the site and fix it up for myself and it occurred to me that others might want a way to get the script easily too.  So rather than just building something to work for myself, I took a little extra time and built a Chocolatey NuGet package for Rebar3-Beta1.

The package takes care of insuring you’ve got Erlang first although honestly I can’t imagine anyone interested in Rebar that wouldn’t already have Erlang on their machine.  It also takes care of setting up a shortcut for Rebar and putting it in the path.  And, of course, like everything else in CNG, it also handles uninstalling the tool if you decide you no longer want or need it.

For now the package is still awaiting moderation on CNG but please feel free to pull it down and give it a try.  Feedback is a gift and I hope some of you will share that gift with me.

Rebar3 on ChocolateyNuGet