IciaCarroBarallobre / elixir_wordle

Elixir Wordle Web App featuring daily words related to Elixir.
Apache License 2.0
9 stars 3 forks source link

Seed reptition #24

Closed IciaCarroBarallobre closed 1 year ago

IciaCarroBarallobre commented 1 year ago

So in that example, I would suggest it is useful to notice your reptition, and instead re-describe it in a way that highlights that each piece of data is of a normal collection, and that you intend to treat uniformly. So for example, I removed all those ecto instructions and created a list of plain maps. Those maps then feed into a helper function.

Enum.each([
        ################## Day 1 ##################
        %{
          word: "sigil",
          clue: "Sigils are mechanisms for working with textual representations.",
          description: "Sigils are mechanisms for working with textual representations.
      They start with the tilde (~) character which is followed by a letter and then a delimiter.
      Common sigils: ~r, regex expressions; ~c, charlist; ~s, strings; ~w, lists of words;
      ~D, date; ~T, time;"
        },
        %{
          word: "kernel",
          clue: "Module that contains basic language primitives and macros.",
          description: "You can invoke Kernel functions and macros anywhere in Elixir code
      without the use of the 'Kernel.' prefix since they have all been automatically imported. "
        },
...
], &insert_word/1
    )
  end

  def insert_word(word) do
    word
    |> then(&Word.changeset(%Word{}, &1))
    |> Repo.insert!()
  end

When you repeat instructions, you will inevitably end up with a maintenance burden and it is more difficult for future readers (even yourself) to know at a glance that each item is getting identical treatment