5

I am curious to know why this was closed as "unclear what you are asking".

I would need to match the average score to a letter grade. That means

if (90 < avg && avg < 100) {
    return 'A';
} 
// and so on until 'F', with 5 if-else statements 

That's a lot of repetition, and the ranges I'm matching to are of the same length. Is there a more efficient way to do this? By efficient, I mean not having to repeat the if-else statement 5 times

There were 8 answers given, there were hot discussions to the point in the comments. It seems everybody got the problem, except for 3 people who voted to close, the same two voted to delete it shortly afterwards.

It's worth mentioning that OP faced difficulties to add their code snippet.

I tried adding code, but it won't format correctly (I'm new to stackoverflow). The instructions say to type two whitespaces for line break but that never worked...

failed revision

Their comment was ignored, and the question was closed ignorantly (in my opinion).

UPDATE

The question was closed again as "too broad". Please, put yourself in OP's shoes, and read the explanation

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.

The problem was outlined (getting rid of multiple if-else statements). The scope was specific (grading system).

Avoid asking multiple distinct questions at once.

One question was asked.

What exactly could OP improve? No one bothered to explain. It was closed again without letting OP read this discussion, and/or make changes. I love this community :)

19
  • 5
    I can't see the question - it's deleted. Based on the title ("is-there-a-more-efficient-way-to-write-multiple-if-else"), though, it's likely to be Unclear or Too Broad. Depends on how it's worded. And having a lot of answers could be a sign that the question is either Unclear or Too Broad or both - if each tries to do a very different thing then it's Broad, if they are tackling different problems, it might be Unclear. Or both.
    – VLAZ
    Commented Aug 16, 2019 at 6:49
  • 5
    @VLAZ No unclearness there: everybody tacked the same problem. Basically, there were 3 main approaches (map, enum, switch) and, as it usually happens here, it turned out to be 8 answers around these 3 methods.
    – Andrew
    Commented Aug 16, 2019 at 7:00
  • 4
    @RobertLongson OK, with the question here, I'd agree that it's not unclear. However, with this question there is likely a dupe - I don't know the language but I think I've seen "How to replace chained if/else" questions for each I've used, And those questions are fairly frequent. I personally think it should have been marked as dupe, not closed as unclear and not deleted. If it involved more code, then migration to Code Review might also have been a consideration, but it probably needs more than just "here is the if/else chain I have" - a whole algorithm or a class might be suitable.
    – VLAZ
    Commented Aug 16, 2019 at 7:08
  • 1
    @VLAZ you make a completely different question when you add a specific domain to the contextless "How to replace chained if/else". The domain was grades and grading systems, which I find quite interesting.
    – Andrew
    Commented Aug 16, 2019 at 7:18
  • 5
    With the ongoing experiment of a reduced number of close votes needed to close a question we should expect a higher false positive rate. On the other hand, the question was initially really bad and with a score of -7 is hardly salvageable. It's good though that Andrew Tobilko at least tried. Commented Aug 16, 2019 at 7:18
  • 4
    @AndrewTobilko I don't think it matters if you have grades, or perhaps other stuff. In general, you go through a bunch of conditions and you have to match one. Most of the chained if/else statements are checking the same thing - in this case, the avg variable. You might have conditions that are mutually exclusive and check different things, e.g., isUserAdmin else if isSystemInSpecialMode else if wasThereError else if isItTheSecondThursday - checking different things. This is more rare. The case OP had is bound to have come up before as a question. Maybe even for grades.
    – VLAZ
    Commented Aug 16, 2019 at 7:25
  • 4
    Yeah..this is one of those questions that grates... I know there are duplicates, but finding them is putting in search effort that the OP should have done. I usually just downvote them. BTW, go with the map - you can load it from a file so that the limits can be changed without a rebuild:) Commented Aug 16, 2019 at 8:15
  • @MartinJames (off-topic) hm... how would you deserialise a predicate from a file?
    – Andrew
    Commented Aug 16, 2019 at 8:19
  • 9
    When the question was closed, it had a different text than what you have quoted here. This was your edit after it was closed. It was pretty unclear before: "I would need to match the average score to a letter grade. That means if 90 < avg < 100 return 'A' and so on until 'F', with 5 if-else statements. That's a lot of repetition, and the ranges I'm matching to are of the same length. Is there a more efficient way to do this? Thanks in advance"
    – adiga
    Commented Aug 16, 2019 at 8:29
  • @AndrewTobilko for an assignment, 'name=value' text would do. In reality, it would depend on the database encryption/certification/verification used, since without that the file would be hacked immediate by the students:) Commented Aug 16, 2019 at 8:45
  • @adiga if you compare two edits, you'll see that there is no semantic difference. I just formatted it as code (again, that's what OP found difficult).
    – Andrew
    Commented Aug 16, 2019 at 8:56
  • 11
    (1) This question is and was not unclear at all. (2) Wether it is too broad is debatable. (3) The best way to handle this would've been to search out the best fitting dupe (there has to be one). (4) Downvoting the question just because it's discussed on Meta is unfair to the OP. Remember: Voting on the Main site works different than here. Commented Aug 16, 2019 at 9:30
  • 2
    @georgeStocker one or two downvotes would indicate that the question is bad. 12 downvotes is rather a we don't want you here sign, without Meta getting involved that only happens to spam / rude posts. And the help center might say it, but de facto it works differently. Thats just how it is. Commented Aug 17, 2019 at 10:16
  • 1
    Well, the two linked questions for duplication do not have any answer like the one of the * duplicated * question. One have a solution with only if / else which is exactly what the OP wanted to avoid. The other doesn't even have an accepted answer. And finally none of the answers uses Map and Predicate. Commented Aug 19, 2019 at 7:19
  • 4
    @ArnaudClaudel No wonder SO looks unwelcoming to newcomers. We heavily downvoted it, we closed it, we deleted it (again), we attached some (not really related or helpful) links to justify ourselves, and we moved on leaving the OP wondering what they've done wrong and what they should have corrected. "Try harder next time, kid."
    – Andrew
    Commented Aug 19, 2019 at 7:34

3 Answers 3

16

I'll willingly accept that the question shouldn't have been closed because it was unclear or because it was too broad.

It definitely shouldn't have been deleted that quickly if we want to give the OP a chance at redemption, although I recognize a few people in that deletion list who don't view questions like this that way.

If nothing else, it should be closed because it is a duplicate.

This question has been rehashed in Java several times over. The long-and-short of it is that the OP is going to have to write that long-ol' if statement (in older Java versions), but there are ways to make it neater.

6
  • 2
    If it has been asked so many time, what novel terms this new question is offering that would not deserve deletion anyways after it was closed?
    – Braiam
    Commented Aug 16, 2019 at 15:35
  • 1
    @Braiam: A sign post for a while until we can get search fixed. I'd rather make this the company's problem more than mine.
    – Makoto
    Commented Aug 16, 2019 at 15:42
  • 5
    Achievement Unlocked: "Shoggy Memory" -> Able to recall the precise location of obscurely titled artifacts immediately upon need. Based on observing Shog9's ability to remember every post on any meta site ever written. (those were good duplicate links)
    – user50049
    Commented Aug 16, 2019 at 18:02
  • 1
    Well, you said "This question has been rehashed in Java several times over", do we need several variations of the same question marked as duplicate?
    – Braiam
    Commented Aug 17, 2019 at 2:44
  • @Braiam: Hey - until search gets fixed, someone at some level is comfortable with it...
    – Makoto
    Commented Aug 17, 2019 at 4:25
  • Signposts would be worthwhile even with search being dramatically improved. Sometimes there are just radically different but equally valid ways to phrase the same question. If nothing else, they provide meta information about which questions on the site are the most frequently asked. Commented Dec 26, 2023 at 17:55
6

At the time the question was closed and deleted, this was its content

Is there a more efficient way to write multiple if else?

I would need to match the average score to a letter grade. That means if 90 < avg < 100 return 'A' and so on until 'F', with 5 if-else statements. That's a lot of repetition, and the ranges I'm matching to are of the same length. Is there a more efficient way to do this? Thanks in advance :)

To me, this is one of those "What does efficient mean?" questions. To get rid of the repetition they mention? It can't be that, the verbose and overengineered accepted answer and some of the others are just as verbose and repetitive in other ways. Somehow more performant? I don't this so either, none of the answers are demonstrably more performant.

Here as some quotes from the answers:

  • I like the way the grading system is defined
  • I like to use an enum for this kind of problem.
  • You could do it like this example [with an explanation of how it works, but not why it's better or more efficient]
  • Just to show off [...] the new Switch Expressions [...] very flexible [...]
  • [...] same thing written in a functional style. There is no repetition, but it's quite verbose

None of the answers these quotes were taken from explain why their solution is more efficient for whatever definition of "efficient" (or trendy) they chose to base their opinion on.

At this point, the question could be closed as both unclear or primarily opinion based.

When you Google the question's title, you find lots of similarly titled questions, most about completely unrelated use cases. I don't see how adding one more makes this useful to anyone. (Maybe someone can favorite the post and let us all know if the votes/view count changes in a year or two.)

You could change the title, but then you would just end up with the duplicates. I don't see any value in keeping this post around and that's why I voted to close and delete.

After the edit, the OP clarified to say they don't want to use multiple if statements. It's more clear what they want, but not why they landed on that choice to solve the problem (XY?). Probably better suited for code review, I dislike it for Stack Overflow.

3
  • 5
    "At the time the question was closed and deleted, this was its content" =>That's an inaccurate statement: Between the time it was put on-hold and the time it was deleted, it was made into the version you see now (see my screenshot, below)./
    – George Stocker Mod
    Commented Aug 16, 2019 at 20:57
  • 3
    @george Fair enough, an in-line snippet was moved to a code block by someone that is not the OP. Commented Aug 16, 2019 at 21:10
  • 6
    To be perfectly frank, the decision to vote to delete a question after such a short period of time was a bad decision. OP''s deserve a chance to fix bad questions after asking. Commented Aug 17, 2019 at 1:01
-8

This is one of those situations where an unwritten rule of Stack Overflow was broken.

Lack of Effort has never been a reason to close, but several of the commenters alluded to that and one said: "Show us the code".

It was then edited a few times and closed and deleted within two hours.

enter image description here

The deleters were on a hair-trigger with this one, and it did not give the OP or anyone else a real chance to make it better. The question in its current form is an acceptable question for Stack Overflow.

People may decide to downvote it for the "lack of research effort" (sigh); but they shouldn't close it for that reason. It's a useful, well scoped question. I think the title could use some work, so I'll see to trying to edit it, but otherwise it's a good question and should be opened.

16
  • 13
    "Useful" is pretty debatable...if we have a few dozen questions like that floating around, how much use is one more of them, honestly?
    – Makoto
    Commented Aug 16, 2019 at 14:03
  • 8
    @Makoto to its asker its quite useful; and closing it as a duplicate means they get their answer and others who stumble upon it find the right answer. Closing it as "unclear what you're asking" and deleting it helps no one.
    – George Stocker Mod
    Commented Aug 16, 2019 at 14:06
  • 15
    That's the principle of immediate gratification. To the asker, it's immediately useful because they are looking for an answer now. To the rest of the site, it's less useful because we've seen this before, and the only real way to give that signal to askers like this one to search before they ask is to close questions as duplicates. Immediate gratification from the OP's perspective is just a bandage over the lack of search so that OPs in this position can be immediately gratified in finding an answer to their question.
    – Makoto
    Commented Aug 16, 2019 at 14:09
  • 3
    Yes, the way to improve our reputation is to be stand-offish to outsiders.
    – George Stocker Mod
    Commented Aug 16, 2019 at 14:10
  • 15
    I fail to see how any of what I've said was stand-offish. Sure, the actions taken by others for this question weren't the best - especially if that's what the site wants to emphasize - but they're only stand-offish because we wouldn't have done the leg work to satisfy the OP's request for help on the site. I see both perspectives which is why I elected to close it as a dupe instead of wading into a debate about whether or not it should or should not remain open. Is closing it as a dupe still stand-offish, I wonder?
    – Makoto
    Commented Aug 16, 2019 at 14:14
  • 6
    @Makoto You really helped the person asking. Not only did they get great answers, they learned some more about how to dig. If your interaction there was all that happened, it would have been a fantastic experience for the OP and it's actually not a badly-written question to help catch more duplicates by different phrasing in the future. What is stand-offish is everything that led up to that, the whole experience of being discussed like you're not even there, closing, re-opening, deleting, undeleting .. that's what I think George is speaking to.
    – user50049
    Commented Aug 16, 2019 at 18:06
  • 1
    @TimPost Spot on; if it's not clear (and this is partly why comments aren't great for discussion); my comments are cumulative on each other. Premise: "Not Useful", my answer: "The asker found it useful. Closing as "unclear what you're asking" and deleting it is not useful to the OP. "rebuttal": "That's the principle of immediate gratificaiton" -> Me: We want people to have an overall positive experience on our site; we will drive people away if we don't do things that improve the interactions. Closing as a duplicate and showing them the answer is a positive thing for both parties.*
    – George Stocker Mod
    Commented Aug 16, 2019 at 18:39
  • 1
    *as positive as 'closing' can be.
    – George Stocker Mod
    Commented Aug 16, 2019 at 18:39
  • 8
    This answer reads like "These damn closers, always closing things. And they deleted it! Maybe it should have been downvoted (but I'll be disappointed). It's a good question." only in the comments did you allow that maybe closing it as a dupe would be a good thing (and I see the comments didn't make it back into the answer, so apparently they are not worthy of being kept, or you're working on it). Perhaps you can see why that might ruffle a few feathers? Commented Aug 16, 2019 at 20:57
  • @HereticMonkey At the time I wrote my answer it had not yet been closed as a duplicate. It wouldn't be closed as a duplicate for another twelve minutes by Makoto. Of course my answer couldn't reference something that hadn't happened yet.
    – George Stocker Mod
    Commented Aug 16, 2019 at 20:59
  • 7
    I'm afraid you misunderstood that part of my comment. I meant, the content of this comment, noting that closing as a duplicate is a positive thing, could be incorporated into the answer "ex post facto", as we are meant to do with comments that provide clarifying information, if I'm not mistaken. Commented Aug 16, 2019 at 21:04
  • 1
    Whenever I look at the list of posts with recent delete votes, I see many are recently closed questions. I conclude there is an organized campaign to delete recent bad questions. I'm not entirely sure this is a good thing.
    – Raedwald
    Commented Aug 17, 2019 at 16:58
  • 2
    ...imagine you're a 20Ker looking for questions to answer in your tag and you come by a low quality queston at -3 or lower. Prior to experiment it would likely hang open, maybe with 1-2 close votes - so you'd simply move on (maybe casting a vote down / close). As of now, it is likely closed (or has 2 votes already so that you can close it). That makes it possible for you to cast a delete vote, why not
    – gnat
    Commented Aug 19, 2019 at 6:05
  • 1
    I think gnat has hit the problem on its head: “on-hold” questions with negative scores still need time to be fixed. No one can reasonably argue deleting it that fast gave the OP time to fix it. At that point it’d vanish from them unless they could find it in their browser history.
    – George Stocker Mod
    Commented Aug 19, 2019 at 10:34
  • 2
    @GeorgeStocker last time I checked "recent deleted questions" gave users links to these, no need to mess with browser history. And another thing I checked (with my own deleted question) is one can edit these and flag for undeletion
    – gnat
    Commented Aug 19, 2019 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .