View Source Credo.Check.Design.DuplicatedCode (Credo v1.7.3)

This check has a base priority of higher and works with any version of Elixir.

Explanation

Code should not be copy-pasted in a codebase when there is room to abstract the copied functionality in a meaningful way.

That said, you should by no means "ABSTRACT ALL THE THINGS!".

Sometimes it can serve a purpose to have code be explicit in two places, even if it means the snippets are nearly identical. A good example for this are Database Adapters in a project like Ecto, where you might have nearly identical functions for things like order_by or limit in both the Postgres and MySQL adapters.

In this case, introducing an AbstractAdapter just to avoid code duplication might cause more trouble down the line than having a bit of duplicated code.

Like all Software Design issues, this is just advice and might not be applicable to your project/situation.

Check-Specific Parameters

Use the following parameters to configure this check:

:mass_threshold

The minimum mass which a part of code has to have to qualify for this check.

This parameter defaults to 40.

:nodes_threshold

The number of nodes that need to be found to raise an issue.

This parameter defaults to 2.

:excluded_macros

List of macros to be excluded for this check.

This parameter defaults to [].

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.

Summary

Functions

Returns the mass (count of instructions) for an AST.

Takes a map of hashes to nodes and prunes those nodes that are just subnodes of others in the same set.

Returns a hash-value for a given +ast+.

Functions

Link to this function

add_masses(found_hashes)

View Source
Link to this function

calculate_hashes(ast, existing_hashes \\ %{}, filename \\ "foo.ex", mass_threshold \\ param_defaults()[:mass_threshold])

View Source

Calculates hash values for all sub nodes in a given +ast+.

Returns a map with the hashes as keys and the nodes as values.

Link to this function

create_issue?(arg1, excluded_macros)

View Source

Returns the mass (count of instructions) for an AST.

Link to this function

prune_hashes(given_hashes, mass_threshold \\ param_defaults()[:mass_threshold])

View Source

Takes a map of hashes to nodes and prunes those nodes that are just subnodes of others in the same set.

Returns the resulting map.

Returns a hash-value for a given +ast+.