Writing clean, consistent and maintainable code is crucial for long-lived Elixir projects.
Credo is a static code analysis tool that helps enforce coding standards, highlight potential bugs and suggest improvements to readability and design.
Why Use It?
Keeps your codebase consistent across team members
Detects common mistakes early (unused variables, missing docs, overly complex functions)
Acts like a “code reviewer” that never gets tired
🍳 Recipe 7: Using Credo for Code Analysis in Elixir
Add Credo
to dependencies in mix.exs
(look for deps
method and add in list):
defp deps do
[
..., # some deps here (make sure to have comma at end)
{:credo, "~> 1.7", only: [:dev, :test], runtime: false} # add this
]
end
and run mix deps.get
to download/update deps.
Running credo is simple:
mix credo
If need details (take full line from running mix credo
):
mix credo explain apps/comixone_scripts/lib/readmanga/manga_listing_scraper.ex:69:5
what if we want to disable such warnings?
┃ You can disable this check by using this tuple
┃
┃ {Credo.Check.Warning.IoInspect, false}
┃
┃ There are no other configuration options.
Let’s configure it!
generate config template using:
mix credo.gen.config
then look for file: .credo.exs
and change it based on recommendation above
🔥 Real-world suggestions:
Run mix credo locally before pushing changes.
Add it to your CI pipeline so that style and quality are enforced automatically.
Explore checks like cyclomatic complexity and refactoring hints to continuously improve your code.