What naming conventions do you prefer for booleans on your models?
Hi there,
I just wondered what naming conventions others used for their booleans?
In a past life I would use things like "active", "admin", etc.
When I learnt the Laravel way, I started using "is_active", "is_admin", etc.
But sometimes I have a field that indicates a product is in a feed, or a person has received some documents in the post. In those instances "is_feed" and "is_documents" sounds weird. Something like "in_feed" and "has_documents" sounds more "eloquent", but then you end up with a lot of different conventions for different booleans.
@garet Personally, most “booleans” I find can be “upgraded” to timestamps. So instead of adding a boolean is_active column to models, I’ll instead add a nullable activated_at timestamp where I can record the date and time a model was activated, rather than “is it or isn’t it?” This also enables functionality such as allowing users to mark records to be activated at a later date, i.e. a product that should go on sale at a particular date and time.
For your example, “a product is in a feed”, I don‘t really know what you’re referring to. This sounds more like business logic specific to your app and its use cases. So it’s up to you to discover the best terms for things within your business domain. If you have a Document model and want to record if a user has “received” that document, then there’s nothing stopping you adding a nullable received_at column to that table where you record when a user verifies they’ve received the document.
@martinbean Thanks for your reply, I marked @gych as the best answer because it answered the question directly and made me less worried about using terminology like in_feed rather than is_feed
I wanted to reply to your comment though because you have some great points. In some cases I do use a timestamp - for example in the case of has_documents I would probably use documents_received_at to specify when the documents were received rather than if they were received.
But you got me thinking about about other cases where I could do this, such as activated_at. The example you gave about a product launch on a particular date is a great example.