jeramyRR

jeramyRR

Is it necessary to warn about the @doc attribute for a private function

Is it really necessary for the compiler to spit out a warning about using @doc with private functions?

I don’t want to have to document my functions in two different ways. I want one clean way to present my comments to other developers throughout the codebase, at least at the function head.

I’ve seen others say, on this forum, that private functions aren’t meant to be documented because they are ephemeral. I believe this to be nonsense. Someone else not writing my code has no say in how long I keep my private functions around. I guarantee you even great developers like Jose Valim write private functions. You can probably find them in elixir’s code itself.

I don’t think we need an @docp or any modifiers to go with the @doc attribute. Just good ole @doc would be great for all functions.

Anyways, I know other people have asked the same question and get the same answer, but I think it’s worth drudging up again.

Most Liked

josevalim

josevalim

Creator of Elixir

Yes, this is total non-sense. Our instance on docs and code comments have been clear since day one.

  1. Documentation is for users of your code (who do not necessarily have access to source)
  2. Code comments are for maintainers of the code (who are looking at the source right now)

It is really important to make this distinction because you often want to provide different information for those different “audiences”. Elixir makes use of both whenever necessary. Sometimes we even have a doc followed by extensive code comments. A user of the function does not care about implementation details but, if you are there in the source, trying to figure it out why it works like that, the information is there.

A private only exists in the source code, that’s why you can’t document something private. But you can certainly add code comments to it. Please do write code comments whenever you feel it is necessary.

josevalim

josevalim

Creator of Elixir

The behaviour could be changed to discard @doc but I think allowing @doc in privates would open up a bunch of other problems. For example, if you try to write a doctest, it won’t work, because you can’t invoke a private function. Oh, do you want to access the @doc in your editor or terminal? That won’t work either, because private functions do not necessarily exist after the module is compiled, they can be inlined, transformed or even fully removed. Allowing @doc would indicate that those two constructs (public and private functions) are somewhat at the same level while they quite clearly aren’t.

All of this has already been said, so I will politely bow out of the discussion as I don’t have anything new to add here. :slight_smile:

wojtekmach

wojtekmach

Hex Core Team

TL;DR:

defmodule Mod do
  @doc """
  Public docs
  """
  def public, do: ...

  # Private docs
  defp private, do: ...
end

that’s it!

While we can’t use @doc for documenting private functions, nothing stops us for using comments to document them. Similarly, if a module is meant to be private we’d mark it as @moduledoc false and document it by using comments. There are many such examples in Elixir itself and libraries.

I don’t think it’s fair to say that documenting internals is discouraged, it’s just we can’t use mechanisms like @doc and @moduledoc for it. @doc etc is used by the compiler to put documentation into beam chunks so that it can be later used by tooling like IEx, ExDoc etc to display public docs. Since private functions are, well, private, tools like IEx and ExDoc won’t seen them so it doesn’t make sense to use @doc etc for them. In fact the compiler could even inline private functions as an optimisation. We could special-case @doc not to put anything into beam chunk if it sees a private function, but that seems like needless complexity. Even if @doc wouldn’t warn, since the tooling won’t show private docs, the only way to see them is to look at the source code. And at the source code, is it really a big deal to use a code comment over a @doc?

dimitarvp

dimitarvp

Honestly, I never had a clue you could do this.

Adzz

Adzz

Just do this:

@doc """
This is a thing where I can write docs and stuff.
""" && false
defp my_very_long_function(arg) do
end

Where Next?

Popular in Discussions Top

adamu
When starting a new project, do you have any go-to libraries you pull in out of habit/preference? For example, Ash, Credo, Mox, ExMachin...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
thiagomajesk
I came across this PKGX tool the other day, if anyone here is using it, could you share your experience? I’m wondering how useful it wou...
New
James_E
I see that the current ExUnit source code has support for rich failure messages on a small whitelist of “recognized” assertion patterns, ...
New
fredwu
Hey folks, Just wanted to share my journey and experiences so far. Preface: I’ve been using Elixir for about 10 years now, so relatively...
New
fireproofsocks
I’m not a Lambda fan-boy because I think it’s overprescribed as a cure-all for every possible problem when in reality, Lambdas are best s...
New
stevensonmt
Has anyone else ever wanted to merge two maps, but have the resulting map only include keys common to both maps? I think of it as analogo...
New
bartblast
Added by a user on X: https://x.com/aldicodes/status/1952095492139299036
New
New
chrisliaw
Hi, I’m wondering is it my thinking process or this is the norm among the Elixir developer for the use of Struct and accessor functions ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
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