• MadhuGururajan@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    45 minutes ago

    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.

  • zener_diode@feddit.org
    link
    fedilink
    arrow-up
    1
    ·
    53 minutes ago

    The article makes a good point, but I would argue its not the whole truth.

    A proper (relational) database does more than just storing values. It also stores relations between different values. And SQL is an extremely powerful way of working with those relations. I see the point that the author makes about projects with a database/state being worse, but in some projects I’ve worked on, I’ve been able to offload a great deal of work into the database schema.

    And also, a database doesn’t just store a few measly little variables. Often, you’ll be storing gigabytes or more in there. That’s completely infeasible to pass from function to function, or sometimes even hold in RAM at once. The point of a database is to store and manage data, in amounts that you don’t want to have to deal with yourself.

  • atzanteol@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    1
    ·
    4 hours 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.

  • brisk@aussie.zone
    link
    fedilink
    arrow-up
    5
    ·
    7 hours ago

    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.