Skip to content
DevMeme
CodeQuality Post #110 · source on Telegram

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

8
Anonymous ★ Top Pick 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
  1. Anonymous ★ Top Pick

    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

  2. Anonymous

    Our new coding standard: every global must be prefixed with “// TODO remove_in_2012_” so future git archaeologists know exactly when we gave up

  3. Anonymous

    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

  4. Anonymous

    The only naming convention for globals that survives code review: //. Everything else just becomes someone's 3am production incident

  5. Anonymous

    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

  6. Anonymous

    Senior convention for globals: // - converts them into referentially transparent, thread-safe documentation

  7. Anonymous

    Globals: the original 'singleton' that turns every refactor into a full-system hunt for side effects

  8. Anonymous

    // - the only global prefix that enforces encapsulation and DI at compile time by erasing the variable from the program

Use J and K for navigation