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.

  • 7
    Or maybe the name is less important if you use an enum instead of a boolean? – amon May 25 '18 at 10:02

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.

| improve this answer | |
  • 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". – Michał Kosmulski May 25 '18 at 15:17

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:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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