Syntactic sugar to encourage use of named arguments

@tmk, I’m not 100% clear which thing you’re looking for in an example, but I think you mean “variable lifting” in Ruby? Let me know if I’ve misunderstood.
I don’t have any FOSS projects, unfortunately, but I’ll provide a sanitized example from my closed-source work.

example from a real (closed source) Ruby codebase

This is part of menu building for a static site generator.
I’ve tried to remove any $COMPANY-specific details.
Feel free to DM critiques of my Ruby to me, but not in this already-long thread.

def render_menu(
  item_descriptors, depth: 0, maxdepth: 1, collapsible: false
)
  # render each item in the menu
  rendered_items = item_descriptors.map do |item_desc|
    # do we have subsections to render or not? (only if there is depth left)
    has_subsections = (
      (maxdepth - depth).positive? &&
      item_desc[:subsections] && item_desc[:subsections].length.positive?
    )

    # create the link to the current item
    rendered_item = link_to(
      item_desc[:item][:short_title], relative_path_to(item_desc[:item])
    )           
                
    # combine with the subsections if there are any to render
    if has_subsections
      rendered_item = html_tag(
        'div',
        %W[   
          #{rendered_item} 
          <button class="caret"
          aria-label="expand/collapse #{item_desc[:item][:short_title]} submenu">
          <span class="caret"></span>
          </button>
        ].join(' '),
        { 
          class: 'sidebar-heading',
          role: 'navigation',
          'aria-haspopup': 'true',
          'aria-expanded': 'false'
        }
      )
      rendered_item += render_menu(
        item_desc[:subsections],
        depth: depth + 1,
        maxdepth:,
        collapsible: true
      )

      html_tag('li', rendered_item, class: 'sidebar-submenu')
    else
      html_tag('li', rendered_item, class: 'sidebar-leaf-item')
    end
  end.join
    
  tree_class = "sidebar-tree sidebar-tree-l#{depth}" + (
    collapsible ? ' sidebar-tree-collapsible' : ''
  )
  html_tag('ul', rendered_items, class: tree_class)
end

Note how that use of maxdepth:, does very little to simplify the code or meaningfully improve readability.
My take is that the feature isn’t really bad, but I’m not seeing a lot of benefit from it and I hate that Rubocop more or less strong-arms me into writing it this way.

I’m trying to tread a fine line and avoid spreading any irrational fear about the feature while still giving voice to my misgivings.
If the goal of the feature is to improve readability, then it needs to decide whether or not

render_menu(
    item_desc.subsections,
    depth=depth + 1,
    maxdepth=,
    collapsible=True
)

is a readability improvement.

I would appreciate a few examples, in any PEP for this, which show when it is not appropriate. I think that would be enough to guide people away from overzealously trying to enforce this usage on everyone.

6 Likes