binarypaladin

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:

  1. 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.)
  2. 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:

  1. 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.”
  2. 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

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

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

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

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

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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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