Skip to content

Current Refactorings

angrycaptain19 edited this page Jan 7, 2021 · 14 revisions

These are the refactorings currently available in Sourcery. If you hover over a Sourcery refactoring proposal in your IDE or look at a Sourcery comment in GitHub you can see the Sourcery Refactoring ID for all of the refactorings being proposed in that change. If you want to disable any of the refactorings in a specific function add in the comment #sourcery-skip followed by the refactoring id or to skip it across your whole project add it into the refactor skips section of your Sourcery config file .

If you have any questions about any of these refactorings or have suggestions for new refactorings please [reach out to us](mailto: hello@sourcery.ai).

Augmented assign
Sourcery refactoring id: aug-assign
Description: Replaces assignments with augmented assignments

Assign if expression
Sourcery refactoring id: assign-if-exp
Description: Replaces multiple if or else statements with an if expression

Binary operator identity
Sourcery refactoring: bin-op-identity
Description: Replaces binary ops between a value and itself with known identities:

  • x | x => x
  • x & x => x
  • x ^ x => 0
  • x - x => 0
  • x / x => 1
  • x // x => 1
  • x % x => 0

Boolean if expression identity
Sourcery refactoring id: boolean-if-exp-identity
Description: Simplifies boolean if expressions by removing unnecessary explicit references to True or False states

Collection to comprehension
Sourcery refactoring id: collection-builtin-to-comprehension
Description: Calling function accepts any iterable for a generator can be passed directly instead of a list comprehension

Comprehension to generator
Sourcery refactoring id: comprehension-to-generator
Description: Replaces comprehensions with generators

Convert any to in
Sourcery refactoring id: convert-any-to-in
Description: Converts any() functions to simpler in statements

Convert to enumerate
Sourcery refactoring id: convert-to-enumerate
Description: Replaces iterative counters with enumerate functions

Default get
Sourcery refactoring id: default-get
Description: Replaces conditional looks to retrieve a value from a dictionary with the get method

Del comprehension
Sourcery refactoring id: del-comprehension
Description: Replaces cases where deletions are made via loops with comprehensions

De Morgan Identity
Sourcery refactoring id: de-morgan
Description: Simplifies conditional logic using De Morgan's Identity

Dictionary comprehension
Sourcery refactoring id: dict-comprehension
Description: Replaces dictionaries created with loops with dictionary comprehensions

Dictionary literal
Sourcery refactoring id: dict-literal
Description: Replaces dictionaries created with 'dict()' with '{}'

Ensure file closed
Sourcery refactoring id: ensure-file-closed
Description: Replaces explicit file open, file close cases with 'while open' to ensure that the file will close

Extract duplicate method
Sourcery refactoring id: extract-duplicate-method
Description: Identifies duplicate sections of code in a function and extracts these into their own method
Requires Sourcery Pro

Extract method
Sourcery refactoring id: extract-method
Description: Extracts complex pieces of functions into their own methods

Flip comparison
Sourcery refactoring id: flip-comparison
Description: Moves variables from the right side to the left side of comparisons

Guard
Sourcery refactoring id: guard
Description: Adds in guard clause to a conditional

Hoist if out of if
Sourcery refactoring id: hoist-if-from-if
Description: Moves if statements that occur in all cases of an if statement outside of the conditional

Hoist loops out of if
Sourcery refactoring id: hoist-loop-from-if
Description: Moves loops that occur in all cases of an if statement outside of the conditional

Hoist statements out of if
Sourcery refactoring id: hoist-statement-from-if
Description: Moves statements that occur in all cases of an if statement outside of the conditional

Hoist statement out of loop
Sourcery refctoring id: hoist-statement-from-loop
Description: Moves statements that are constant across all cases of a loop outside of the loop

Inline immediately returned variables
Sourcery refactoring id: inline-immediately-returned-variable
Description: Moves a variable inline to a return in the case when the variable being declared is immediately returned without being otherwise used or manipulated

Index range replacement
Sourcery refactoring id: for-index-replacement
Description: Replace item lookups in loops using the index with direct reference to the items

Invert any & all
Sourcery refactoring id: invert-any-all
Description: Switches not any or not all statements to all or any statements respectively

Invert any & all in body
Sourcery refactoring id: invert-any-all-body
Description: Switches any or all statements to not all or not any statements respectively when there is a not in the body of the initial statements

Last if statement guard
Sourcery refactoring id: last-if-guard
Description: Convert the final conditional into a guard clause

Lift duplicated conditional
Sourcery refactoring id: lift-duplicated-conditional
Description: Lift an if statement out of another if statement when they both have the same condition

List comprehension
Sourcery refactoring id: list-comprehension
Description: Replaces lists created with loops with list comprehensions

List literal
Sourcery refactoring id: list-literal
Description: Replaces lists created with 'list()' with '[]'

Merge comparisons
Sourcery refactoring id: merge-comparisons
Description: Consolidates multiple comparisons into a single comparison

Merge dictionary assign
Sourcery refactoring id: merge-dict-assign
Description: Merges the creation of a dictionary and immediately adding items to it when an empty dictionary is created and items are immediately added

Merge duplicate blocks
Sourcery refactoring id: merge-duplicate-blocks
Description: Restructure conditional to remove duplicate blocks of code within the conditional

Merge is instance
Sourcery refactoring id: merge-is-instance
Description: Combines together multiple isisntance functions

Merge list append
Sourcery refactoring id: merge-list-append
Description: Merges the creation of a list and appending items to it when an empty list is created and items are immediately added via an append

Merge list extend
Sourcery refactoring id: merge-list-extend
Description: Merges the creation of a list and extending to add items to it when an empty list is created and items are immediately added via an extend

Merge nested if statements
Sourcery refactoring id: merge-nested-ifs
Description: Merges together multiple nested if conditions into one

Merge repeated ifs
Sourcery refactoring id: merge-repeated-ifs
Description: Merges together the interior contents of if statements with identical conditions

Merge set add
Sourcery refactoring id: merge-set-add
Description: Merges the creation of a set and immediately adding items to it when an empty set is created and items are immediately added

Min/max identity
Sourcery refactoring id: min-max-identity
Description: Replaces duplicate conditionals looking for the minimum or maximum value of multiple variables with a min or max function

Move assign
Sourcery refactoring id: move-assign
Description: Moves assignment of variables closer to their usage

None compare
Sourcery refactoring id: none-compare
Description: Replaces '==' in comparisons for none values with 'is'

Order statements
Sourcery refactoring id: order-statements
Description: Reorders statements within a function so that variables are used as close to their declaration as possible

Or if experession identity
Sourcery refactoring id: or-if-exp-identity
Description: Replaces conditionals with identity checks with an if expression using or

Remove dictionary keys
Sourcery refactoring id: remove-dict-keys
Description: Removes '.keys()' from any 'in dictionary' statements

Remove pass from body
Sourcery refactoring id: remove-pass-body
Description: Removes unnecessary passes from the body of a conditional

Remove pass from elif
Sourcery refactoring id: remove-pass-elif
Description: Removes unnecessary passes from the elif section of a conditional

Remove redundant exception handlers
Sourcery refactoring id: remove-redundant-except-handler
Description: Removes exception handlers for exceptions previously covered in the function

Remove redundant if statements
Sourcery refactoring id: remove-redundant-if
Description: Removes if statements where the conditional is always valid

Remove redundant pass
Sourcery refactoring id: remove-redundant-pass
Description: Removes passes from functions with other components or where the pass would never be read

Remove unreachable code
Sourcery refactoring id: remove-unreachable-code
Description: Removes code that will never be executed in a function

Remove zero from range
Sourcery refactoring id: remove-zero-from-range
Description: Removes unnecessary explicit definitions of 0 from ranges

Replace index with underscore
Sourcery refactoring id: for-index-underscore
Description: Replaces a named index in a for loop with an underscore

Replace while with for
Sourcery refactoring id: while-to-for
Description: Replaces while loops with a counter to a for for loop

Return identity
Sourcery refactoring id: return-identity
Description: Removes unnecessary explicit calls to return True or False

Set comprehension
Sourcery refactoring id: set-comprehension
Description: Replaces sets created with loops with set comprehensions

Simplify boolean comparison
Sourcery refactoring id: simplify-boolean-comparison
Description: Removes unnecessarily verbose boolean comparisons

Simplify generator
Sourcery refactoring id: simplify-generator
Description: A identify generator (a for a in coll) can be replaced directly with the collection coll

Simplify length comparison
Sourcery refactoring id: simplify-len-comparison
Description: Removes unnecessarily verbose length comparisons

Simplify negative index
Sourcery refactoring id: simplify-negative-index
Description: Replaces a[len(a)-1] with negative index lookup a[-1]

Simplify numeric comparison
Sourcery refactoring id: simplify-numeric-comparison
Description: Consolidates any mathematical operations in a numeric comparison so there is a direct comparison of variable to number

Split or ifs
Sourcery refactoring id: split-or-ifs
Description: Splits out conditions combined with an or in an if statement into their own if statement. N.B. This often makes the code worse but can be an intermediate step that enables other refactorings.

Square identity
Sourcery refactoring id: square-identity
Description: Replaces cases of a variable being multiplied by itself with squaring that variable

Sum comprehension
Sourcery refactoring id: sum-comprehension
Description: Replaces summed values created with loops with sum comprehensions

Swap if & else branches
Sourcery refactoring id: swap-if-else-branches
Description: Swamps if and else branches of conditional sections of a function to reduce complexity

Swap nested ifs
Sourcery refactoring id: swap-nested-ifs
Description: Swaps the order of nested if statements to reduce the complexity of a function

Switch
Sourcery refactoring id: switch
Description: Switches the order of multi-step conditionals to minimise complexity

Use assigned variable
Sourcery refactoring id: use-assigned-variable
Description: Uses a variable that was previously defined in the function instead of repeating what was defined in the variable

Use any
Sourcery refactoring id: use-any
Description: Replaces conditional checks if an item exists in a list with the any function

Use count
Sourcery refactoring id: use-count
Description: Replaces 'sum()' functions in conditionals with 'count()' functions where appropriate

Use dictionary items
Sourcery refactoring id: use-dict-items
Description: Use dictionary.items() in for loop to access both key and value at same time

Use len()
Sourcery refactoring id: use-len
Description: Replaces 'sum()' functions in conditionals with 'len' functions where appropriate

Yield from
Sourcery refactoring id: yield-from
Description: Replaces yields as part of for loops with a "yield from"

Clone this wiki locally