manckaert

manckaert

Code Critique: struggling with 'correct' functional style

Hello everyone

I’m looking for some feedback on this code I wrote: redirect-checker (I didn’t paste the code here as it’s about 40 lines).

Thanks!

Most Liked

mindok

mindok

Welcome @manckaert!

Looks good to me. My main metric is whether I can read & understand it quickly, and I could.

mix format is your friend (although, like all friends, I have some disagreements from time to time). It’s good to get into the habit of using it - the community’s eyes are used to running over code formatted in a particular way. There’s only one block of code that got reorganised when I ran it:

    redirect_to =
      case location_header do
        {"Location", loc} -> loc
        nil -> nil
      end

If this becomes part of a bigger system, you may find passing tuples around gets confusing, in which case upgrade to a map or (better still) a struct.

Also, HTTPoison.get would, I imagine, be quite slow with lots of wait time. If you want to learn a bit more about the power of BEAM, you could have a crack at parallelising. I’m sure there are many articles explaining how - my google-fu turned up a reasonable one as a starting point: Write Unbelievably Clean Concurrent Code in Elixir Using the Task Module | TopTechSkills.com

BartOtten

BartOtten

Welcome @manckaert!

Additional to the comments written above, you could split the last function declaration:

# matches `foo,bar,foo` as the third argument should be the same as the first.  
# The `to_url` can be seen as an alias and makes the code easier to understand.
def verify_response({redirect_to, from_url, to_url = redirect_to}), do:
      %{result: :match, from_url: from_url, to_url: to_url}

# matches `foo,bar,baz`
def verify_response({redirect_to, from_url, to_url}), do:
      %{result: :no_match, from_url: from_url, to_url: to_url, redirect_to: redirect_to}

In your code, the function is quite small so it’s easy to digest. This is just to show you a technique you might not know already :slight_smile:

manckaert

manckaert

Thank you @mindok for the welcome and taking a look!

You’re right about the formatting, I come from Python mostly and there black is a lifesaver - especially in a team. I was mostly relying on how Emacs formatted my code but I fixed and issue with elixir-ls and now the section you highlighted gets formatted correctly.

I had already started rewriting it using Tasks as I had already noticed the IO is killing performance when doing the HTTP requests. It’s turning out quite nice but currently the “result collection” is pretty messy :sweat_smile:

Thank you again for your time!

manckaert

manckaert

Thanks @BartOtten! I had thought of doing the verification like that as it feels more “elixir - like” but never got it working. Thanks for showing me the correct way :smiley:

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement