Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[in-review] Code optimisations via adding the credo #285

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ jobs:
run: mix deps.get
- name: Run tests
run: mix test
- name: Run static analytics
run: mix credo suggest --all --strict
1 change: 1 addition & 0 deletions lib/blanks.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Blanks do
@moduledoc false
def replace(ast, replacements) do
replacements = List.wrap(replacements)

Expand Down
5 changes: 3 additions & 2 deletions lib/display.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule Display do
@moduledoc false
use GenServer

alias IO.ANSI
alias Display.{ProgressBar, Intro, Failure, Notifications}
alias Display.{Failure, Intro, Notifications, ProgressBar}

def start_link do
GenServer.start_link(__MODULE__, %{clear_screen: true}, name: __MODULE__)
Expand All @@ -20,7 +21,7 @@ defmodule Display do
{:noreply, %{state | clear_screen: false}}
end

def handle_cast(:clear_screen, state = %{clear_screen: true}) do
def handle_cast(:clear_screen, %{clear_screen: true} = state) do
IO.puts(ANSI.clear())
IO.puts(ANSI.home())

Expand Down
3 changes: 3 additions & 0 deletions lib/display/colours.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.Paint do
@moduledoc false
def red(str), do: painter().red(str)
def cyan(str), do: painter().cyan(str)
def green(str), do: painter().green(str)
Expand All @@ -13,6 +14,7 @@ defmodule Display.Paint do
end

defmodule Display.Colours do
@moduledoc false
alias IO.ANSI

def red(str), do: colourize(ANSI.red(), str)
Expand All @@ -26,6 +28,7 @@ defmodule Display.Colours do
end

defmodule Display.Uncoloured do
@moduledoc false
def red(str), do: str
def cyan(str), do: str
def green(str), do: str
Expand Down
1 change: 1 addition & 0 deletions lib/display/failure.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.Failure do
@moduledoc false
alias Display.Paint

@no_value :ex_unit_no_meaningful_value
Expand Down
7 changes: 4 additions & 3 deletions lib/display/intro.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule Display.Intro do
@moduledoc false
alias Display.Paint

def intro(module, modules) do
if not (module in modules) do
show_intro(module.intro)
else
if module in modules do
""
else
show_intro(module.intro)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/display/notification.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.Notifications do
@moduledoc false
alias Display.Paint

def congratulate do
Expand All @@ -13,8 +14,7 @@ defmodule Display.Notifications do
defp module_names(modules) do
modules
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&name/1)
|> Enum.join(", ")
|> Enum.map_join(", ", &name/1)
|> Paint.red()
end

Expand Down
1 change: 1 addition & 0 deletions lib/display/progress_bar.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.ProgressBar do
@moduledoc false
@progress_bar_length 30

def progress_bar(%{current: current, total: total}) do
Expand Down
1 change: 1 addition & 0 deletions lib/elixir_koans.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule ElixirKoans do
@moduledoc false
use Application

