TL;DR: We’re coding for the web all wrong. And it’s mostly because of the languages we’re using. We’re piling hack upon hack upon hack. Good coding principles and practices are becoming more common in web development, but I can’t help wishing we could take a few steps back and start with tools that help us write better code in the first place.

WARNING: contains opinions.  Some of which may be uninformed.

Anyone involved in software that knows me well will know a couple of things about me:

  1. I have a different approach to web development than a lot of people – mostly because I have a background in safety critical software, which is written very differently to code for a website.
  2. Despite that background, I’m pretty pragmatic. Horses are for courses. Tools are just tools.
  3. (Because of that background) l hate JavaScript. Any language that guesses what you want to do seems to me to be unhelpful. PHP is better, but far from perfect.

I mostly write PHP for back end code because I’m pragmatic. It’s not perfect. It’s slow. It promotes better discipline than JavaScript. But it’s also relatively accessible, hugely widespread, easy to deploy.

And I’m always looking to write better PHP. Which is why I’m currently reading Adam Wathan’s Refactoring to Collections. A book which tries to apply some declarative programming principles to PHP coding.  This, in short, means he’s come up with some nice ways to write code that says what you more want to get done, and less HOW to get it done.

And it’s great and it’s already transforming the way that I think.

But…and maybe this is addressed in later chapters that I’ve not read yet…I can’t read stuff like this without thinking about simplicity and performance. Here’s me wanting to do some simple transformations on some arrays, and I need to:

  • use composer to pull down the package that contains the Collection class;
  • load an autoloader that loads the dependencies;
  • probably, in doing so, include a load of code that I’m never going to run.

And thats before we even write a line of code. Once we have some code written using collections, method chaining, etc, then because I’m using other people’s implementations of higher-order functions, I’m totally removed from how efficient and scalable that code is.

This is a more general problem with declarative programming. It’s really nice and easy to read, but you don’t have the implementation detail. So how are you to know if using these collections going to make code that scales?

I find myself thinking “Why aren’t collections part of the standard library of PHP?”

That’s because, PHP isn’t a declarative language, it’s an imperative language. Adding collections to it is a fudge that makes it something that it isn’t.

So if you really want to do declarative programming nicely, you have two options: use a different language (options limited), or pile layer upon layer of abstraction on top of an existing language to make it something that it isn’t (options many and varied!).

Which is exactly what we do with JavaScript: all these frameworks and transpilers that we have are really compensating for the fact that vanilla JavaScript is a terribly loose language that needs additional structure and discipline put on top of it to make it something you can work with.

I do lament that the web – and that I specifically – ended up with PHP and JavaScript. And I long to learn Elm (yes, complied to JS) or Ruby or Go or Python instead.

But I’m pragmatic too. PHP and JavaScript are the tools I have to work with right now until I learn something new.

Don’t get me wrong. I love that coding is so easy and accessible and these open languages have transformed my life for the better. I’m also really enjoying Refactoring to Collections and it will transform how I code. And I’m indebted to and hugely respectful of the open source project creators and maintainers and documenters and book writers that make things like illuminate/support, Laravel, PHP itself, WordPress, Drupal, heck jQuery and all the JavaScript libraries and frameworks and stuff that makes so many amazing things possible and easy.

This is just a pedantic, ex-SPARK95-coder’s rant, wishing that we could choose base-level tools and languages that help us write better code in the first place, inspired by JJJ and the guy who (allegedly) couldn’t deploy an app because Angular had 40,000 files in it.

It can be better. Software really, doesn’t have to be terrible.