Elixir Tutorial: How def and defp Control Function Visibility in Modules
A tutorial published on DEV Community explains the distinction between public (def) and private (defp) functions in the Elixir programming language. Public functions defined with def are accessible from any module and form a module's external interface, while defp restricts a function's use to within its own module. The compiler enforces this privacy by raising an UndefinedFunctionError when outside code attempts to call a private function. The article uses a running todo-list project built with Elixir 1.20.1 to demonstrate how marking helper functions as defp keeps a module's public surface small and signals to other developers that those functions are internal implementation details. The author argues that limiting the number of public functions is itself a form of API design, making a module's intended role clearer and allowing internal refactoring without breaking dependent code.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in