18

Today's TDWTF article starts with a confession from the author:

I didn’t know what the For-Case anti-pattern was until relatively recently, when there were a spate of articles condemning it as an anti-pattern. I’m sure I’ve probably used it, at some point, but I never knew it by name. It’s thought of as a textbook antipattern that generally implies a misunderstanding of for loop, case statements, the problem being solved, or some combination of all three.

He then proceeds as if the reader, naturally, knows what the For-Case anti-pattern is without any further explanation being needed.

But I don't! I haven't seen the "spate of articles" that Remy talks about, and the only significant reference that I can find on Google (besides Remy's article) is a blog post by Raymond Chen about the for-if antipattern, which is apparently related. He doesn't define the "for-case anti-pattern" either, though.

What is this "For-Case anti-pattern" that these guys are talking about, and what makes it an anti-pattern?

4
  • 1
    thedailywtf.com/articles/The_FOR-CASE_paradigm Commented May 10, 2017 at 13:01
  • 3
    It's commonly known as (and easier to find via search engines) as the loop-switch anti-pattern Commented May 10, 2017 at 13:02
  • 1
    Strange that it is an anti-pattern. I see it, I just shake my head, and think why would anyone do that? How did this ever become a pattern? Commented Jun 16 at 21:22
  • @gnasher729 I tend to agree, but can imagine initially-reasonable code evolving to have this pattern through sloppy-but-not-insane changes. For instance, imagine a five-step data-wrangling tool where initially each step is to be run individually so it can be manually reviewed and tweaked before the next step. A CLI that takes a step number argument and passes it into a switch might be a fine implementation! Then the process matures; manual review is eliminated and all 5 steps just need to run in sequence automatically. Finally imagine a careless, rushed developer makes that change... Commented Jun 17 at 12:54

2 Answers 2

27

The "pattern" was introduced in an earlier Daily WTF article. The basic idea is that you have a for loop with a case inside of it that selects based on the for loop index variable, e.g.:

For Icnt = 0 To 5 Select Case Icnt Case 0 DoStuff() Case 1 DoOtherStuff() Case 2 DoMoreStuff() Case 3 DoDifferentStuff() Case 4 DoYetMoreStuff() Case 5 DoFinalStuff() End Select Next 

Assuming the index variable can't be changed inside the loop, (which is not always true, depending on which language you're using,) a bit of analysis demonstrates that the execution is exactly the same as if you removed the for and the case entirely and all the case blocks were simply executed sequentially:

DoStuff() DoOtherStuff() DoMoreStuff() DoDifferentStuff() DoYetMoreStuff() DoFinalStuff() 
2
  • Would it still be considered an anti-pattern if the item you iterate on is not constant but rather built ad runtime based on the application logic? Like, first building an array of "actions" to perform, identified by some ID string, than sorting them based on whatever logic, and finally running them by iterating on those strings? Commented Jun 30 at 7:34
  • 1
    @MatteoTassinari The antipattern is that both the for loop and the case switch can be removed without any change in the program's output. If that's not what's happening then it's not the for-case antipattern. Commented Jun 30 at 23:24
-3

To put it very simply, you're nesting two different flow control methods, which makes the code harder to understand.

If you see a for loop, you generally assume that loop conditions will control the logic within the loop. But if you then nest a whole case statement inside, with all of its own logic, that goes out the window.

3
  • 5
    There's nothing inherently wrong with nesting two control flow structures. Sometimes you need to. But in this case it's useless. Commented May 10, 2017 at 18:18
  • "which makes the code harder to understand." Boy, is that ever the understatement of the day/month/year. Commented May 14, 2017 at 0:15
  • 2
    This is not why the for-case antipattern is stupid. Commented Sep 3, 2020 at 10:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.