matt-savvy
I see some people adopt this convention when naming fields or variables where the value is a boolean. I personally see this as an antipattern, curious what the group here thinks. E.g.
online? = user.status == :online
Should a boolean variable or field be named with a trailing question mark?
- nope, only functions that return bool values should end with
? - yes
- I’m not sure, I see it both ways
0
voters
Trending in Polls
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
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
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
sodapopcan
I think it would help to explain why you see it as an anti-pattern. I personally really like it for variables, not so much for fields because it can cause issues with serialization (even though thanks to LiveView/OTP I haven’t had to work on a project that has a web API in a quite a while).
Perhaps I’m reading too much into your use of “should” here but my answer is that there is no “should” about it, it’s a question of style on a per-project basis.
matt-savvy
I guess I mean “do you consider this a language idiom”
dimitarvp
It honestly does not matter, if your functions get so long that a
?suffix on variables inside them actually matters then you have much bigger problems to worry about, and you should address those first.As for function suffixes, why not. It helps express intent a little bit.
Nope.
D4no0
IMO it’s a nice pattern that the language allows. You go to other languages codebases and you can always notice patterns like
is_activeall the time.For values I would say I use it half the time, however I always use this pattern for functions, really helps to distinct them between simple functions and bang functions.
sodapopcan
In that case I still don’t like any of the poll options, lol. No, it’s not an idiom and no, it’s not an anti-pattern. There is certainly no harm in it beyond personal preference. I personally really like the extra clarity and avoiding
is_,has_etc. And I don’t have an example off the top off my head but there are times where it’s awkward to come up with the correct English prefix or the correct one reads strangely. If I think of one I’ll post back.LostKobrakai
Trailing ? is certainly not just a pattern for functions. E.g. ecto uses
changeset.valid?.sodapopcan
Oh ya, that is an example of keys using it that I like.
thiagomajesk
For those who responded with “yes,” I have a question for you: Would you name your boolean database columns with a trailing question mark as well? I’m just curious to know the extent to which you would keep this naming convention in your apps.
PS.: I’m thinking in terms of keeping consistency with Ecto schemas (you can always specify the column source, but the mismatch might not be desirable)
dimitarvp
Absolutely not, DB booleans will always be
is_this_or_thaton my watch. I have zero desire fighting with edge cases. Sticking to good old alpha-numeric strings for these things.benwilson512
Notably you can have your cake and eat it too if you want by naming the database column via the is_ pattern and the Ecto schema via ? by using the source option.