in reply to passing subroutine args as a hash: why not?

I find them to be an atrosity. Forget the number of parameters, but what you are passing around.

If one key gets misspelled, it can create an ugly bug that is hard to find. Pass in your parameters like a normal hu-man. And if you can use OOP, dont' pass them in as hashes either. Create attributes where they make sense, pass in the rest.

  • Comment on Re: passing subroutine args as a hash: why not?

Replies are listed 'Best First'.
Re: Re: passing subroutine args as a hash: why not?
by djantzen (Priest) on Jun 05, 2003 at 19:50 UTC

    If one key gets misspelled, it can create an ugly bug that is hard to find.

    This is one of the best reasons to use named parameters! Within your subroutine you can match the names you received against a list of names you expected to receive and you know immediately what's missing. You can also wrap the arguments in a temporary parameter object that automatically does the argument checking for you.


    "The dead do not recognize context" -- Kai, Lexx
      So now you have to add logic to figure out what was misspelled in each subroutine. Talk about overhead.

      sub somethingComplicated() { my( $var1, $var2, $var3, $var4 ) = @_; }
      Calling this and screwing up the assignment to var3 via the function call is quite hard. It's called protecting the developer from shooting himself in the foot.