matt-savvy

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

Showing Posts 1 to 10

sodapopcan

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

matt-savvy OP

I guess I mean “do you consider this a language idiom”

dimitarvp

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

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_active all 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

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

LostKobrakai

Trailing ? is certainly not just a pattern for functions. E.g. ecto uses changeset.valid?.

sodapopcan

sodapopcan

Oh ya, that is an example of keys using it that I like.

thiagomajesk

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

dimitarvp

Absolutely not, DB booleans will always be is_this_or_that on my watch. I have zero desire fighting with edge cases. Sticking to good old alpha-numeric strings for these things.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

Where Next? Top

Trending in Polls Top

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews