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

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement