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.

First 10 of 52 Posts! Switch mode

joaoevangelista

joaoevangelista

I have the same feeling about the warning, maybe not even using a new attribute like docp. If that warning is to warn you that you are documenting a function that will never used outside and maybe you declared it wrong by using defp instead of def then the undefined function check will catch it. Or if the documentation will not render on hexdocs it’s ok too it’s private the function will not appear there anyway. Just let us write our docs

mudasobwa

mudasobwa

Creator of Cure

Sure thing. What you are missing is the main point: if there is a need to document a private function, it means there is a need for other developers to use this private function, which means it should not be private.

Private functions are not meant to be re-used. Private functions might stay there forever, but they are just implementation details, they are none of anybody’s business.

If you find yourself explaining to mates what your private functions do, you are either teaching them Elixir in general, or doing it plain wrong.

jeramyRR

jeramyRR

Yeah sure, I get some of what you say, but I don’t think you’re thinking about every situation where you might want to document something you’re doing. Let’s say for instance you’re interfacing with someone else’s api which may be a little complicated, or maybe not. You may want to document why, or what the api is requiring from you in that function.

Or even better, this is a dynamic language, and not everyone uses dialyzer. You may want to document the format of the returned data.

Maybe i’m trying to conform to an RFC and I want to document where to find it.

There are plenty of reasons why I want to document something. All these “you’re doing it wrong if you have to document it” excuses is really just a blow off answer.

To directly answer your “it means there is a need for other developers to use this private function”, that’s not the only case. It’s possible that my logic is completely wrong, or something breaks later on down the road. I want to let the developer that comes after me know why I chose to do something the way I did, or what I thought my function was going to do/return.

I haven’t been programming for that long, but I swear I’ve never seen a community so against documenting their code or making others feel like dog crap for wanting to document theirs.

mudasobwa

mudasobwa

Creator of Cure

I never ever meant this. What I meant is being restrictive here, Elixir compiler plays the role of linter. There are 100 to 1 cases when documenting private functions is wrong. For the rest one still has any other type of comment, including but not limited to assigning the module attribute like

@future_readers """
This is implementing that, it’s done that way because of this. Thanks for understanding.
"""

Allowing comments for privates because not everyone uses dialyzer is exactly what we are against of. It’s like allowing "0" == 0 because not everyone uses proper type conversions, IMSO.

jeramyRR

jeramyRR

This is kind of what I’m talking about “what we are against”. We who? The coding gods that say “Thou shalt program the way I want you to program?”

And, if the coding gods want us to always use Dialyzer, something that’s not even a part of the language, then the coding gods should make the language strongly typed.

I don’t buy this at all. If you’re writing code that is going to be pushed into production, and you’re not commenting it, you’re treating the people that are going to have to debug the code later just plain wrong.

It doesn’t matter that anyone here thinks that you shouldn’t write a private function, or whatever the case is. People are going to write code, and they’re probably going to write it in a way that you think they shouldn’t. It doesn’t matter. There still should be a standard way of documenting or commenting it. The way for that was chosen… @doc. That should be available everywhere.

mudasobwa

mudasobwa

Creator of Cure

That is where you got it wrong. The way for that was chosen (by language creator.) This way is (like it, or hate it, it does not actually matter): you use @doc to comment public functions only, period.

mudasobwa

mudasobwa

Creator of Cure

There is nothing more overhyped for zero value than strong typing. It brings literally nothing save for headaches and enormously huge boilerplates.

jeramyRR

jeramyRR

This is the whole reason for this post. The fact that it can only be used for public functions. In my opinion, and obviously others since there are more posts on this forum talking about it, that it should be open to all functions, not just public.

jeramyRR

jeramyRR

That’s an opinion, and you already went against it when you suggested that commenting be replaced by using dialyzer, which is a typing addon to the language.

benperiton

benperiton

I think the ‘problem’ is that there isn’t a way to just comment and document (in the ‘what this does’ sense, not the ‘create documentation’ sense) a function no matter what it is.

@doc is used to also create documenation automatically - hence people saying don’t use it for private functions, as you don’t want other people to consume those really.

What you want is something like a @nodoc or @docno that allows you to use the same format to comment code, but without it getting attached to auto generatred docs?

Last Post!

nulltree

nulltree

The second point is what really gets me - I pretty much just care about getting better info about the private function’s contract ‘on LSP hover’ myself. My mind is a single-core processor with a small cache - I can’t juggle a lot of context in my head and jumping back and forth to read comments is effectively that.

If that requires a new nomenclature besides documentation and code comments, I’m fine with that.

Where Next?

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2976 91332 914
New
f0rest8
Hi everyone :waving_hand: Posting here to showcase and announce that Metamorphic is now officially live on a public-facing domain at htt...
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
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

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

We're in Beta

About us Mission Statement