Refactor #6759

Improve internal option handling pattern

Added by Markus Roberts about 1 year ago. Updated 12 days ago.

Status:Closed Start date:03/17/2011
Priority:Normal Due date:
Assignee:Daniel Pittman % Done:

0%

Category:plumbing
Target version:3.0.0
Affected Puppet version: Branch:
Keywords:
Votes: 0

Description

The code base contains many occurrences of the pattern:

      def initialize(..., options = {:op1 => 'default1'})
         options.each { |opt, val| send(opt.to_s + "=", val) } 
         :

This is problematic for several reasons; it should be replaced with something like:

      def initialize(..., options = {})
         options = {
            :opt1 => 'default',
            :opt2 => 'default2',
            :
          }.update options
         @opt1 = options.delete(:opt1)
         @opt2 = options.delete(:opt2)
         :
         raise "Unknown options: #{options.inspect}" unless options.empty?
         :

If inheritance is an issue this can be augmented to:

(in the base)

      def initialize(..., options = {})
         parse_options({
            :opt1 => 'default',
            :opt2 => 'default2',
            :
          }.update options)
         :

      def parse_options(options)
         @opt1 = options.delete(:opt1)
         @opt2 = options.delete(:opt2)
         :
         raise "Unknown options: #{options.inspect}" unless options.empty?
         :

(in children)

      def initialize(..., options = {})
         super(...,{
            :opt3 => 'default3',
            :opt4 => 'default4',
            :
          }.update options)
         :

      def parse_options(options)
        @opt3 = options.delete(:opt3)
        @opt4 = options.delete(:opt4)
        :
        super(options)
      end

History

Updated by Daniel Pittman about 1 year ago

This looks like a net loss to me: we have to open-code all the option handling. Can’t we delegate this to one of the option parsing libraries or something, and use those to allow us to do the right thing?

Updated by Luke Kanies about 1 year ago

I agree with Daniel – I agree that the current system is non-optimal, but I think your recommendation will miss problems caused by providing invalid options and will also require a lot of extra code for every method.

I’d prefer to have a clean library that we used to provide consistent behavior. I tried to do this with the Puppet::Util::MethodHelper module, but it ended up being uglier to use it than to just use the one-liner you’re decrying. My mistake was not trimming down the module and then using it consistently.

Updated by Ben Hughes 4 months ago

  • Description updated (diff)
  • Status changed from Accepted to Unreviewed

Updated by Devon Peters 4 months ago

  • Description updated (diff)
  • Category set to plumbing
  • Status changed from Unreviewed to Needs Decision
  • Assignee set to Daniel Pittman
  • Target version set to 3.X

Updated by Daniel Pittman 4 months ago

  • Status changed from Needs Decision to Closed

It isn’t that this is a bad plan, or isn’t valuable, but it isn’t anything of sufficient priority that we need an open ticket tracking it at this stage, for a purely internal bit of work.

Updated by Daniel Pittman 12 days ago

  • Target version changed from 3.X to 3.0.0

Also available in: Atom PDF