2

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
14

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.

  • 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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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