• UndercoverUlrikHD@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    9 months ago

    Is it really tempting for people? They’ve given me too many headaches when I’ve had to reformat or add functionality to files.

    Unless it’s a simple single use script that fit on the computer screen, I don’t feel like global variables would ever be tempting, unless it’s for constants.

    • yiliu@informis.land
      link
      fedilink
      arrow-up
      1
      ·
      9 months ago

      They’ve given me too many headaches…

      I.e. you did use them, but learned the hard way why you shouldn’t.

      Very likely OP is a student, or entry-level programmer, and is avoiding them because they were told to, and just haven’t done enough refactoring & debugging or worked on large enough code bases to ‘get’ it yet.

      • Dave@lemmy.nz
        link
        fedilink
        arrow-up
        1
        ·
        9 months ago

        Hey, don’t you group me in with people who have had a small amount of real training!

  • idunnololz@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    9 months ago

    Just create a global object and stuff your variable in there. Now you have a global singleton and that’s not a purely bad practice :D

        • SkyNTP@lemmy.ml
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          9 months ago

          Software dev is full of obscure keywords that describe otherwise pretty simple or basic concepts you stumble upon in practice naturally and that you probably already understand.

          • singleton: a class/object that is designed to be single use, i.e. only ever instantiated with a single instance. Typically used when you use class/objects more for flow control or to represent the state of the program itself, rather than using it to represent data
          • immutable: read-only, i.e. unchangeable
          • dependency injection: basically when you pass a function or object into another function object, thereby extending their effective functionality, typically for modular code and to separate concerns.

          Here’s one more of my favourite examples of such a keyword: memoization