4

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.

2
  • 9
    Or maybe the name is less important if you use an enum instead of a boolean?
    – amon
    Commented May 25, 2018 at 10:02
  • An enum is likely best. IsBuy or isSell is just about acceptable. Buy_sell and the others are just awful. You might as well have “isBuyOrSell” which always returns true.
    – gnasher729
    Commented Nov 18, 2022 at 17:40

2 Answers 2

24

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.

4
  • Additionally, an enum gives you the option to easily extend the data type in case a new option needs to be added, e.g. "Lend". Commented May 25, 2018 at 15:17
  • Another bonus is that you can add logic to the enum. Example sign(), taxCode(), etc.
    – Borjab
    Commented Nov 18, 2022 at 11:33
  • @Borjab: depends on the language. It's possible in Python and Java, but in most other languages, enums cannot have functionality. Commented Nov 18, 2022 at 12:07
  • Also possible to add logic to enums in PHP, where enums are really special types of classes & objects.
    – bdsl
    Commented Nov 18, 2022 at 12:28
0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.