[Java] Really scary switch statement story

Introduction

A long time ago, I summarized the story when I messed up with a switch statement. It's a very embarrassing story, but I've put together an article to prevent a similar accident from happening again.

1. Initial state

TestServlet.java


@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//Request parameter"id"Get the value of.
		int id = Integer.parseInt(request.getParameter("id"));

		// "id"Processes are distributed based on the value of.
		switch(id) {
		case 1:
			//Process 1-A
			break;
		case 2:
			//Process 2-A
			break;
		case 3:
			//Process 3-A
			break;
		default:
			//Default processing
			break;
		}
		...
	}
}

2. Renovation due to additional functions

TestServlet.java


@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//Request parameter"id"Get the value of.
		int id = Integer.parseInt(request.getParameter("id"));

		// "id"Processes are distributed based on the value of.
		switch(id) {
		case 1:
			//Process 1-A
			//Process 1-B
			//Process 1-C
			break;
		case 2:
			//Process 2-A
			//Process 2-B
			//Process 3-C (★ I erased the break immediately after this...)
		case 3:
			//Process 3-A
			//Process 3-B
			//Process 3-C
			break;
		default:
			//Default processing
			break;
		}
		...
	}
}

3. Accident occurred

Parameter id value Expected processing result Actual processing result
1 X X
2 Y Not Y
3 Z Z
Other α α

4. Reason for the accident

5. What to do after the accident

Reference URL

Recommended Posts

[Java] Really scary switch statement story
Java switch statement
switch statement
[Java] Branch enum with switch statement
Java, if statement / switch statement starting from beginner
Basics of java basics ② ~ if statement and switch statement ~
Java static story
Really scary ClassCastException
Java initializer story
Java for statement
Java generic story
Java switch statement and break, Kotlin when expression ...
Java instruction execution statement
[Java] for statement / extended for statement
Switch java with direnv
Switch statement range specification
(Memo) Java for statement
Lombok's Java 9+ support story
Studying Java-Part 11-switch statement
[Java] Aizu Online Judge's story 2
[Java] Aizu Online Judge's story 1
[Swift] switch statement using tuples
Java if and switch statements
Java programming basics practice-for statement
☾ Java / Iterative statement and iterative control statement
Java programming basics practice-switch statement
Let's understand the switch statement!
Seasonal display with Java switch
C # and Java Overrides Story
A story that I finally understood Java for statement as a non-engineer