I don’t think there is any computer science concept that distinguishes the terms scripts, templates, and programs in any real sense.
Go code is Go code. Something runs the code, it runs. Maybe it exits in 0.01 seconds, maybe it exists in 15 hours after calculating some astrophysics equation. PHP code is PHP code. Something runs it, it runs. Maybe it exits in 2 minutes, maybe it runs for 15 years until someone unplugs a server. Same story for any language. Heck, you could make the argument that HTML is the same way. It executes in a web browser and becomes an infinitely running program (e.g. listening for events) until the tab closes.
But my brain makes a distinction between these things. They feel different.
I find myself writing PHP once in a while, because I have production sites in WordPress, or I might use PHP just for an
include statement once in a while. When I’m writing PHP, I’m probably writing it as a meta-language overtop HTML.
Code language: HTML, XML (xml)
<h2> the_title(); </h2> include "parts/metadata.php"; <div class="excerpt"> the_excerpt(); </div>
I’m really just coding a template there. If I tried to execute that PHP file directly, it would fail, because those functions are defined elsewhere. By the time this particular PHP executes, I imagine hundreds if not thousands of other PHP files have already been executed. It doesn’t feel weird, it just feels… different than coding in other ways.
I could write PHP that I manually execute.
When I do that, it feels like I’m writing a script. I call it on demand, it runs, it exits. I almost never write PHP like this (In fact, I had to
brew install php in order to even run that), but I could.
I was thinking of this distinction my brain makes lately while working with Go (I’m just a beginner). I’m doing some work fleshing out a fairly large GraphQL API that is powered by Go. Rather than outputting HTML it outputs JSON, but it seems feels like templating work. Files that are part of a much larger whole and that will not run independently.
I’m also doing other work in Go that is definitely in script territory. I write Go code, I run it at the command line, and it runs.
My scripts are slightly more complicated. What I’m doing is:
- Connect to a Postgres database
- Query for all records of a particular table
- Pull JSON data from one of the fields of each record
- Manipulate that JSON data
- Put it back
This is written in Go because we’re writing most everything else in Go, I might as well level up the expertise there. But even if we weren’t, Go is incredibly fast, so it can run this script several orders of magnitude faster than if it was written in, say, Ruby. This is a game-changer in large data work.
So this programming work feels distinctly different, and in the script category, because:
- The code is designed to be run once and then tossed
- It runs independently and for a short time period
- It runs only when it is called explicitly
I’m not saying it excuses sloppiness, but, uhmm, I guess I’m not not saying that either. Code that is temporary in nature feels like, as long as it absolutely does what it needs to do perfectly, doesn’t need the same approach as code that will live and be touched by others for untold years.
I put program as a third category of code. Perhaps a bad name, but I needed to call this personal distinction something. Remember in a template the code only executes in context of larger (and probably third-party) code. Templates are just little boats in a sea of code I’ll probably never even look at. Contrast that with a script, a largely totally independent bit of code where I’m intimately familiar with every line in a single file. I’d put a program in the middle. It’s software that is largely entirely written by you and your team. A whole code base. You turn it on, you turn it off. I’m thinking of something like a video game. You run the game, play it, then your dad tells you to turn it off and do your homework and you do, so the program stops running.
So I suppose it’s like:
- Scripts = On-Demand, Independent, Short-Lived
- Programs = Running or not, Medium-Lived
- Templates = Part of something else, not run independently, runs in short bursts, Forever-lived
I’m not sure this is terribly useful, it was just on my mind.
I already can think of code that falls in the gaps. Not long ago I had to write some PHP that was intended to be run once and on-demand (a script!), but the intention was to migrate some WordPress metadata, so used copious WordPress methods to do that work, but also build and output UI, so… template?