Comment by worik

Comment by worik a day ago

8 replies

> If the function is called from 37 places, you need to refactor your code,

Really?

I do not have to think hard before I have a counter exampl: authentication

I call authenticate() is some form from every API

All 37 of them

bognition a day ago

If you are explicitly calling authenticate() for each api, you’re doing it “wrong”. At that point you want implied authentication not explicit authentication. Why not move it to some middleware that gets called in every api call?

  • kazinator a day ago

    Because then you are calling middleware_caching_auth_broker() from 37 places instead of authenticate(). Just the name has changed, not the 37.

    • bognition an hour ago

      No that’s not how this works. You register the middleware with your web framework and it gets called as part of all web requests before the request hits your endpoints. This allows you to trust that authentication has been called for all api calls

    • all2 a day ago

      But that's ok because the calls are hidden from the programmer.

      I'm not sure if my response is serious or tongue-in-cheek. Maybe a bit of both.

    • KPGv2 21 hours ago

      > Because then you are calling middleware_caching_auth_broker() from 37 places

      No you aren't. You aren't really calling it from anywhere. The framework you're using, which you aren't writing, is calling the registered middleware.

      The topic here is complexity for the code structure because it's called from 37 different places. A registered middleware doesn't run into that issue because it doesn't get called anywhere that "code structure complexity" matters.

      Your reasoning is isomorphic to "I'm calling a bit shift millions of times because I have written some code in a programming language." Technically true but not what we're talking about here.

      • philwelch 20 hours ago

        That sounds like the programming equivalent to thinking that food just comes from the grocery store.

        • tsurba 17 hours ago

          And going to a grocery store instead of 37 individual farmers…?

kazinator a day ago

The strongest interpretation of the remark is not that you need to refactor because you have a function called 37 times (which is likely a good thing) but rather that if you think you need to move an if statement into or out of it, you face refactoring.