|
Posted by Chung Leong on 08/15/06 20:56
Simon Dean wrote:
> I mean, should you pass parameters into the function, or set the
> variable properties of the class? Return a value from a function, or set
> another variable?
>
> What are the advantages? Which way is correct?
<subliminal>use the stack... the stack is your friend... use the
stack</subliminal>
Setting properties in lieu of passing arguments is wrong. It makes your
function non-reentrant, meaning your function cannot be called again
until it has exited. That's especially problematically in multithreaded
code. Even in single-threaded code (as is a PHP script) you want to
avoid it: in a complicate application it's hard to guarantee that a
function won't end up calling itself.
<subliminal>side effects are evil...</subliminal>
For example, A::DoAThing() calls B::DoBThing() which in turns calls
C::DoCThing() and so on. In the midst of this, for some reason,
K::DoKThing() has to call A::DoAThing(). Now, if K::DoKThing() has to
modify the object in order to get something to A::DoAThing(), then when
we return to the beginning of the chain (i.e. when B::DoBThing()
returns), A::DoAThing() would no longer have the arguments that it had
started with originally.
<subliminal>scoping is good...</subliminal>
[Back to original message]
|