A Senior Developer's Guide to Naming Global Variables
Description
A screenshot of a tweet from a user named Richard (@zzaaho). The tweet poses a question and gives a sarcastic answer: 'Q: What is the best prefix for global variables? A: //'. The tweet was posted on January 21, 2019, and shows 209 retweets and 544 likes. The humor is derived from a deep-seated principle in software engineering. Global variables, which can be accessed and modified from anywhere in a program, are notorious for creating bugs that are difficult to trace. The '//' characters signify a single-line comment in many popular programming languages. The joke, therefore, is that the best way to handle a global variable is to comment it out, effectively advising against its use altogether. This resonates strongly with experienced engineers who have learned the hard way about the perils of shared mutable state and prefer more encapsulated, predictable architectural patterns
Comments
8Comment deleted
Using a global variable is like leaving your house keys under the doormat. It's convenient until you come home to find your entire state has been unexpectedly mutated
Our new coding standard: every global must be prefixed with “// TODO remove_in_2012_” so future git archaeologists know exactly when we gave up
The same developer who suggests commenting out global variables probably has a 10,000-line God class named "Utils" that's imported in every single file of their microservice architecture
The only naming convention for globals that survives code review: //. Everything else just becomes someone's 3am production incident
This tweet perfectly encapsulates the senior engineer's visceral reaction to global variables - the '//' prefix is the kindest thing you can do to them. It's the architectural equivalent of Marie Kondo asking 'Does this global state spark joy?' and every experienced developer immediately answering 'No, thank it for its service and comment it out.' After debugging enough race conditions, side effects, and 'works on my machine' issues caused by mutable global state, you learn that the best global variable is a dead global variable. The 544 likes represent 544 developers who've been burned by globals at 3 AM during a production incident
Senior convention for globals: // - converts them into referentially transparent, thread-safe documentation
Globals: the original 'singleton' that turns every refactor into a full-system hunt for side effects
// - the only global prefix that enforces encapsulation and DI at compile time by erasing the variable from the program