As a programmer, you look for repeated logic and try to condense it down into something smaller. One way to do this is to loop over a data structure.
A new(ish) way in Railo is to use a nested function.
Take this unit test:
function testGetEnvironment() { testName = getTestName(getFunctionCalledName()); host = "127.0.0.1"; env = "local"; assert(appUtils.getEnvironment(host) EQ env, "Host [#host#] yields environment [#env#]", testName); host = "dev-xxx.yyy.net"; env = "dev"; assert(appUtils.getEnvironment(host) EQ env, "Host [#host#] yields environment [#env#]", testName); host = "xxx.yyy.net"; env = "live"; assert(appUtils.getEnvironment(host) EQ env, "Host [#host#] yields environment [#env#]", testName); host = "no.such.host"; env = "unknown"; assert(appUtils.getEnvironment(host) EQ env, "Host [#host#] yields environment [#env#]", testName); }
Four blocks, almost the same. We could do this:
function testGetEnvironment() { testName = getTestName(getFunctionCalledName()); hostEnvMap = [ {host = "127.0.0.1", env = "local"}, {host = "dev-xxx.yyy.net", env = "dev"}, {host = "xxx.yyy.net", env = "live"}, {host = "no.such.host", env = "unknown"} ]; for (hostEnv in hostEnvMap) { assert(appUtils.getEnvironment(hostEnv.host) EQ hostEnv.env, "Host [#hostEnv.host#] yields environment [#hostEnv.env#]", testName); } }
Or we could use a nested function:
function testGetEnvironment() { testName = getTestName(getFunctionCalledName()); test = function(host, env) { assert(appUtils.getEnvironment(host) EQ env, "Host [#host#] yields environment [#env#]", testName); }; test("127.0.0.1", "local"); test("dev-xxx.yyy.net", "dev"); test("xxx.yyy.net", "live"); test("no.such.host", "unknown"); }
Which is better?
If the inner function ever needed to be made available outside of the outer function, it pops right out. If on the other hand the data structure is of more use to other parts of the application, the later is preferable.
I think the nested function reads better.
In the example given, a unit test, the data structure is very much throw-away. Hence why I went with the inner function.