reid-rigo

reid-rigo

Hello, I’m new to Elixir and created this package, which is the adaptation of some Ruby code I wrote, as a learning exercise: codeowners | Hex

I’m looking for feedback on:

  • How to make this code more idiomatic
  • More appropriate functions/modules to use
  • Best practices for libraries

Feedback is welcome here or in Github

Showing Posts 10 to 1

reid-rigo

reid-rigo OP

the use of File.cwd! in build is odd; consider allowing callers to either supply a root directly or as an override. To reduce the need for most callers to pass a root, load could do some guessing based on if the supplied file is in a doc or .github directory.

I made a change to accept root as an option in this commit

reid-rigo

reid-rigo OP

Thanks for helping me work through the __MODULE__ stuff. Updated in this commit.

RudManusachi

RudManusachi

One of the point you were asking:

  • How to make this code more idiomatic

So I just pointed to what seemed more idiomatic to me :slightly_smiling_face:

Based on a suggestion in another thread I tried #{__MODULE__} , but that outputs Elixir.Codeowners when I run mix docs.

So it does with any module name, such as: #{Logger} would print "Elixir.Logger". If you want to nicely disregard the prefix Elixir. you could inspect/1 it:

"#{inspect(__MODULE__)}"

See example: elixir/lib/elixir/lib/kernel/utils.ex at c5816a227f5ad295ccbfcc5571308f76190d6bfe · elixir-lang/elixir · GitHub

sodapopcan

sodapopcan

For me it’s not about refactoring—a global rename is a global rename—it’s about ease of reading. When I see __MODULE__ I immediately think “local.” Seeing a fully-qualified self-referencing module name always causes a bit of a jarring pause where I have to go and confirm that it is indeed referencing itself. This is indeed on the minor end of readability issues and is not a hill I’d die on. I do, however, vehemently disagree with the advice in that guide to use an alias if you want “a prettier name” :face_with_raised_eyebrow: If it weren’t for the few legitimate uses cases it has, I wouldn’t be upset if the :as option for alias were removed from the language.

reid-rigo

reid-rigo OP

What problems does using __MODULE__ introduce for you in docs?

Based on a suggestion in another thread I tried #{__MODULE__} , but that outputs Elixir.Codeowners when I run mix docs.

dimitarvp

dimitarvp

What problems does using __MODULE__ introduce for you in docs?

reid-rigo

reid-rigo OP

rule_for_path could be slightly more efficient if the Enum.reverse was moved into build to avoid doing the same computation more than once

I’m really torn on this one. Reversing appears to take a few microseconds, and so for a CODEOWNERS file of significant size, it looks like most of the rule searching time will be spent in Regex.match?

Will ppl be surprised to load their CODEOWNERS and then see all of their rules in reverse order? I could reverse the list again for inspect purposes, but that feels like shenanigans.

the typespec for Codeowners.Rule is not satisfied by the default values in the defstruct. rule_for_path can return a struct containing unexpected types in the “no rule matched” case.

Thank you, fixed.

the use of File.cwd! in build is odd; consider allowing callers to either supply a root directly or as an override. To reduce the need for most callers to pass a root, load could do some guessing based on if the supplied file is in a doc or .github directory.

The ! definitely feels gross, though an exception appears to be a truly rare circumstance. I like your idea of doing some ‘guessing’ - Github specifies 3 possible locations for the file so it wouldn’t be hard to figure out which one was used and derive root from that.

Edit: Because Path.expand uses File.cwd!, I’m not sure there’s anything to be gained here. People should be able to call Codeowners.load(".github/CODEOWNERS")

reid-rigo

reid-rigo OP

I’m not seeing the full benefits of __MODULE__. It appears to help for future refactoring, but what about all of the references in docs?

al2o3cr

al2o3cr

Some thoughts, in no particular order:

  • rule_for_path could be slightly more efficient if the Enum.reverse was moved into build to avoid doing the same computation more than once

  • the typespec for Codeowners.Rule is not satisfied by the default values in the defstruct. rule_for_path can return a struct containing unexpected types in the “no rule matched” case.

  • the use of File.cwd! in build is odd; consider allowing callers to either supply a root directly or as an override. To reduce the need for most callers to pass a root, load could do some guessing based on if the supplied file is in a doc or .github directory.

RudManusachi

RudManusachi

Hi, @reid-rigo :wave: and welcome!

I’d point you to this style guide: GitHub - christopheradams/elixir_style_guide: A community driven style guide for Elixir · GitHub

Use the __MODULE__ pseudo variable when a module refers to itself.

It might look weird at first (becuase of double underscores and all caps) but it’s very common to refer to the module itself.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews