티스토리 뷰

Servlet JSP

JSP 스파게티 Spaghetti 코드

Programmers 2021. 4. 26. 19:11
반응형

스파게티 Spaghetti 코드란?
Spaghetti code is a pejorative phrase for unstructured and difficult-to-maintain source code. Spaghetti code can be caused by several factors, such as volatile project requirements, lack of programming style rules, and software engineers with insufficient ability or experience. Wikipedia

 

Spaghetti code - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Software source code with poor structure Spaghetti code is a pejorative phrase for unstructured and difficult-to-maintain source code. Spaghetti code can be caused by several factors,

en.wikipedia.org

영어보다 여기 캡처를 보면 바로 이해가 간다.

스파게티 코드 짤

홀수, 짝수를 구하는 코드 1.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<% int num = 0; %>
		   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%String num_ = request.getParameter("num");
		if(num_ != null && !num_.equals(""))
			num = Integer.parseInt(num_);%> 
			
	<% if(num%2 != 0){ %>
	홀수입니다.
	<% } else{ %>
	짝수입니다.<% } %>
</body>
</html>

홀수, 짝수를 구하는 코드 2.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    		   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% int num = 0;
   String num_ = request.getParameter("num");
		if(num_ != null && !num_.equals(""))
			num = Integer.parseInt(num_);%> 
			
	<% if(num%2 != 0){ %>
	홀수입니다.
	<% } else{ %>
	짝수입니다.<% } %>
</body>
</html>

홀수, 짝수를 구하는 코드 3.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    		   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<% int num = 0; %>
<body>

<%String num_ = request.getParameter("num");
		if(num_ != null && !num_.equals(""))
			num = Integer.parseInt(num_);%> 
			
	<% if(num%2 != 0){ %>
	홀수입니다.
	<% } else{ %>
	짝수입니다.<% } %>
</body>
</html>

위의 3 코드는 동일하다. 즉, 코드 블록이 어디에 삽입되던 문제가 없다.
하지만 코드가 길어진다면 엄청나게 읽기가 힘들어진다.
이게 스파게티 코드라고 한다.
절대 지양해야 한다.

반응형

'Servlet JSP' 카테고리의 다른 글

JSP MVC model2  (0) 2021.04.26
JSP MVC model1  (0) 2021.04.26
JSP로 서블릿 간단 출력 예제  (0) 2021.04.26
JSP 내장객체  (0) 2021.04.26
JSP 코드 블록  (0) 2021.04.26
댓글
공지사항