-
Notifications
You must be signed in to change notification settings - Fork 23
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
boolean naming #48
Comments
Why |
I guess I had a brain freeze and forgot about const [opened, setOpened] = useState(false);
const open = () => setOpened(true);
const close = () => setOpened(false); |
I agree that in some cases the Personally, I'm fine with not prefixing some of these if they are unambiguous when the name is clearly describing a state, and not an action. What I don't like at all, is reversing the name of a boolean expression to its counterpart because the name itself sounds better, but the usage of it becomes weird (double negative conditions and the like). In general, if we pick "states" ( |
It can be ugly but it also makes it so obvious that I much much prefer the first option. Also agree with |
This would sort of throw a wrench into searching through a file. If for instance I wanna view all usages of
the prefix-less equivalent for
I do agree with both of you that throwing a different tense can make it more confusing. I think the the best approach would be to simply prepend the Would be nice to document this, so it would be clear for all that it is ok to not prefix |
The advantage of the |
Currently, we sort of have a generally accepted rule, that all booleans are prefixed with is e.g.
isDragging
,isOpen
,isVisible
.I understand that this rule is in place as it makes it very clear that a variable is a boolean, however personally the is always feels ugly.
The problem that is is trying to solve is that some names could be mistaken for verbs.
Example of the issue: open is a two fold definition, it can mean "open something" (an action) and also "something is open" (a description). Same is true for dragging, "dragging something" (an action) and "something is dragging" (a description)
However, this is not always true; example being visible, which is just an adjective and wouldn't be confused for anything else. Moreover, we can also define better names for booleans, for example, instead of the confusing open or the prefixed isOpen, we can use the inverted closed, which again, wouldn't be mistaken for anything other than an adjective.
To illustrate the ugliness of using is, here's some React hooks code:
with the prefix
with better naming
Even though the latter example, flips the definition of the variable, to me it's a lot more pleasant to read.
The text was updated successfully, but these errors were encountered: