all 6 comments

sorted by: hot top controversial new old
[–] 12 points 1 day ago*

it’s common programming advice that global variables are bad

They're not. This is what we tell beginners because one needs to be careful when maintaining a global state and beginners often do it horribly.

But having a global state is sometimes the right solution.

Pro tip: if you ever find yourself saying "x programming practice is always wrong" you're probably wrong.

  • source
  • [–] 5 points 1 day ago* (1 child)

    Isn't a database only as global as you make it? The connection is surely a real object that needs to be passed around and referenced just like any other.

    I don't really touch JavaScript or web dev so I might be misunderstanding the domain usage.

  • source
  • hideshow 1 child comment
  • [–] 2 points 22 hours ago

    I think his point is more that you might have multiple components that need database access in your app, so those component have a connection (or reuse a central access) and for simplicity reasons, you made 1 database for the whole app, rather than one per component. Once that happens there's a new path that could be used to pass information between the components in a way that isn't the function interface. Much like a global variable can be used that way. And there's a risk you start to use the database like a global variable store.

    I don't use databases much, so I don't have a strong opinion, but it's an interesting thought.

  • source
  • parent
  • [–] 6 points 1 day ago

    That's a weird take.

  • source
  • A database is a store for globals only if we don't care too much about introducing complexity.

    Globals often need to have the following properties:

    1. Need to persist for the duration of the program.
    2. Everybody needs the latest value out of the global variable.

    Now both of these things don't need to be explicitly a "global variable" in the programming sense. Logically speaking we just need code that implements the behaviour of the global variable well enough to satisfy our requirements.

    most DBs are most often an overkill for our requirements of a global variable.

    sqlite is a fantastic fit for this use-case if one wants some level of ACID properties.

    One can go with a redis cache or some other kind of disk caching KV store.

    Or simply just go with a global variable and measure how it does before doing all this crap.

  • source