spencer.christensen
I’m going through Tate and DeBenedetto’s Programming Phoenix LiveView and observed the code below.
defp assign_chart_svg(%{assigns: %{chart: chart}} = socket) do
socket
|> assign(:chart_svg, render_bar_chart(chart, title(), subtitle(), x_axis(), y_axis()))
end
defp title do
"Product Ratings"
end
defp subtitle do
"Average star ratings per product"
end
defp x_axis do
"products"
end
defp y_axis do
"stars"
end
As somebody new to functional programming, I’m wondering whether this granular-level of function definitions is typical? I can see some options:
- it is idiomatic and a best practice to wrap strings in a function.
- it is only a best practice to wrap a string in a function when the function name can clarify the purpose of the string (“stars” is not as descriptive as
y_axis) - it is unusual and is likely being done to account for some sort of future abstraction that relies on a callback of said functions
I’m not arguing for or against anything, just curious for best-practices and idiomatic Elixir use of string binaries. Thanks!
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mindok
Probably a more common alternative in Elixir would be to use module attributes. e.g.:
mindok
…However, module attributes are private to a module, so to expose them to other modules you would need to use a public function, e.g.
sodapopcan
Ya, what @mindok said. I have have barely read any of that book so maybe there are going somewhere with it and will flesh it out later? Wrapping strings in functions as a general rule certainly isn’t a “thing” though. I’ve never done it and this is the first time I’ve seen code that does it (I’ve been doing Elixir for around 5 years).
From what I have read of the book, though, they do extract far more than I personally care to. There are lots of small single-user private functions that don’t really read any better than the bare code in my view. I’m not implying the book is bad by any means (how could I know? …and all of Sophie’s stuff I have read and watched is amazing) and while idiomatic Elixir is certainly a thing, it’s not super obsessive and people have varying styles.
EDIT: One reason I can think of to use functions is that heex overloads
@to access assigns. So if you haveThat reads super confusingly because the
@titlein therenderfunction is referring to:titlein the socket, not the module attribute@title.That’s total guess, though.
al2o3cr
IMO this would be clearer as a function that took
chartand a keyword list:That avoids the confusion of “what the heck ARE all these unnamed literal positional args” while not spamming private functions.
christhekeele
This is correct—it is a conceit they are using to keep the examples focused to the “lesson” at hand: learning Liveview; and to leave “room to grow” the examples with later functionality.
@mindok’s posts concerning module attributes are the idiomatic approach to this! But you can see why introducing those patterns don’t serve what is being taught: instructive writing can be weird like that sometimes.