How to name a Boolean variable that represents either of two options?
https://softwareengineering.stackexchange.com/questions/371545
-
06-02-2021 - |
Question
I'm developing an application with Python. I want to have a Boolean variable that represent whether something is buy
or sell
but I'm not sure how I should name it. Here are my current ideas:
- isBuy
- isSell
- buy_sell
- sell_buy
- buy1_sell0
actually I like the last one the most although it's somehow the ugliest because it tells you all you need to know about it with certainty. However I thought I'd ask some more experienced people to see what is the actual python convention for such situations.
Solution
Don't use a Boolean. Use an enum. E.g TransactionType
with instances Buy
and Sell
.
That is unambiguous and far easier to understand.
If you want to persist the data efficiently, the boolean can be a good solution as long as there are only two instances in the enum. However, your code need not be efficient at that level of detail (that's the interpreters job); it needs to be very understandable. The enum achieves that goal far better.
OTHER TIPS
You should use is or has keyword in prefix to show that it is a Boolean variable. So, according to me first and second variable names are preferable instead of other variables as it clearly shows the meaning of the variable. Some of the built-in boolean variable name are as follows:
isalnum()
isalpha()
isdecimal()
isdigit()
isidentifier()
islower()
isnumeric()
isprintable()
isspace()
istitle()
isupper()