How to properly indent this pipe?

After reading the community style guide for Elixir, fell upon the following rule:

which defines how one should ident code using the pipe macro.
However, how does this rule apply to the following code ?

[
      System.get_env("PROJECTS_PATH"),
      :alfred |> Application.get_env(:app ) |> to_string
]
|> myFun()

Should it be:

[
      System.get_env("PROJECTS_PATH"),
      :alfred 
      |> Application.get_env(:app ) 
      |> to_string
]
|> myFun()

or:

[
      System.get_env("PROJECTS_PATH"),
          :alfred 
          |> Application.get_env(:app ) 
          |> to_string
]
|> myFun()

Or none of the above?
Should I change it at all ?

1 Like

Ask mix format

edit

Okay, back at a computer and it looks like this:

$ echo "[
           System.get_env("PROJECTS_PATH"),
               :alfred 
               |> Application.get_env(:app ) 
               |> to_string
     ]
     |> myFun()" | mix format -
[
  System.get_env(PROJECTS_PATH),
  :alfred
  |> Application.get_env(:app)
  |> to_string
]
|> myFun()
2 Likes

your List elements should align and the function call should align with the List. So:

[
  System.get_env("PROJECTS_PATH"),
  :alfred
  |> Application.get_env(:app)
  |> to_string
]
|> myFun()

but this is a job that the formatter does for you. I have it incorporated into save in my editor so the file is formatted on save. That’s what I’d expect most folks would do.

3 Likes

Always use mix format

5 Likes

My personal opinion is that the pipe shouldn’t happen within the list. I assign the piped value then use that value within the function.

projects_path = System.get_env("PROJECTS_PATH")
alfred_app = 
  :alfred
  |> Application.get_env(:app)
  |> to_string()

myFun([projects_path, alfred_app])

I’d argue that this is more readable at a glance than inline-evaluation of functions in a list. mix format will fix spacing and such but it can’t fix code clarity.

3 Likes

Don’t waste time with these things, and rely on mix format. Your team will save a bunch of time not worrying about this.

Plus, if your code doesn’t format, it means there’s a typo somewhere… it’s a great feeling when your code shifts around. It means you have no typo bugs :wink:

2 Likes