in reply to Re^14: Assignable Subroutines
in thread Assignable Subroutines
For now, I'll provide two other ways to accomplish what you want, through functional programming or just using Perl itself . . .
Both examples require more functionality in the code higher up. It's not OO due to insufficient encapsulation.
Again, if you don't want to use OO, that's fine, as there are plenty of places where OO is inappropriate. But if you are going to use it, do it right.
This is completely wrong. Now the object designer has to anticipate all of the infinte set of incorrect things that people might try to do with his object and explicitly make each one an error.
No, you don't:
package Employee; . . . sub apply_effect { my ($self, $effects) = @_; # $effects->list returns a list of objects containing # the singular effects foreach my $effect ( $effects->list ) { my $attribute = $effect->attribute; throw_exception() unless exists $self->{$attribute}; # Rest of code } }
Essentially a "deny by default" strategy. Now your test suite just needs to throw a few effects that are known to have attributes it's not going to use, check that exceptions were thrown, and you're set.
It really seems to me that in your game, buildings should just ignore the speed message. But even if your RTS engine is a special case where all sorts of things should ignore all sorts of messages then maybe you're right to do what you're doing but that's still not an argument as to why software in general should do it too.
The important point is that it's going to depend on the application. I was making a general framework, and in certain games it's going to make sense to ignore, while others might want to throw an error. It's none of my businesses to impose that on the game designer. Further, this strategy may be useful outside games, and I'd rather have a general framework that is easily modified rather than writing from scratch.
I can see the argument for using (improved) lvalue subs for internal use (as dragonchild mentioned higher up the thread). But rarely/never outside the class.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^16: Assignable Subroutines
by fergal (Chaplain) on Jan 27, 2005 at 21:10 UTC | |
by hardburn (Abbot) on Jan 27, 2005 at 21:19 UTC | |
by fergal (Chaplain) on Jan 27, 2005 at 22:10 UTC | |
by hardburn (Abbot) on Jan 27, 2005 at 22:30 UTC | |
by fergal (Chaplain) on Jan 27, 2005 at 23:32 UTC |