Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Could be generated code?
Also, it's not an if-else, so switch won't do. Unless some pattern is applied -
@netikras if there are two if containing the same argument in that hell things are going to be spicy
-
@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]); -
@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 -
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 -
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"
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"
rant
javascript