68
u/TheBrainStone Mar 01 '21
I was wondering what’s so bad about this until I noticed the first condition.
24
91
u/Willinton06 Mar 01 '21
That’s a quantum string bro, it could be 2 languages at the same time so checking is a must
1
45
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 01 '21
Image Transcription: Code
let saveLanguage = false;
if (languagesVal == "" && languagesVal != "en-US" && languagesVal != "it-IT" && languagesVal != "fr-FR"
&& languagesVal != "es-ES" && languagesVal != "pt-PT" && languagesVal != "de-DE" && languagesVal != "ru-RU") {
languagesVal = "en-US";
saveLanguage = true;
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
15
20
u/VoilaLaViola Mar 01 '21
Typical Debug-Driven-Development.
Someone at that line must have found languagesval to be "" and added another condition, not even trying to read (or even worse: to understand) the surrounding code...
16
u/seventyeightist shameless Mar 01 '21 edited Mar 01 '21
I took it at face value this morning, chuckled to myself and thought "hope I never do that!", but then came back to it later (because my partner is also a software engineer, as am I, and he was having a bad day with "broke the build" so I sent him this in the sense of "at least you don't have to deal with this"...!)
What struck me is the name of the variable, not that we should ever take any notice of that in itself obviously, but note it is the plural "languages" rather than "language", like they are expecting an array, set or some other 'collection'-type... so you can say the first "and" should have been "or" (although it would be redundant anyway in that case) but it seems more likely that in some precursor to this, either languagesVal was empty or it could contain some set of (1 or more) languages, and this is a poor translation from "if languagesVal is empty or it doesn't contain any one of en-US, it-IT, fr-FR etc etc" then default to en-US".
New hobby identified: supplying a superficially plausible backstory to examples on r/badcode. I am the kind of person to generate 69,420 possibilities of things per day so I'm sure I can accommodate this!
6
3
u/bonedangle Mar 01 '21
Sometimes I have to ask myself what the inverse would be when looking at an expression. I found that helpful in making a decision on refactoring..
But in this case it's a basic lack of understanding values and simple logic 😂
5
u/DogsOnWeed Mar 01 '21
Either stick the language codes in a list and iterate through it as you check using a function, or put them in a switch statement, which despite looking better than if if if if if, probably takes just as long to write.
2
u/in-some-other-way Mar 02 '21
Read the first condition
1
u/DogsOnWeed Mar 02 '21
It's bugged I know, but even if it worked properly it's still a very tedious way of doing it.
1
u/RavynousHunter Mar 01 '21
At least have a damn speck of mercy and format the damned if, if you're hell bent on using the bloody thing over a switch! Or whatever Javascript's equivalent to Python's "x not in y" happens to be.
1
u/_Screw_The_Rules_ Mar 02 '21
So would a set/list of languages, that needs to be checked for the current language, be the best way to go about this or is there something even more smooth/faster/better?
2
u/path411 Mar 02 '21
Yeah, if you need to make multiple simple equality checks vs the same variable, just create a list and check if the list contains the variable.
1
1
u/lifeRunsOnCod3 Mar 01 '21
In python, this check can be achieved by creating a Set of the Permitted Languages and then checking if the language at runtime is a member of the set.
26
5
u/byornski Mar 01 '21
It's worse than that though due to the first check being == "". Excluding type-weirdness, the only way this can be true is if the other cases are also true so those tests are redundant.
4
u/SuperKatzilla Mar 01 '21 edited Mar 01 '21
Yep.
langs.indexOf(currLang)==-1in js. Edit: triple ticks to single ticks4
u/AutoModerator Mar 01 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/SuperKatzilla, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
-12
215
u/bnz_ Mar 01 '21
Fun fact: this code is somehow legacy and no one can remove all those useless &&