def start(_type, _args) do
Expand Down
1 change: 1 addition & 0 deletions lib/execute.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Execute do
@moduledoc false
def run_module(module, callback \\ fn _result, _module, _koan -> nil end) do
Enum.reduce_while(module.all_koans, :passed, fn koan, _ ->
module
Expand Down
31 changes: 13 additions & 18 deletions lib/koans.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Koans do
@moduledoc false
defp valid_name(name) do
Regex.match?(~r/([A-Z]|\.\.\.).+/, name)
end
Expand All @@ -18,12 +19,10 @@ defmodule Koans do
generate_test_method(unquote(compiled_name), unquote(number_of_args), unquote(body))

def unquote(compiled_name)() do
try do
unquote(compiled_body)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
end
unquote(compiled_body)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
end
end
end
Expand All @@ -35,12 +34,10 @@ defmodule Koans do

quote do
def unquote(name)(answer) do
try do
unquote(single_var)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
end
unquote(single_var)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
end
end
end
Expand All @@ -53,12 +50,10 @@ defmodule Koans do

quote do
def unquote(name)({:multiple, unquote(answer_vars)}) do
try do
unquote(multi_var)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
end
unquote(multi_var)
:ok
rescue
e -> {:error, __STACKTRACE__, e}
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/koans/01_equalities.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Equalities do
@moduledoc false
use Koans

@intro """
Expand Down
1 change: 1 addition & 0 deletions lib/koans/02_strings.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Strings do
@moduledoc false
use Koans

@intro "Strings"
Expand Down
3 changes: 2 additions & 1 deletion lib/koans/03_numbers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Numbers do
@moduledoc false
require Integer
use Koans

Expand Down Expand Up @@ -49,18 +50,18 @@
end

koan "Let's grab the individual digits in a list" do
individual_digits = Integer.digits(58127)
individual_digits = Integer.digits(58_127)

Check warning on line 53 in lib/koans/03_numbers.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "individual_digits" is unused (if the variable is not meant to be used, prefix it with an underscore)
assert individual_digits == ___
end

koan "Oh no! I need it back together" do
number = Integer.undigits([1, 2, 3, 4])

Check warning on line 58 in lib/koans/03_numbers.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "number" is unused (if the variable is not meant to be used, prefix it with an underscore)

assert number == ___
end

koan "Actually I want my number as a string" do
string_digit = Integer.to_string(1234)

Check warning on line 64 in lib/koans/03_numbers.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "string_digit" is unused (if the variable is not meant to be used, prefix it with an underscore)

assert string_digit == ___
end
Expand Down Expand Up @@ -109,14 +110,14 @@
end

koan "I want the first and last in the range" do
first..last = Range.new(1, 10)

Check warning on line 113 in lib/koans/03_numbers.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "first" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 113 in lib/koans/03_numbers.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "last" is unused (if the variable is not meant to be used, prefix it with an underscore)

assert first == ___
assert last == ___
end

koan "Does my number exist in the range?" do
range = Range.new(1, 10)

Check warning on line 120 in lib/koans/03_numbers.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "range" is unused (if the variable is not meant to be used, prefix it with an underscore)

assert 4 in range == ___
assert 10 in range == ___
Expand Down
1 change: 1 addition & 0 deletions lib/koans/04_atoms.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Atoms do
@moduledoc false
use Koans

@intro "Atoms"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/05_tuples.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Tuples do
@moduledoc false
use Koans

@intro "Tuples"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/06_lists.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Lists do
@moduledoc false
use Koans

@intro "Lists"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/07_keyword_lists.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule KeywordLists do
@moduledoc false
use Koans

@intro "KeywordLists"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/08_maps.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Maps do
@moduledoc false
use Koans

@intro "Maps"
Expand All @@ -22,7 +23,7 @@
end

koan "Extending a map is as simple as adding a new pair" do
person_with_hobby = Map.put(@person, :hobby, "Kayaking")

Check warning on line 26 in lib/koans/08_maps.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "person_with_hobby" is unused (if the variable is not meant to be used, prefix it with an underscore)
assert Map.fetch(person_with_hobby, :hobby) == ___
end

Expand Down
1 change: 1 addition & 0 deletions lib/koans/09_map_sets.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule MapSets do
@moduledoc false
use Koans

@intro "My name is Set, MapSet."
Expand Down
4 changes: 4 additions & 0 deletions lib/koans/10_structs.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule Structs do
@moduledoc false
use Koans

@intro "Structs"

defmodule Person do
@moduledoc false
defstruct [:name, :age]
end

Expand Down Expand Up @@ -35,10 +37,12 @@ defmodule Structs do
end

defmodule Plane do
@moduledoc false
defstruct passengers: 0, maker: :boeing
end

defmodule Airline do
@moduledoc false
defstruct plane: %Plane{}, name: "Southwest"
end

Expand Down
1 change: 1 addition & 0 deletions lib/koans/11_sigils.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Sigils do
@moduledoc false
use Koans

@intro "Sigils"
Expand Down
3 changes: 3 additions & 0 deletions lib/koans/12_pattern_matching.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PatternMatching do
@moduledoc false
use Koans

@intro "PatternMatching"
Expand Down Expand Up @@ -94,6 +95,7 @@ defmodule PatternMatching do
end

defmodule Animal do
@moduledoc false
defstruct [:kind, :name]
end

Expand All @@ -103,6 +105,7 @@ defmodule PatternMatching do
end

defmodule Plane do
@moduledoc false
defstruct passengers: 0, maker: :boeing
end

Expand Down
4 changes: 2 additions & 2 deletions lib/koans/13_functions.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Functions do
@moduledoc false
use Koans

@intro "Functions"
Expand Down Expand Up @@ -104,8 +105,7 @@ defmodule Functions do
result =
"full-name"
|> String.split("-")
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
|> Enum.map_join(" ", &String.capitalize/1)

assert result == ___
end
Expand Down
1 change: 1 addition & 0 deletions lib/koans/14_enums.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Enums do
@moduledoc false
use Koans

@intro "Enums"
Expand Down Expand Up @@ -26,7 +27,7 @@
end

koan "Sometimes you just want to know if an element is part of the party" do
input = [1, 2, 3]

Check warning on line 30 in lib/koans/14_enums.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "input" is unused (if the variable is not meant to be used, prefix it with an underscore)
assert Enum.member?(input, 1) == ___
assert Enum.member?(input, 30) == ___
end
Expand Down Expand Up @@ -60,8 +61,8 @@
end

koan "Zip-up in pairs!" do
letters = [:a, :b, :c]

Check warning on line 64 in lib/koans/14_enums.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "letters" is unused (if the variable is not meant to be used, prefix it with an underscore)
numbers = [1, 2, 3]

Check warning on line 65 in lib/koans/14_enums.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "numbers" is unused (if the variable is not meant to be used, prefix it with an underscore)
assert Enum.zip(letters, numbers) == ___
end

Expand Down
9 changes: 7 additions & 2 deletions lib/koans/15_processes.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Processes do
@moduledoc false
use Koans

@intro "Processes"
Expand Down Expand Up @@ -88,8 +89,12 @@ defmodule Processes do

send(pid, {:hello, self()})

timeout = 100 # ms
failure_message = "Sorry, I didn't get the right message. Look at the message that is sent back very closely, and try again"
# ms
timeout = 100

failure_message =
"Sorry, I didn't get the right message. Look at the message that is sent back very closely, and try again"

assert_receive ___, timeout, failure_message
end

Expand Down
1 change: 1 addition & 0 deletions lib/koans/16_tasks.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Tasks do
@moduledoc false
use Koans

@intro "Tasks"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/17_agents.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Agents do
@moduledoc false
use Koans

@intro "Agents"
Expand Down
2 changes: 2 additions & 0 deletions lib/koans/18_genservers.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule GenServers do
@moduledoc false
use Koans

@intro "GenServers"

defmodule Laptop do
@moduledoc false
use GenServer

#####
Expand Down
19 changes: 16 additions & 3 deletions lib/koans/19_protocols.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Protocols do
@moduledoc false
use Koans

@intro "Want to follow the rules? Adhere to the protocol!"
Expand All @@ -12,13 +13,25 @@ defmodule Protocols do
end

defmodule Painter do
@moduledoc false
@derive Artist
defstruct name: ""
end

defmodule(Musician, do: defstruct(name: "", instrument: ""))
defmodule(Dancer, do: defstruct(name: "", dance_style: ""))
defmodule(Physicist, do: defstruct(name: ""))
defmodule Musician do
@moduledoc false
defstruct(name: "", instrument: "")
end

defmodule Dancer do
@moduledoc false
defstruct(name: "", dance_style: "")
end

defmodule Physicist do
@moduledoc false
defstruct(name: "")
end

defimpl Artist, for: Musician do
def perform(musician) do
Expand Down
Loading
Loading