frontend-coding-standards
frontend-coding-standards copied to clipboard
How should we name numeric properties?
Let's assume there is a carousel and you can specify the amount of items that is visible at once. What would be a good name for that property? numVisibleItems? visibleItemCount? visibleLength? Something else?
It would be good if we have a standardized way for this so it is immediately clear what the purpose of a property like that is.
I tend to use itemsInView
This might depend on the language we use. I understand why you want numVisibleItems in JS but itemsInView in TS would probably be enough.
itemsInView sounds like an Array of items that is currently in view. Not like the number of items that is in view.
numVisibleItems is not even allowed right? As it's a abbreviation of number? I use itemsInView for that as well, could also be totalItemsInView if that makes more sense.
I would probably vote for visibleLength, or visibleItemsCount, the others seems can also mean array of items
The only way to make these names 100% unambiguous is by naming it like: numberOfCarouselItemsInViewport. If you create a static of the type number I think something like DraggableCarousel.itemsInView gives enough information :)
I feel like in a strongly typed language we shouldn't need the variable name to have a type.
Type information is not always (quickly) available when reading code. Like in online environments when reviewing the work.
So in my opinion, everything we can do to reduce ambiguity in naming will drastically improve the readability and understandability of the code. Finding the right balance is always tricky though :)
I think something like DraggableCarousel.itemsInView gives enough information
When reading that, to me it looks like a getter function that returns all the items that are currently in view.
I feel like in a strongly typed language we shouldn't need the variable name to have a type.
Indicating purpose doesn't mean you have to include the "type" in the name.
- In the case of
itemsInViewit indicates it's a list (array). - In the case of
visibleItemCountit's clear that's an amount (number). - Having
isVisibleindicates that it's a state (boolean).
None of them have the "type" in the name (array, number, boolean), but are still super clear.
I'd say visibleItemAmount or visibleItemLimit as it's a value you specify, a count to me indicates a set that exists and has its items 'counted'.