6

Testing a script embed plugin I am building on various random websites, and came across this.

Like, bruh; have you ever heard of a javascript map? Basic functional programming? Or even a switch statement?

It's the same statement, over and over again, but with different parameters. Even old javascript had enough tools to do this with at least a basic stench of "efficiency"

Comments
  • 2
    Is this some kind of a 301 redirect plugin?
  • 1
    Could be generated code?

    Also, it's not an if-else, so switch won't do. Unless some pattern is applied
  • 0
    I hope that is generated.
  • 0
    I feel murderous
  • 1
    @netikras if there are two if containing the same argument in that hell things are going to be spicy
  • 1
    This makes me want to learn functional programming just to fix it.
  • 2
    @netikras the conditions in the if involve always the same variable, so they are mutually exclusive. Hence a switch statement would have been better.

    But since conditions have all the same structure and the body contains just one and always the same function call with varying arguments a better approach would be to make a dictionary in the form dict = { key: value, ... } where keys are the strings compared to location.pathname and values are the strings used in the replace method and then just call:

    window.location.replace(dict[location.pathname]);
  • 0
    @michezio you're right. I just skimmed over the screenshot and missed the fact that it's == everywhere and there are no regexes.

    My mistake
  • 0
    Must be a newbie, I recall when I first started, I didn't know I can create a model to hold all data related to something (i.e. user) so I had three list of strings to spread my data....

    Read them by index 😅

    Sweet old days of not knowing but feeling like hacker man lol
  • 0
    Fjesus Xhrist, I thought it was a log.
    I hate this.
  • 0
    Not to sound like a dick but if the first thing you think of when you see this is "this should be a switch statement" I think you're missing the bigger picture.

    The first thing you should think of should be "redirect should be done server-side and not in clientside javascript"
Add Comment