티스토리 뷰

대분류 - Struts Frameworks [STRUTS]

규칙명 - Do not declare instance variables in Struts Action classes [STRUTS.INST-3]

- 스트럿츠의 액션클래스에서 인스턴스 변수를 만들지 말라는 이야기이다.

- 왜냐하면, 스트럿츠의 액션클래스는 singleton 이기 때문에 당연히 다중 쓰레드에 대해 안전해야 한다.(thread-safe 해야 한다는 말이죠) 만약, 액션클래스에 인스턴스 변수를 선언하게 되면 해당 인스턴스 변수는 멀티쓰레드가 서로 공유하기 때문에 호출시마다 서로 다른값이 반환될 수 있다. 즉, 결과값을 보장할 수 없다는 뜻이다.
- 또한 인스턴스 변수를 가지지 말아야하는 것 이외에도 스트럿츠의 액션클래스를 임의로 new 를 통해 생성해서도 안된다. (당연한건가?) 하지만, 일반적으로 이런 내용을 무시하고(또는 몰라서) 유틸리티 클래스처럼 사용하거나 또는 중복되는 로컬변수를 없애는 목적으로 인스턴스 변수를 남발하는 경우가 있는게 현실이다. 기본은 기본으로써 존중되어야 하는데 말이다...

DESCRIPTION

This rule identifies instance variables in Action classes. By default, an error is reported for any non-static member variable of any class derived from Action. Select instance
variables can be allowed by specifying them in the parameter list.

본 규칙(STRUTS.INST-3) 은 액션클래스 내에 인스턴스변수의 존재여부를 식별한다. 만약 액션클래스를 상속받은 클래스가 non-static 멤버 변수등을 사용할 경우 표시해 주며, PARAMETER 에 설정함으로써 특정 인스턴스 변수의 사용여부를 변경할 수도 있다.

PARAMETERS

-Allow instance variables with the following names
-This parameter allows you to specify the names of instance variables which should be allowed in Action classes. The default is an empty list (in other words, the default is that no instance variables are allowed).

JTest 에서 "View/Change Rule Parameter" 를 통해서 수정할 수 있는데, 주어진 이름과 매핑되는 인스턴스 변수를 제외하고 검사할 수 있다. 기본값은 없다.

BENEFITS

Avoiding instance variables in Action classes helps to make code thread-safe. Action classes are singletons, and there may be several threads executing an Action class instance.  For this reason, there should be no instance variables in Action classes. It is important to use only local variables (rather than instance variables) in Action classes.

위에서 설명한 것 처럼 thread-safe 한 코드를 위해서 인스턴스 변수를 사용하면 안된다. 왜냐면, 액션클래스는 singleton 으로 구현되어 있어서 멀티 쓰레드를 처리할 수 있기 때문으로, 이렇기 때문에 액션클래스 내에서는 절대로 인스턴스 변수를 사용하면 안되고, 반드시 로컬변수만을 사용해야 한다.

EXAMPLE

package examples.rules.STRUTS;

import org.apache.struts.action.Action;

public final class INST extends Action {
    int foo;  //VIOLATION- instance variable in an Action class
}

org.apache.struts.action.Action 을 상속했기 때문에.. 문제가 된다.

REPAIR

Remove all instance variables, and implement the code using local variables instead.

package examples.rules.STRUTS;

import org.apache.struts.action.Action;

public final class INST extends Action {
    //FIXED
}

사용하지 않는것이 맞다.


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함