benperiton

benperiton

Code formatting - always start with a raw value?

Hey,

Just a general question regarding code formatting. I ran credo earlier, and it gave me a couple of refactoring opportunities for Pipe chain should start with a raw value.

def find(attribute, value) do
    result = 
      PackageQuery.find_by(attribute, value)
      |> Repo.one
    
    case result do
      package ->
        {:ok, package}

      nil ->
        {:error, nil}
    end
  end

I guess it wants me to reformat it so it looks like

def find(attribute, value) do
    result = 
      attribute
      |> PackageQuery.find_by(value)
      |> Repo.one
    
    case result do
      package ->
        {:ok, package}

      nil ->
        {:error, nil}
    end
  end

That doesn’t look particularly readable to me, if its a single thing to pass through then yea, I get that, but if the function takes multiple arguments, then it seems that it makes it less obvious.

So my question is, what does everyone else do? Always start a chain with a raw value, or just do what reads best? And is there any benefit to doing so?

Most Liked

Kabie

Kabie

I usually turn off this check.

I think the pipeline is about data transformation, and a pipeline should always start with the data. When the data I want to work with is not a raw value(or variable) but needs to be retrived from a function call, I will start with the function call, even if it have arity 1.

sillypog

sillypog

I totally agree, benperiton. I am always sad when I have to convert your second example into the first just to satisfy credo. I don’t want to turn off the check though because, as you said, it does improve readability for arity/1 functions.

It seems to imply a hierarchy for the parameters that may not exist. In the arity/1 case, it’s not just more readable; it makes semantic sense because it is pushing this piece of data through a series of transformations and/or supplying that as input for subsequent steps. But with the example here, separating :first from :second and :third implies that :first is the data being transformed and that :second and :third are incidentally needed for that first step. If foo was a function called sum, it would be clear that the parameters have equal weight and order is not important.

I would say that if you can imagine reordering the parameters supplied to foo and it makes no semantic difference to what the function is doing, you should not start with a raw value.

lpil

lpil

Creator of Gleam

exfmt will never change the semantics of your code, so whichever way you write it will be respected. :slight_smile:

PatNowak

PatNowak

In your case it might not as important as in other cases, because when you have pipe chained from 1 or 2 function calls, it’s easy enough to read it- even - when it’s written in “Java” style:

a = foo(bar(:first, :second))

However even on a simple example we see that using pipe chains makes code more readable:

a = :first 
    |> bar(:second)
    |> foo()

But imagine here a pipe chained from 7 or more function calls eg. when using functions to validate the changeset.
Following credo in the project I work in, it’s not mandatory, but I treat it as a helpful tip. If I think I can omit some rules, I want to think twice, because things that are “ok” for me, sooner or later might be ugly for someone else… or me in the future :wink:

voger

voger

I generally try to follow this style guide

Especially for this case prefer bare variables to start the chain

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement