binarypaladin
Semi-private functions and documentation
In projects and libraries I’ve found that as things become more complicated, “semi-private” functions pop up. What do I mean by that? (I’m glad you asked.)
We all know and love private functions. I know I do. For me these functions do not have to support a stable API, can be gutted, changed, removed, or heck… even inlined by the compiler if necessary.
However, sometimes I need functionality that’s cross module but the function is something—especially in the case of creating a public library—that is considered private. “Hey, if you’re not working on this library, don’t use this function. It’s for internal use. Don’t rely on it. We might change it. Additionally, its functionality is really meant for the sorts of implementation details our consumers need not concern themselves with.”
The moment you want to use this cross module, it’s gotta be public. But there are some functions in a system you really care about a stable API and others that you don’t.
Even in my own commercial project where all the developers are working on the same codebase, there are pieces of functionality that are similar. “Don’t touch this unless working in conjunction with X. It’s exposed only because it has to be.” This works pretty well there.
Is there some official way to do a couple things:
- Document a function but keep the documentation out of ExDoc? (Why would I want to do this? Because, like comments, you’d only see them in the codebase rather than in the docs.)
- Mark a function as having an unstable API or unsuitable as an API? (This would be better, in my opinion.)
I’ve only seen two patterns that I took note of:
- Document in comments only so the public docs show no docs. This works because only someone in the code sees the “docs” but it’s a bit jarring and I find it “ugly.”
- Add a disclaimer to the docs for the function. “Don’t touch!” This is my preferred path, but if I were creating a library for a Hex package, I’d want something a bit more official.
WHAT I AM NOT ASKING: I’m not asking about whether semi-private functions are appropriate. I am not asking if I should reconsider their use. I’m asking if there is a better, official way to mark/handle/document said functions because they live in my codebase and the codebases of others.
Most Liked
Adzz
Just to call out you can also do this:
defmodule Thing do
@doc ""
An explanation
""" && false
def semi_public(thing) do
end
end
which will still exclude the doc from hexdocs but lets you write a doc still.
LostKobrakai
The most official way here is @doc false / @moduledoc false. The latter allows you to still have documentation for functions while having them not show up in ex_doc anyways. That’s the approach taken by elixir itself and you can see it used in many of the larger elixir projects like phoenix and others.
martosaur
FWIW whenever I see @doc false / @moduledoc false in a library code, the intent is very clear to me and it is exactly what you describe.
D4no0
This is a good point, not having these protected functions is a limitation that I also encounter very often.
For application code, I use boundary, it’s an extremely powerful concept to separate the internal functionality of a feature and create a public interface. I never tried to use a library that uses boundary, but I think it might work as well.
It doesn’t do any magic besides checking if you have boundary violations in your codebase, so it cannot make documentation private. I am not entirely sure, but I think you could disable documentation for unwanted modules by just doing @moduledoc false.
LostKobrakai
This is a tricky subject because it’s an open ended one. There’s a myriad of permutations around where, when and how a developer (or really some other piece of code) might be meant/allowed to use a function or not and for the former get more context (docs) or not.
Visibility from the language level (and really the runtime level) is nothing elixir can change. That comes with using erlang and the beam, which have visibility toggles on a per module level by either exporting or not exporting a function/type/….
The approach of using @moduledoc false is kinda an extension to the module level approach, where shared or extracted functionality, which is required to be publicly available, on the module is marked to not generate documentation to signify that functions on that module are not to be used by third party code. Boundary goes farther by enforcing such conventions at a technical level with a compiler as best as possible, but that requires spelling out the access patterns as well as types of “consumers of code” explicitly.
Both @doc false as well as Boundary can also do their signaling at a function level, where the former comes with the downside that you cannot have documentation and at the same time not have it.
None of these can prevent someone consuming the code as a dependency to ignore the signals and still call public functions even if not supposed to do that, given the beam simply doesn’t support that. The elixir compiler could attempt to statically check that, but things like apply and hot code updates mean that’ll never be perfect and Boundary already exists if you want to use an imperfect solution.







