• masterspace@lemmy.ca
      link
      fedilink
      English
      arrow-up
      0
      ·
      6 days ago

      The equivalent in JavaScript / TypeScript would actually be function () {}, this is the syntax for named functions.

      C# is the same as bash though.

          • SpaceCowboy@lemmy.ca
            link
            fedilink
            arrow-up
            0
            ·
            6 days ago

            Yeah for whatever reason, FE devs want to make everything a const. It’s like a religious belief or something, it’s really kinda weird.

            const fun = () => { const something = “whatever” const array = []; array.push(someting)

            for (const thing of array) { if (thing === ‘whatever’) blah(thing) } }

            Semicolons? Optional. Which quotes you should use? Whatever you feel like! But you must declare things as a const wherever possible! Even if it’s an array that you’re going to be changing, declare it as a const because you should know that you can push things into a const array, and since it’s possible to declare it as a const, you must declare it as a const.

            Why is this? Nobody knows, but it’s important to FE devs that you use const.

            • Ghoelian@piefed.social
              link
              fedilink
              English
              arrow-up
              0
              ·
              6 days ago

              The reason is very simple, performance. If a value doesn’t need to be changed, don’t declare it as mutable. This isn’t just a front-end thing btw.

              • SpaceCowboy@lemmy.ca
                link
                fedilink
                arrow-up
                0
                ·
                6 days ago

                Pushing something onto an array isn’t changing the array? It’s not changing the reference to the array, but from a style standpoint it doesn’t make sense.

                And if you’re declaring a const within the scope of a function, it’s still allocating memory when it enters the scope and disposing it when it leaves the scope, same as a variable. There’s no performance benefit to do this.

                Something like const CONSTANT_VALUE = “This never changes” has a performance benefit and is actually how other languages use constants. The value will always be the same, the compiler understands this and can optimize accordingly. If you’re declaring an iterator or the result of calling a webservice to be const it’ll be a different value every time it runs that code, so it’s not something a compiler can optimize. In style terms, it’s a value that’s different every time you get to that line of code, so why would you want to call it constant?

                You’re comment indicates the FE dev obsession with always using const stems from a misunderstanding of how computers work. But of course many religious beliefs originate from a misunderstanding of the world. Whatever man, I just make it a const to make the linter happy, because it’s dumb FE bullshit LOL.

                • masterspace@lemmy.ca
                  link
                  fedilink
                  English
                  arrow-up
                  0
                  ·
                  5 days ago

                  Lol.

                  Pushing something onto an array isn’t changing the array? It’s not changing the reference to the array, but from a style standpoint it doesn’t make sense.

                  So you’re arguing for writing things as they seem, not the way that computers treat them?

                  You’re comment indicates the FE dev obsession with always using const stems from a misunderstanding of how computers work.

                  Maybe rethink this.

                  • SpaceCowboy@lemmy.ca
                    link
                    fedilink
                    arrow-up
                    0
                    arrow-down
                    1
                    ·
                    5 days ago

                    A constant inside a function is not constant to the computer. It’s only constant within the scope of the function. So it’s not constant to the computer since every time the function is called the “constant” will have a different value.

                    Do you even know what a real constant is?

                    You maybe need to rethink some things.