As I always understood it, any change to the programs state (or anything to do with IO) is a side effect. It does not matter, whether the change occurs in a global variable or in a private field of the object the method is called on. It follows that all methods which do not return anything either do nothing at all or have a side effect.
My confusion comes from one of our university's instructors (who is still a student and thus not omniscient yet;) ) telling me setters don't have side effects.
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- Name for a method that has only side effects
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- NameError: name 'self' is not defined, eve
- Is there an existing solution for these particular
- Implementation Strategies for Object Orientation
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
Your instructor is mistaken. With apologies to the SO editors for not pasting the entire article here, this is what Wikipedia has to say:
http://en.wikipedia.org/wiki/Side_effect_(computer_science)
Money Quote #1:
Money Quote #2:
Non-NOP Setters always satisfy that criteria.
Getters and setters are just syntactic sugar for get_ and set_ methods. They can absolutely have side effects (though it's probably a bad idea to start tweaking lots of fields when all the caller wanted was to increment a counter or something).
First of all: I am aware of the language agnostic tag! "runako" answered the question quite correctly. But often you want to apply your knowledge to the real world so I think I would be nice to also provide an answer that addresses this problem in a more pragmatic way.
When dealing with real world languages like c++,c# or java then even a nop function has actual side effects which can cause code to be executed!
Just think about static-constructors. Even though the specs don't always specify the time a static constructor is ran for a class, most of the time it will be the point in time where a method or member of the class is first accessed.
Example in C#:
What's more is that even a method that isn't being called at all can cause side effects! Think of reflection (the ability of a program to query information about its own structure). When a method is present but not being called it can still be detected by reflection.
A method with no calls to it surely has a side effect on a program that outputs the number of methods inside!
It all boils down to this: If you want to know about the actual side effects of a method, you first have to determine what you even consider to be a "side effect".