Improving coding

Hi all

I wrote two modules, that looks pretty much the same:

defmodule Busiket.Repo.Seeders.CountryCode do

  alias Busiket.Repo
  alias Busiket.CountryCode
  alias Poison.Parser

	def insert(lc) when is_list(lc) do

    lc
    |> Enum.each(fn(ele) -> Repo.insert!(ele) end)

  end

  def delete() do

    Repo.delete_all()

  end

	def convert_json_struct(filename) when is_binary(filename) do

    filename
    |> query_json_file!
    |> convert_json_language_code

  end

  defp query_json_file!(filename) do

    case File.read(filename) do
      {:ok, values} ->
        values
      {:error, reason} ->
        raise reason
    end

  end

  defp convert_json_language_code(json) when is_bitstring(json) do

    json
    |> Parser.parse!
    |> Enum.map(fn(ele) -> %CountryCode{alpha2: ele["alpha-2"], alpha3: ele["alpha-3"]} end)

  end

end 

and

defmodule Busiket.Repo.Seeders.LanguageCode do

  alias Busiket.Repo
  alias Busiket.LanguageCode
  alias Poison.Parser

	def insert(lc) when is_list(lc) do

		lc
		|> Enum.each(fn(ele) -> Repo.insert!(ele) end)

	end

	def delete() do

	  Repo.delete_all()

	end


	def convert_json_struct(filename) when is_binary(filename) do

	  filename
	  |> query_json_file!
	  |> convert_json_language_code

	end

  defp query_json_file!(filename) do

    case File.read(filename) do
      {:ok, values} ->
        values
      {:error, reason} ->
        raise reason
    end

  end

  defp convert_json_language_code(json) when is_bitstring(json) do

		json
		|> Parser.parse!
		|> Enum.map(fn(ele) -> %LanguageCode{code: ele["alpha2"], text: ele["English"]} end)

	end

end

I feel like, the codes look pretty much so same. How can I make more clean?

Thanks

1 Like

The key to removing duplicating is identifying the stuff that changes :slight_smile: , Since the only thing that changes between these two files is the struct builder function, we can pass it via an argument

defmodule Busiket.Repo.Seeders.DataSeeder do

  alias Busiket.Repo
  alias Busiket.LanguageCode
  alias Poison.Parser

  def insert(lc) when is_list(lc) do
    lc
    |> Enum.each(fn(ele) -> Repo.insert!(ele) end)
  end

  def delete() do
    Repo.delete_all()
  end


  def convert_json_struct(filename, builder_fun) when is_binary(filename) and is_function(builder_fun, 1) do
    filename
    |> query_json_file!
    |> convert_json_language_code(builder_fun)
  end

  defp query_json_file!(filename) do
    case File.read(filename) do
      {:ok, values} ->
        values
      {:error, reason} ->
        raise reason
    end
  end

  defp convert_json_language_code(json, builder_fun) when is_bitstring(json) and is_function(builder_fun, 1)do
    json
    |> Parser.parse!
    |> Enum.map(builder)
  end

end

Busiket.Repo.Seeders.DataSeeder.query_json_struct("language_codes.json",
  fn(ele) -> %LanguageCode{code: ele["alpha2"], text: ele["English"]} end)

Busiket.Repo.Seeders.DataSeeder.query_json_struct("country_codes.json",
  fn(ele) -> %CountryCode{alpha2: ele["alpha-2"], alpha3: ele["alpha-3"]} end)