개요
In computer programming, a function, or method, is said to have a "side effect"
- if it modifies state outside its own context or
- has observable interactions with its calling functions or the outside world beyond returning a value.
사이드이펙트 사례
-
관련 없는 컨텍스트의 상태를 변경
단적인 예: 글로벌 스테이트의 변경
-
I/O 작업 (DB 접근, 네트워킹)
-
print(”hello”)
In contrast, a side effect might be something like
- modifying a global variable,
- performing I/O operations, or
- calling another method that has an unrelated effect, as these things extend beyond the scope of the single method.
사이드 이펙트로 분류되는 일반적인 기준
- 스코프(컨텍스트): 컨텍스트 바깥의 상태를 변경하는지
- 변경의 캡슐화 여부: 변경이 외부에서 관측 가능한지
In conclusion, whether mutating the state of an instance is classified as a side effect often depends on the scope and encapsulation of that change.