Matthew Tyson
Contributing Writer

Complexity bad: An interview with HTMX creator Carson Gross

feature
Mar 13, 20249 mins

How one computer science professor in Montana aims to supplant reactivity, compete with JavaScript, restore true REST, and make the web more developer-friendly.

Complexity, problem solving. Man solves a maze.
Credit: alphaspirit.it/Shutterstock

Carson Gross is the creator of HTMX and Hyperscript, the mind behind The Grug Brained Developer, a professor of software engineering at Montana State University, and co-author of Hypermedia Systems

It was a pleasure to pick Carsonโ€™s brain about the impetus behind projects like HTMX and Hyperscript, the failures of REST, why JavaScript is here to stay, and much more.

Tyson: Itโ€™s hard to pick where to start here so Iโ€™m going to go with Grug Brained Developer. I kind of feel like I could just refer everyone there for all programming-related questions.

Gross: Ha, I appreciate that. I think itโ€™s worth reading for most developers. I kind of geared it towards younger devs, though. It certainly isnโ€™t perfectโ€”hard to be when you are using a caveman voice and trying to be funnyโ€”but I think most of the advice is solid.

Tyson: Itโ€™s true that most of the coding trouble Iโ€™ve ever gotten myself into was mainly a result of thinking I was smart.

Gross: Yeah, I think everyone who has programmed for a while has experienced that point at which you realize youโ€™ve gotten in too deep and canโ€™t keep the system in your head anymore. I donโ€™t know if itโ€™s always thinking you are too smart, or just not recognizing when things start to get too complicated. Also, social dynamics in tech jobs punish people who say things like, โ€œUh, this is too complicated to me.โ€

Tyson: That is so true about the atmosphere around admitting itโ€™s too complicated. One time I chatted to a friend โ€œJavaScript is so flexible, you can code your way out of anything,โ€ and he chatted back, โ€œHaha, thatโ€™s dangerous talk.โ€  He was right. 

Do you have any thoughts about JavaScript as a language?

Gross: I think JavaScript is an okay scripting language. It isnโ€™t a great general-purpose language in my opinion. In a somewhat bizarre way, it feels like C++ to me: it has too many features and ways to do things now. On the other hand, JavaScript has one unbelievable feature: itโ€™s there, in the browser. And that will make it the premier scripting language for the web for the foreseeable future, in my opinion.

Tyson: Iโ€™m pretty impressed by Hyperscript, but I get the feeling itโ€™s kind of a fun sideline for you. Would you tell us about the technical experience of implementing the parser and so on?

Gross: Sure. Hyperscript is a scripting language that โ€œcompetesโ€ with JavaScript as an option for front-end scripting. Itโ€™s based on HyperTalk, the scripting language for HyperCard, and has features that I think make for a better authoring experience when you are simply trying to script on a web page. For example, the Hyperscript runtime automatically resolves Promises it encounters, so script writers donโ€™t need to deal with them. It has a natural, English-like syntax, which some people love and a lot of people hate. It is definitely a passion project of mine, but Iโ€™m hoping to get it to 1.0 this year, after HTMX 2 ships.

As far as its implementation, yep, itโ€™s implemented in JavaScript as a lexer, parser, and eval-based runtime. I was surprised that the JavaScript runtime is fast enough to let me get away with that, but it is. I wouldnโ€™t try to implement a Bitcoin miner in Hyperscript, but itโ€™s fast enough for light DOM scripting. The Hyperscript parser and runtime is right at the edge of the complexity Iโ€™d be willing to take on with JavaScript.

Tyson: HTMX is another impressive bit of coding. My introduction to HTMX was one of the most-read articles on InfoWorld last year, which shows thereโ€™s a lot of interest in it. This project seems more serious, too, in that it looks to restore at least the possibility of a true REST architecture.

Gross: Yeah, itโ€™s been a wild year for HTMX. For readers that havenโ€™t heard of it, HTMX is a JavaScript library I wrote that lets you add attributes, very similar in spirit to the href and action attributes of links and forms in standard HTML. Using those attributes, you can trigger AJAX requests and then replace parts of the DOM with the HTML that the server responds with.

Tyson: Iโ€™d say HTMX extends the vocabulary of good olโ€™ HTML to cover some modern use cases, in particular, AJAX. Is that a fair description?

Gross: Yep, thatโ€™s a good way to explain it. I sometimes say that HTMX โ€œcompletes HTML as a hypermediaโ€ in that it allows any element on the page to issue an HTTP request in response to an event, and then place that response anywhere in the DOM.

That probably sounds pretty simple, and it is, but with just that little generalization of HTML you unlock a lot of UX patterns that have traditionally been reserved to JavaScript. Examples include infinite scrolling, lazy loading sections of a page, and as-you-type server-side validation.

