• UndercoverUlrikHD@programming.dev
    link
    fedilink
    arrow-up
    121
    ·
    11 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.

    • PetDinosaurs@lemmy.world
      link
      fedilink
      arrow-up
      72
      ·
      11 months ago

      Most people suck at software engineering.

      Plus, there’s always the temptation to do it the shitty way and “fix it later” (which never happens).

      You pay your technical debt. One way or another.

      It’s way worse than any gangster.

      • rodolfo@lemmy.world
        link
        fedilink
        arrow-up
        15
        ·
        11 months ago

        amen

        Plus, there’s always the temptation to do it the shitty way and “fix it later”

        double amen

              • decerian@lemmy.world
                link
                fedilink
                arrow-up
                6
                ·
                11 months ago

                Why is that weirder? The people writing scientific software are, by and large, less good at writing software than people who only specialize in software development. I’d expect there to tons of terrible engineering practices in an old code base like that

                • PetDinosaurs@lemmy.world
                  link
                  fedilink
                  arrow-up
                  3
                  ·
                  edit-2
                  11 months ago

                  good question.

                  Because even trivial things like Fourier transforms (to people like me) are very difficult to understand to those that don’t know them. They took me years to understand. Non scientific software engineers do not understand those. It’s just a different course of education.

                  You’re also right about old code base as well. Algorithms like these belong in c++ (or C or fortran), and it’s extremely difficult to explain why to people who have no understanding of numerical computing.

                  It’s just different education.

          • rodolfo@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            11 months ago

            I wish I was so lucky to have comments.

            in real life, I’m fighting with - I’m not joking - a few dozen “quick patches”. code does not reflect in any point functional requirements, and dude is adamant he’s in the right and supersarcastic in any occasion.

            • PetDinosaurs@lemmy.world
              link
              fedilink
              arrow-up
              3
              ·
              11 months ago

              I’ve been working at my current company for almost a year.

              I had no idea it could be this bad.

              I actually had to fight/plead with someone to “please read the code”. Guy did get fired though.

      • manapropos@lemmy.basedcount.com
        link
        fedilink
        arrow-up
        4
        arrow-down
        1
        ·
        11 months ago

        If you’re smart you do it the quick and easy way and leave the company before it bites you in the ass. Only suckers stay with the same company for more than a few years

    • yiliu@informis.land
      link
      fedilink
      arrow-up
      24
      ·
      11 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.

    • BorgDrone@lemmy.one
      link
      fedilink
      arrow-up
      20
      arrow-down
      1
      ·
      11 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.

      I don’t get it either. Why would you ever feel the need for them to begin with?

    • fluxion@lemmy.world
      link
      fedilink
      English
      arrow-up
      2
      ·
      11 months ago

      As with the sexual connotation here, the temptation is not rooted in long-term considerations like future maintainability

    • ZILtoid1991@kbin.social
      link
      fedilink
      arrow-up
      3
      arrow-down
      1
      ·
      11 months ago

      Depends on what you’re doing. Functional programming has its own downsides, especially once you want to write interactive programs, which often depend on global states. Then you either have to rely on atoms, which defeat the purpose of the functional programming, or pass around the program state, which is janly and can be slow.

      I personally go multi paradigm. Simpler stuffs are almost functional (did not opt for consting everything due to performance issues), GUI stuff is OOP, etc.

    • GTG3000@programming.dev
      link
      fedilink
      Русский
      arrow-up
      1
      ·
      edit-2
      11 months ago

      Well, if you’re writing something the user will be looking at and clicking on, you will probably want to have some sort of state management that is global.

      Or if you’re writing something that seems really simple and it’s own thing at first but then SURPRISE it is part of the system and a bunch of other programmers have incorporated it into their stuff and the business analyst is inquiring if you could make it configurable and also add a bunch of functionality.

      I also had to work with a system where configurations for user space were done as libraries setting global constants. And then we changed it so everything had to be hastily redone so that suddenly every client didn’t have the same config.

  • idunnololz@lemmy.world
    link
    fedilink
    arrow-up
    76
    arrow-down
    1
    ·
    11 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
          11 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

    • xmunk@sh.itjust.works
      link
      fedilink
      arrow-up
      9
      ·
      edit-2
      11 months ago

      Real enterprise programmers know that everything should be on the stack… so they declare a List《void*》 in main.

      • Walnut356@programming.dev
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        11 months ago

        I feel like it’s like pointers.

        “Variable” refers to the label, i.e. a box that can contain anything (like *ptr is a pointer to [something we dont know anything about])

        Immutable describes the contents, i.e. the stuff in the box cant change. (like int* ptr describes that the pointer points to an int)

        Rust makes it very obvious that there’s a difference between constants and immutable variables, mainly because constants must be compile time constants.

        What do you call it when a variable cant change after its definition, but isnt guaranteed to be the same on each function call? (E.g. x is an array that’s passed in, and we’re just checking if element y exists)

        It’s not a constant, the contents of that label are “changing”, but the label’s contents cant be modified inside the scope of that function. So it’s a variable, but immutable.

    • Eufalconimorph@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      ·
      11 months ago

      And more generally mutable aliasing references of any sort are evil. Doesn’t mean they’re not useful, just that you need magic protection spells (mutexes, semaphores, fancy lock-free algorithms, atomics, etc) to use them safely. Skip the spell or use she wrong one, and the demon escapes and destroys all you hold dear.

  • KittyCat@lemmy.world
    link
    fedilink
    arrow-up
    24
    ·
    11 months ago

    You can do better, define intergalactic variables that share the same memory location across multiple programs so you can seamlessly pass variables from one to the next.

  • Successful_Try543@feddit.de
    link
    fedilink
    arrow-up
    17
    arrow-down
    1
    ·
    11 months ago

    I’ve once had a course involving programming and the lecturer rewrote the code, which we were usually using at our institute, making ALL variables global. - Yes, also each and every loop counter and iterator. 🤪

  • fbmac@lemmy.fbmac.net
    link
    fedilink
    arrow-up
    14
    arrow-down
    2
    ·
    edit-2
    11 months ago

    I asked stable diffusion for a photo-realistic version of this image. This isn’t what I had in mind

  • stephfinitely@lemmy.world
    link
    fedilink
    arrow-up
    11
    ·
    11 months ago

    I am not a programmer who knows how to program. I know this because global variables are how I fix most the issue I run into, but are constantly told this wrong.

  • BilboBargains@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    11 months ago

    Our Father, who art in Microsoft HQ,

    hallowed be thy naming conventions;

    thy architecture;

    thy will be done;

    on earth as it is in Linus Tech Tips.

    Give us this day our daily StackOverflow.

    And forgive us our 'sploits,

    as we forgive those who trespass against our user stories.

    And lead us not into temptation;

    but deliver us from a thicket of global variables.

    For thine is the irritating project manager, the power and the glory,

    for ever and ever.

    Or at least 7 years until obsolescence.

    Amen.

  • Blackmist@feddit.uk
    link
    fedilink
    English
    arrow-up
    5
    arrow-down
    1
    ·
    11 months ago

    Nothing wrong with global variables.

    If anyone asks just say it’s the singleton pattern.