rcb

rcb

New to Elixir, finished first practice project, and would greatly appreciate feedback/tips for improvement

I’ve been learning Elixir during my month off between semesters. I started a practice project last week (a basic credit card validator application) that I just finished today. If anyone has some time that they wouldn’t mind sharing with me, I would really appreciate some feedback on my code — things like best practices, idiomatic Elixir code, extraneous functions or steps in algorithms, etc.

I’ve pushed the project to GitHub, which can be found here. The primary outer module (Cardinal) contains a simple driver function. The bulk of the work is done in the Cardinal.Validator and Cardinal.Checksum modules.

There are a few tasks/improvement areas that I know I need to address, which I’ve listed in the readme file. I’m sure that I’m missing a lot of areas for improvement, though; I had to rewrite a handful of things throughout the process as I discovered more and more built-in Elixir functions that made certain tasks much easier. Identifying more of such issues would be fantastic for learning more about Elixir.

Thank you very much to anyone willing to take a look and provide feedback.

Most Liked

rodrigues

rodrigues

Hi @rcb, good job, I see you’re catching up fast :crystal_ball::rocket:

A few points:

  • You should try mix format :+1:t3: If you can change your editor to do it automatically on save, it gives you a nice feedback loop. On top of that, credo can also bring you some nice tips on style and code organization.

  • Cardinal.Checksum.double_every_other/2 could be like this instead, what do you think?

  defp double_every_other([], _pos), do: []
  
  defp double_every_other([head|tail], pos) when rem(pos, 2) == 0 do
    [head | double_every_other(tail, pos + 1)]
  end
  
  defp double_every_other([head|tail], pos) when rem(pos, 2) == 1 do
    [head * 2 | double_every_other(tail, pos + 1)]
  end

Note that rem/2 can only be at the function head because it’s also a guard.

(EDIT: maybe just using Enum.with_index/1 and Enum.map/2 could be better than the above)

  • Prefer is_binary/1 than is_bitstring/1 if you want a guard to guarantee it’s a String. Every binary is a bitstring, but not every bitstring is a binary.

Cheers!

rcb

rcb

Those look a lot cleaner than mine. The double_every_other() function was one I wrote early on when the program worked a bit differently and I don’t think I really revisited it much except for minor cleanup when the program changed. When I get back on my main machine, I’ll try this implementation out.

I didn’t realize that distinction was there between those two. I’ll change those out too.

Thanks for looking things over and taking the time to provide some guidance – I really appreciate it!

rcb

rcb

Thanks for the advice regarding passing IO to the private function. I’ve removed that and replaced it with regular IO.gets/puts calls. You’re right about the invalid data example too — I overlooked a pretty big error there. Thanks for the heads up on that, along with the link to the Luhn algorithm example and for taking the time to respond. I appreciate the help!

Also, thanks to both @srowley and @axelson for the excellent advice regarding the reduce_double_digits() function. I’ll work on implementing your suggestions too.

Thanks to everyone who took the time to review my code and provide such helpful advice. I really appreciate the help and guidance!

Where Next?

Popular in Questions Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement