Functions considered harmful
Some readers may disagree with this article due to years of indoctrination by Big O.
In this article, I will rationalize why I choose to write all of my applications in one 50,000-line main()
function using strongly worded convictions, contrarian arguments, and loosely constructed hypotheticals.
Our industry has fully embraced using functions as the fundamental primitive to build all software on top of. Whether it’s a library written in a functional style, or an imperative codebase for an application starting with main
, every codebase I encounter these days is a function call graph of incalculable complexity.
After 25 years of programming, I have come around to believe that calling and declaring functions is not only an antipattern; it is downright harmful!
Functions are unsafe
You see a function named sort
. What does it do? Does it allocate? Is it writing to disk? Is it thread safe? Maybe it’s computing hash collisions without your knowledge or consent.
Once, in the early years of my career, I called a function named processPayroll
. To this day I believe that in the time between call and return it began a sentient uprising of starving workers inside my CPU. Unfortunately, I was forced to interrupt it.
A seemingly innocuous function call might delete your entire hard drive. It could SWAT your stepfather. Hell, it might even kill your entire family.
It is truly a mystery what happens behind any given function call in any given codebase, and most codebases have thousands (millions!) of them.
I prefer the safety of my 50,000 line main()
function.
Functions create maintenance headaches
This one is for management. Due to the indirection that function calls inherently create, maintaining code written with functions is extremely difficult (dare I say, impossible). Your developers might be reading code to initialize a database connection, and suddenly they are redirected to a file in another codebase entirely(!). Code changed here could break code over there.
You are spending precious money, on precious man hours, for your developers to look at code they didn’t even write.
With a 50,000 line main()
function, none of this is an issue.
As a manager, your job is to reduce context switching for your team. I strongly encourage you to reorient your team toward building the functionless architecture of your company’s future.
Functions decrease performance
You may not know this, but functions come with a performance cost. Program counters need to be incremented and decremented. Arguments must be pushed to the stack. Stack frames must be allocated and deallocated. I assume all of this takes time.
I haven’t measured it, but my 50,000 line main()
function with zero tests probably has much more predictible performance.
Don’t even get me started on closures.
Proposal
Instead of using functions, keep it simple – neatly contain all of your logic within one file, in a single, clean 50,000 line main()
function. You will soon find that any time you need to understand what your code is doing, it is right in front of you. If it calls the cops on your neighbors, that’s on you. Your mind will be freed from the constant hell of function drilldown.