• Tartas1995
    link
    fedilink
    arrow-up
    14
    ·
    1 year ago

    Yes and no. Any programming language encourages nesting as in the end the computer does nest your code. So it is only normal and predictable that languages would reflect that. BUT! Nest logic can often be inverted and by doing so, reduce how much nesting you need to do.

    If (data is not null) {
        If (data has field x) {
             Return data x
        } else return null
    } else return null
    

    Can be

    If (data is null) return null
    If (data hasn't field x) return null
    Return data x
    
    • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
      link
      fedilink
      arrow-up
      6
      arrow-down
      8
      ·
      1 year ago

      I’m not arguing that avoiding deep nesting is a good idea, or that techniques foe doing that don’t exist. I’m just pointing out that Js style programming naturally leads you to nesting things because of the nature of callbacks. Notice how your example isn’t using callbacks.

      • Tartas1995
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

        Yeah and you can void nesting there just as easily and you have the same issues in any other programming language. You just need to create functions. Also JavaScript is not single threaded… you only have access to the dom on one thread, for obvious reasons.

        Please explain to me how you do e.g. file downloads without a callback in your favorite language. If you solution involves having the main thread being stuck in a while loop, I am not sure if your complain about nested code can be taken seriously.

        • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
          link
          fedilink
          arrow-up
          2
          arrow-down
          9
          ·
          1 year ago

          Sure, you end up passing higher order functions around, and my point is that complexity obviously goes up. There’s a reason callback hell is a well known thing in Js land. Meanwhile, Js is single threaded from user perspective. The fact that there is a background rendering thread in client side js is completely tangential to the discussion.

          Finally, the problem with callbacks is generally seen in server-side Js runtimes. A great example is if you have an HTTP handler that needs to get data from a db. In a language with user accessible threads you just make a db operation synchronously and return the result. In Js, you have to do a callback. The reason you can do the operation synchronously when you have threads is due to the fact that HTTP handler thread can accept a request and then dispatch a new thread to handle it while waiting for other requests.

          • Tartas1995
            link
            fedilink
            arrow-up
            2
            ·
            1 year ago

            It is not really tangential to the discussion. You claimed it is because Js single threaded. Also it is not single threaded from the “users” perspective if you mean the developer. There are workers.

            If your issue is asynchronous function calls, just call synchronous functions. You might be stuck in a while loop somewhere but if you prefer that, use it. There are sync functions for everything in Js and/or you can easily create them yourself.

            • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
              link
              fedilink
              arrow-up
              2
              arrow-down
              9
              ·
              1 year ago

              Js is single threaded from user perspective. You have no access to the threading runtime as a user and cannot spawn a thread in Js to do some background work. Workers are a recent addition, but using them is quite different from what I’m talking about.

              And being stuck in a while loop is precisely why people have to use callbacks and why all the APIs are async. This is literally the problem. If you’re dealing with any non trivial load, you are forced to use async mechanics.

              • Tartas1995
                link
                fedilink
                arrow-up
                1
                ·
                1 year ago

                And what is the alternative? How do you handle any non trivial load in any other language? Without a second thread or while loop. Because apparently you dislike both. You like it sync for your database -> while loop somewhere, but while loops are bad. But asynchronous is bad because it adds complexity to your code when you use functions to reduce the nesting.

                On nodejs, the platform that you talked about earlier, they are literally called worker_threads". So they are different? How? Why can’t you use them?

                • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
                  link
                  fedilink
                  arrow-up
                  1
                  arrow-down
                  9
                  ·
                  1 year ago

                  I literally just explained this to you in a prior comment. Let’s say you have a web server that’s accepting connections. You have one thread that grabs the connection and spawns a thread from a pool of workers threads to handle it. Then it goes back to listening for connections. This is how things work on platforms like the JVM. The runtime manages the execution of the threads. This is similar to the way the OS handles scheduling processes. When you’d doing async by hand you’re basically handrolling your own scheduler each time.

                  What stops you from using worker threads is lack of ergonomics. Just the fact that each worker has to live in a separate file creates friction. In a sane language you can just do new Thread().start() and you’re done. In Js it’s a Rube Goldberg machine as always. When you look at at actual code people write in Js, you see async code everywhere. So, maybe you can answer why you think that is, and why people aren’t using worker threads to write sync style code they’d write in other languages.

                  • Tartas1995
                    link
                    fedilink
                    arrow-up
                    1
                    ·
                    1 year ago

                    You seem to misunderstand my points.

                    And I don’t think you want to.