I'm honestly trying to figure out if this article is sarcasm... because the ****-show of your "right way" taking a simple if/else (that should be a switch/case) and vomiting up four times the code that's ten times harder to follow? Just shows the batshit crazy hoops the "don't if else" nutters will jump through, and what a disaster Java always feels like for me when it comes to actually manipulating data sets.
I mean seriously, all that code for nothing doing the job of:
private string findCountryName(string internetCode) {
switch (internetCode) {
case "de": return "Germany";
case "fr": return "France";
case "ar": return "Argentina";
}
return "Suffix Not Valid":
}
Is just nuts. And it also shows why so many people are leaving Java for greener pastures. Even the hamburger to Java’s ham — the once pathetic and now powerful JavaScript — is cleaner, and simpler at this!
const
countryNames = {
de: "Germany",
fr : "France",
ar : "Argentina"
},
findCountryName = (code) => countryNames[code] || "Suffix Not Valid";
This is what’s wrong with so much of the pedantic trash the people bitching about “code smell” — especially those complaining about if/else — because of course throwing two to ten times the code at the same problem, introducing unnecessary slow loops and taking a dump on the namespace is what’s going to make it easier to maintain, easier to expand, and easier to write. Of course it is.
The real kicker of course being the card stacking that indicates either total ignorance of what return does, or intentionally trying to make the code look worse than it is that is “else after return”. One of the most dumbass things one could possibly ever write regardless of what programming language is in use. And I’m seeing it in nearly every mind-numbingly silly “if/else is bad” article. Proof positive of either maliciously sabotaging the one you don’t like, or not knowing enough about programming to flap one’s yap about the entire topic!
Because to be brutally frank I would take this:
private string FindCountryName (string internetCode) {
if (internetCode == "de") return "Germany";
if (internetCode == "fr") return "France";
if (internetCode == "ar") return "Argentina";
return "Suffix not Valid";
}
…all day every day over the disaster of “dictionary for nothing” code bloat BS you call “right” when it comes to execution time, code clarity, ease of maintenance, or any other meaningful metric of code quality. Although again, when checking just one variable switch is always a better choice.
Again the real comedy gold of it, not just is it if/else clearly doing switch/case’s job, it’s not even the job for “else” even when using “if” because you’ve go return in there! Is there some book or guide out there you guys are all blindly coping the “else for nothing” rubbish from? I’ve seen dozens of articles this past year where every single one of them starts out with that mental midgetry!