Tyson: I have to say, I thought I pretty much knew what REST meant but I was wrong for a long time. You address how that came to be in your article, How did REST come to mean the opposite of REST?

Would you mind distilling for readers the difference between REST-as-intended versus REST as it is commonly implemented?

Gross: *laughs* Well, we arenโ€™t changing how the industry uses the term REST. Today that term means โ€œJSON APIs over HTTPโ€ and thatโ€™s what itโ€™s going to mean likely for the rest of our careers.

However, that isnโ€™t what REST originally meant. Instead, REST was a description of the way the World Wide Web worked (itโ€™s network architecture) before JSON APIs existed; just links and forms driving interactions via a browser. The term REST came from a famous dissertation by Roy Fielding, and he defined a series of constraints that a RESTful system needs to conform to.

Today, most JSON APIs that are called โ€œRESTfulโ€ donโ€™t satisfy those constraints. Even more hilariously, a simple static website with a couple of HTML pages that link to one another does satisfy all those constraints. So, someone who creates a simple static website is, at some level, a better โ€œRESTโ€ engineer than people with fancy โ€œRESTfulโ€ JSON API engineer titles on their resumes.

The language isnโ€™t going to change, but I think itโ€™s worth understanding the original meaning of the term and, in particular, what is called the uniform interface constraint of REST to really understand why the web is so flexible.

Tyson: I havenโ€™t been in a programming class since the beginning of the millennium. Do you still use books?  Just kidding. (I think.) 

Whatโ€™s it like being a teacher of programming?  Can I take CSCI 468 from you?

Gross: Ha!  Well, if you want to take 468, which is the compilers class I teach, you can just read Crafting Interpreters by Bob Noystrom and get about 50% of it. We target the JVM with bytecode on the back end and, to be honest, that isnโ€™t that interesting unless you are a big Java guy.

I like teaching. There are definitely downsides: the pay sucks and there is bureaucracy to deal with. But there are lots of upsides and I enjoy getting students over the hump to where they feel like they can program on their own.

Tyson: Yep, I am definitely a Java guy.

So, this is returning a bit to the REST topic, but from a higher perspective. You are co-author of Hypermedia Systems, which makes the case that the way the web is designed provides an application architecture that we can build upon, if we look at it right.

Gross: Definitely, and HTMX tries to build on that network architecture by enhancing HTML as hypermedia, rather than replacing that original architecture with a new one, namely fixed-format JSON Data APIs. In the book, I try to outline the systemic nature of properly functioning hypermedia. For example, for a long time, I didnโ€™t appreciate how important the hypermedia client (e.g., browsers) was for the uniform interface to work properly. You really need to have a whole systemโ€”hypermedia like HTML, a network protocol like HTTP, hypermedia servers, and hypermedia clientsโ€”to make the whole thing work as intended.

Tyson: In closing, Iโ€™d like to summarize everything important in a few short, amusing remarks, but youโ€™ve already done that. I try to find things I disagree with in grugโ€™s philosophy, but I canโ€™t really. Expression complexity, closures, saying no โ€ฆ grug has good advice for all of these topics.

But Iโ€™m curious about grugโ€™s feelings about the visitor pattern. Iโ€™ve used it and thought it went well. In the end, the project didnโ€™t ship, but the code was solid. Tell me whatโ€™s wrong with the visitor pattern.

Gross: Yeah, Iโ€™m not 100% against the visitor pattern, despite what the grugbrain essay says. (In general, Iโ€™m not 100% for or against anything in software development.)  However, I often think itโ€™s better to encode operations directly in a tree rather than have another side thingie that performs operations over the tree. Sometimes this mixes concerns a bit, but I donโ€™t mind that in this case.

As an example, in Hyperscript, the parse tree elements have the eval methods defined on them and are defined directly in the spot they are parsed. This mixes concerns, but I think it adds clarity to the program, because when you want to understand how, for example, the โ€œwaitโ€ command works, you can go to one place and see how itโ€™s both parsed and evaluated. I find that very useful.

Tyson: Why is it that we know complexity is bad for us but we are drawn to it anyway? 

Gross: Well, again, I think there are industry pressures at play here. Software is a brutal industry and appearing to be unintelligent can harm your career badly. This means that we all feel pressure to demonstrate we are clever, and also feel pressure to not admit that someone elseโ€™s code is confusing or seems too complicated to us. In many circles, โ€œsophisticatedโ€ is praise and โ€œdumbโ€ is criticism. 

I understand that dynamic and Iโ€™m sympathetic to engineers who feel those pressures. This is one reason why I suggest senior engineers who have a secure position in an engineering organization should often loudly proclaim that things seem too complicated to them if, in fact, they do. This makes it okay for younger engineers to voice their concerns, as well, and relieves those pressures organizationally, to an extent at least.