Software Engineering
naming coding-standards variables naming-standards boolean
Updated Mon, 27 Jun 2022 12:58:10 GMT

How to name a Boolean variable that represents either of two options?


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.





Comments (1)

  • +0 – 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". — May 25, 2018 at 15:17