Qqwy

Qqwy

TypeCheck Core Team

Code style: Main functions on top, small functions on bottom or vice-versa?

When programming a module in Elixir, I like to start my module with one or two ‘main’ functions that show the main flow of data that will go on in here, and then write out the ‘small’ functions that are used internally in there.

I find this to be very readable, and giving future readers the opportunity to first see the problem from afar, and then zoom in to the part where you think something might need to be changed or updated.

Now I’ve seen multiple people writing modules in the opposite fashion, with the smallest (often private) functions on top, and the main functions at the bottom. I personally find it less nice to read, but are there any advantages of this approach?

Most Liked

NobbZ

NobbZ

I do prefer to have public stuff at the very top of my source files, sorted by importance, after that all the private stuff in random order¹. Organizing the module this way, makes public stuff much more discoverable.

Nevertheless, especially when getting back from a C, C++, Idris, or Agda marathon, I often code in the opposite direction, because all of these languages need to have at least declare before use.

1: Whenever I realize there is a new private necessary I hit PgDn a couple of times, and when my cursor is in the section of private functions, I start to code/hack where I am.

karolsluszniak

karolsluszniak

This is indeed a nice question. Reading through Programming Elixir, I’ve got an impression that each public function should have its private helper functions grouped directly under it. This is certainly made easy by such a compact privacy notation of def and defp.

Still, I didn’t find any info about this neither in the book nor in other articles (such as the style guide). Maybe it’s a delicate matter and no one wants to enforce one way.

I’d also suggest taking a look at source code of serious repos like Elixir, Phoenix or Ecto. I find Elixir sources nicely written and highly readable.

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

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
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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 48342 226
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement