jeroenbourgois
I would like to add a function to my project that is available throughout all modules without aliasing/importing it. How should I go about it? I know I can include it in my *_web.ex file so it becomes available in controllers, views, … but I want to use it in business logic as well. Like the title op my topic suggest, I would like it to be available next to all other default Kernel module functions.
The function itself is very simple:
def is_empty(""), do: true
def is_empty(nil), do: true
def is_empty([]), do: true
def is_empty(_), do: false
Thank you for your input!
PS: I opted for is_empy/1 vs empty?/1 to relate to the existing is_nil/1 function.
PS2: our codebase has various occurences of x in ["", nil] checks, which I would like to simplify.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
There’s nothing automatically imported besides
Kernelin elixir. You either need to import explicitly or have a macro doing the import.christhekeele
Yup. Elixir is explicit about imports by design, with
Kernelbeing the single exception. You will need to define some sort ofMyProject.Kerneland import it wherever you want to use its functions.Just BTW, the
is_nil/1function does not chooseis_nilovernil?half-hazardly, this is in line with a particular Elixir naming convention:To follow this convention that other developers in your project may rely upon, you would either want to name this function
empty?/1, or implement it as a guard.That implementation will be different than the functional version, but can be used anywhere guards or allowed! To translate your example
is_empty/1, it would look something like:Finally, as a design note, outside of extenuating circumstances, I would treat it as a code smell that you are often checking if
thing in ["", nil, []]. The implication is that you are regularly uncertain if your data is a string, list, or null value; all throughout your program.It would be hard with no context to diagnose why this is happening or propose a better pattern, but I would keep
on this part of your code! You may find an opportunity to coerce the variable type of an input into your program into a known single type close to where it is received, then confidently refactor a lot of less-confident, unassertive code. Then theoretically you could handle “empty cases” throughout just by matching against one of
"",[], ornil.jeroenbourgois
Thank you (both) for the responses!
As for the data, most checks are if
a in ["", nil](without the list), because for those fields in the database we can havenilor the empty string. In the database we want the difference, since nil is the default (aka: never set) and the empty string can be ‘set to empty’, but was set none the less.We find it valuable to have that distinction in the db, but in the application it doesn’t matter most of the time.
Thanks again for the input, I’ll take this feedack to our team!
christhekeele
That’s a very common situation, makes perfect sense!
Generally, rather than scattering these checks across my codebase, I’d try to exert them within modules singly responsible for interacting with ecto structs that have these quirks—for example, perhaps inside the schema modules themselves with changesets, or a context module that validates and navigates all of this conditional logic in one place.
Specific to this common empty-string data-model case however, I’d propose a simpler way to handle this—at least, if your team is willing to make a schema change, and using postgres—other dbs may have similar solutions but I’m not sure:
This simply ensures that a given field can not be an empty string.
Whether or not my
textfields areNOT NULLthese days, I simply do not allow empty strings in my data model. It never makes sense in the domain layer, causes semantic confusion in nullable columns, and as you experience, pushes a whole bevy of edge case handling to application logic.Empty strings are the opposite of data, moreso than
NULLs! Get’em out of your database! Make invalid states unrepresentable! Have your nullable changesets cast""tonilonce, your non-nullable ones error, and never think about it again.The one exception is maybe if I have a user-entered free-form text area like a description and I want to discern between “never interacted with” and “had a value manually deleted”. But that’s really something that should be modeled differently, with change tracking or an enum-type state machine.
D4no0
I’ve dealt with this a few times also in some legacy codebases and the best solution I found is to use custom Ecto types that know how to deal with these values, you skip the step where you have to deal manually with these kind of checks.
sodapopcan
Man, this forum is unforgiving for inadvertent submits! But that is neither here not there.
I’m gonna triple down on custom Ecto types.
If they happen to sound scary, they really aren’t. If your team is resistant at all, do a book club (or “doc club” if you will) on them and you’ll realize they really aren’t.
EDIT: aaaand I need to edit again, because clearly I was responding to OP and not you, D4no0.
D4no0
I don’t see why there would be any difficulties or traction with custom ecto types, for example for strings that are not normalized in database you could do:
Where
is_emptycan be a custom guard that can detect all these shifty types, or you could do that with a function as above.dimitarvp
You’re a missing a guard here.
sodapopcan
Me neither but I’ve dealt with people who are resistant.