Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suffixing names of boolean values with a question mark? #182

Open
danielcompton opened this issue Jul 26, 2019 · 16 comments
Open

Suffixing names of boolean values with a question mark? #182

danielcompton opened this issue Jul 26, 2019 · 16 comments

Comments

@danielcompton
Copy link
Collaborator

When writing Clojure, I often will add a ? suffix to any boolean let bindings or defs. There is a strong convention of doing this for functions that return booleans.

Is there similar consensus that names of boolean values should be suffixed with a question mark?

See #136 as a similar issue, as well as https://guide.clojure.style/#pred-with-question-mark.

@bbatsov
Copy link
Owner

bbatsov commented Jul 30, 2019

Is there similar consensus that names of boolean values should be suffixed with a question mark?

Let's ask around. I don't do it myself (mostly because I'm too used to Ruby where this is not support for variables, only for methods), but I've seen many people doing so.

@alexanderjamesking
Copy link

+1 for appending ? to boolean values as well as functions that return a boolean.

@chrisglass
Copy link

Another 👍 for ? as a convention.

@manuel-uberti
Copy link

I regularly use ?, even for keywords indicating a boolean value in a map.

@borkdude
Copy link
Sponsor

borkdude commented Jul 30, 2019

I usually do this in bindings (argument names, local bindings, etc) and var names that are intended as predicates, but not for configured values in EDN files or metadata. This seems to be consistent with how it's done in Clojure and ClojureScript. You have int?, string?, etc., but :private, :macro, etc. in metadata:

user=> (defmacro ^:private odd? [x])
#'user/odd?
user=> (meta #'odd?)
{:private true, :arglists ([x]), :line 1, :column 1, :file "NO_SOURCE_PATH", :name odd?, :ns #object[clojure.lang.Namespace 0x5d1659ea "user"], :macro true}

Also note the boolean configuration options for the ClojureScript compiler have no question marks:

http://cljs.github.io/api/compiler-options/

So maybe:

  • settings/stating facts/dealing with serialized data: no question mark
  • asking questions (predicates): a question mark

@ioRekz
Copy link

ioRekz commented Jul 30, 2019

That's also what we do at Pitch

@blueberry
Copy link

I do it only occasionally, in cases where it is really important for that particular binding to stick out. I think that recommending that for all boolean variables would raise lots of confusion, since in clojure anything can be treated as logical true/false. Whether something is boolean or just a regular logical value is not that important in most cases. Another source of confusion would be that, since predicated are suffixed with ?, it would be visually less obvious what is a predicate function and what is a boolean local binding...

@borkdude
Copy link
Sponsor

borkdude commented Jul 30, 2019

Another consideration for the style guide:

  • IF you are using ? for a predicate THEN your function is expected to return a boolean.

There was a lot of debate when Rich introduced a question-marked predicate in 1.7-1.9 (I can't remember which release) that didn't return a boolean. Eventually he changed it.

@superstructor
Copy link

I always do this for fns, defs and keywords in maps for boolean values.

@SarmBoJim
Copy link

I'm not that experienced at programming so I might be completely misunderstanding the question.

If you are suffixing a boolean value with a question mark instead of a function that returns a boolean then that seems wrong to me as a function that returns a boolean is a question and so it makes sense to suffix it with a question mark whereas a boolean value is not a question but can be an answer to a question.

@seancorfield
Copy link
Collaborator

While I'm strongly in favor of ? on predicate functions -- and those should return only true or false -- I am against it on keywords for readability and because Clojure and ClojureScript seem to avoid ? on keywords even when they are simple Boolean flags.

I'm on the fence for local symbol bindings but lean toward not using ? on Boolean locals because I think it feels more of an arbitrary delineation between truthy locals and Boolean locals.

So my preference is ? on predicate functions only.

@WorldsEndless
Copy link

I definitely tend towards "?" For functions, not values. I generally use that to indicate "return value should be used for it's truthiness" without strictly meaning "Boolean", since it seems like Boolean are second class citizens in Clojure (needed only for interop, since truthiness is conventionally indicated by nil in Clojure)

@tosh
Copy link

tosh commented Aug 1, 2019

[to qualify my comment: I do not have nearly as much experience with Clojure as the other commenters here so take this into context please & feel free to ignore]

I do like the elegance of the naming convention to use the ? suffix for predicate functions.
It makes them easy for me to identify. They convey a useful implicit contract:

  • takes 1 argument
  • returns a boolean

(They might also work with 0 arguments and more than 1 argument on occasion but that's orthogonal additive functionality.)

Additional thought: mnemonically I read the ? as an indicator that work / thought might be done to "produce" the boolean.

I think it often causes more confusion than clarity to have similar but slightly different things seem the same.

@camsaul
Copy link

camsaul commented Aug 5, 2019

We suffix variables as well as keywords in maps with ? when they're intended to be used as boolean values at Metabase.

Personally I think it enhances readability. Let's say you have a function create-session! that can create a session for a user or some other sort of Session, e.g.

(defn create-session! [& {:keys [user-session?]}]
  (if user-session?
    #_(create user session...)
    #_(create non-user session...)))

It's pretty clear that user-session? is an option rather than some sort of existing session. Compare:

(defn create-session! [& {:keys [user-session]}]
  (if user-session
    #_(create user session...)
    #_(create non-user session...)))

It's not immediately clear whether user-session is a boolean option or some sort of existing session you can pass in. Of course, one could argue that user-session itself is a bad name, or that a docstring would fix this, but either way it seems to me like a ?-suffixed variable is always clearer, so there's no downside to adopting it as a convention.

@liuchong
Copy link

liuchong commented Apr 28, 2020

I do this on functions return boolean but not on boolean values 👀

@gphilipp
Copy link

gphilipp commented Mar 9, 2022

one could argue that user-session itself is a bad name, or that a docstring would fix this

Yes, I think it would be more appropriate to name it user-session-exists and get rid of the ?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests