rope icon indicating copy to clipboard operation
rope copied to clipboard

Refactoring idea: extract sublist to method/variable refactoring from container literals/function arguments

Open lieryan opened this issue 3 years ago • 1 comments

Given, a collection literal like so:

actions = [
    '1. Walk Forward',
    '2. Evade',
    f'3. {attack_name}',
    '4. Flee',
]

or a function call:

action = env.user_input_choices(
    'Choose what to do:',
    '1. Walk Forward',
    '2. Evade',
    f'3. {attack_name}',
    '4. Flee',
)

or similar constructs with:

  1. [ ] list literal
  2. [ ] tuple literal
  3. [ ] dict literal
  4. [ ] set literal
  5. [ ] function call argument list

When I select a partial list like the following:

    '2. Evade',
    f'3. {attack_name}',

and perform the following actions:

  1. [ ] extract refactoring
  2. [ ] variable refactoring

Then, we should extract the sub-list/set/dict/tuple with the appropriate concatenation operator, like so:

extracted_var = [
    '2. Evade',
    f'3. {attack_name}',
]

actions = [
    '1. Walk Forward',
] + extracted_var + [
    '4. Flee',
]

or:

def extracted_function(attack_name):
    new_var = [
        '2. Evade',
        f'3. {attack_name}',
    ]

actions = [
    '1. Walk Forward',
    *extracted_function(attack_name),
    '4. Flee',
]

lieryan avatar Sep 19 '21 16:09 lieryan

Great idea :)

climbus avatar Sep 19 '21 17:09 climbus