Skip to content

Credo Elixir

jovanhan2 edited this page Nov 7, 2018 · 3 revisions

Credo is a static code analysis tool for the Elixir language with a focus on teaching and code consistency.

Styling highlights

  • Seperate nested if.. else into multiple functions if the depth is greater than 1

Features

Have a way of displaying more info about hints if needed. E.g. copy filename + line combo

┃ Phoenix.Channel
┃
┃   [R] Category: refactor
┃    ↗  Priority: medium
┃
┃       If/else blocks should not have a negated condition in `if`.
┃       lib/phoenix/channel.ex:26 (Phoenix.Channel.subscribe)
┃
┃    __ CODE IN QUESTION
┃
┃       if !Socket.authenticated?(socket, channel, topic) do
┃
┃    __ WHY IT MATTERS
┃
┃       An `if` block with a negated condition should not contain an else block.
┃
┃       So while this is fine:
┃
┃           if !allowed? do
┃             raise "Not allowed!"
┃           end
┃
┃       The code in this example ...
┃
┃           if !allowed? do
┃             raise "Not allowed!"
┃           else
┃             proceed_as_planned
┃           end
┃
┃       ... should be refactored to look like this:
┃
┃           if allowed? do
┃             proceed_as_planned
┃           else
┃             raise "Not allowed!"
┃           end
┃
┃       The reason for this is not a technical but a human one. It is easier to wrap
┃       your head around a positive condition and then thinking "and else we do ...".
┃
┃       In the above example raising the error in case something is not allowed
┃       might seem so important to put it first. But when you revisit this code a
┃       while later or have to introduce a colleague to it, you might be surprised
┃       how much clearer things get when the "happy path" comes first.

Exit status

he exit status of each check is used to construct a bit map of the types of issues which were encountered by or-ing them together to produce the final result

use Bitwise

issues
|> Enum.map(&(&1.exit_status))
|> Enum.reduce(0, &(&1 ||| &2))

with

consistency:  1
design:       2
readability:  4
refactor:     8
warning:     16

So 12 means readability + reafactoring

Suggest, list and explain

  • suggest - suggest issues
  • suggests issues and groups by file
  • explain - details and reasoning, via filename:line_number:column (as above)

Clone this wiki locally