In karate there is an image that’s used to define the position of perfect readiness: “mind like water”.
— David Allen, Getting Things Done
I find web applications, scripts, fascinating. Like water their natural state is stateless. And like water they have no memory at all.
Every time you interact with a web application is its first time ever. Every single time is its first and only time.
That is because most web applications are stateless. You start them, they gather whichever files they need, do their thing … and then forget all about it. All. Everything’s gone.
Imagine that: when you clicked on the link for this page scripts on the backend read configuration files, learned where and how to connect to a database, made that connection, retrieved this article, figured out how this page should look and what information should go on it – and now, by the time you’re reading this, it has forgotten all about it! The next time someone wants to see this article the script has to go through the same steps because it honestly has no clue, no memory, of what it just has done.
Apply that to your desktop applications for a moment! You click the icon of your favorite application. *Click*. It loads. It loads itself, configuration information, user settings, additional files it uses. Finally, there it is…
Now you click one of its buttons – and it would have to start all over again? No way! Your computer remembers all its “stuff” in memory.
But with web applications the answer is “way!!”.
Why again? Because a web application, because PHP, is stateless.
How then does a web application remember “stuff”? How does it remember its configuration? Its user settings?
Simple: the programmer, the developer, has to make sure that all relevant information is stored and make sure that all relevant information is read back in when the script is called upon. He also has to make sure this happens fast.
Which is where the folly of doing things the “clever” way come in.
The Long Way
It’s a long way.
It’s a long, long, long way there,
I’m gonna keep on tryin’
— The Little River Band, It’s a long way
When you retrieve information from files because you have to remember that information you will want to – nay, need to — use that information in and through variables of some type or another.
Storing that information in a scheme, for example in XML through patConfiguration, may seem like a neat trick but what do you gain?
The script loads. It loads the class to parse the configuration file. It loads the configuration file itself. It parses the configuration file – and stores the information in variables. If you want you can cache the file so it doesn’t need to be parsed again.
This, my friend, is not a state of perfect readiness. This doesn’t achieve the high velocity impact we want our web application to have.
It doesn’t help that an additional class has to be compiled each time the script runs. It doesn’t help that a non-native format has to be translated into something PHP understands.
What does help is squashing the urge to reinvent the wheel. Go with the flow and use PHP’s own built-in functions instead. Built-in, that means; pre-compiled, highly optimized. Fast. Effective. parse_ini_file, for example.
There are many ways to be clever, very clever indeed, in PHP but The Long Way hardly ever is.
Make your code flow and follow the natural, intuitive path of least resistance.