frontend-coding-standards icon indicating copy to clipboard operation
frontend-coding-standards copied to clipboard

How should we name numeric properties?

Open ThijsTyZ opened this issue 4 years ago • 9 comments

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.

ThijsTyZ avatar Feb 17 '21 11:02 ThijsTyZ

I tend to use itemsInView

pigeonfresh avatar Feb 17 '21 12:02 pigeonfresh

This might depend on the language we use. I understand why you want numVisibleItems in JS but itemsInView in TS would probably be enough.

Miguel-Bento-Github avatar Feb 17 '21 12:02 Miguel-Bento-Github

itemsInView sounds like an Array of items that is currently in view. Not like the number of items that is in view.

ThijsTyZ avatar Feb 17 '21 12:02 ThijsTyZ

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.

ReneDrie avatar Feb 17 '21 13:02 ReneDrie

I would probably vote for visibleLength, or visibleItemsCount, the others seems can also mean array of items

qiyu1987 avatar Feb 17 '21 15:02 qiyu1987

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 :)

pigeonfresh avatar Feb 18 '21 15:02 pigeonfresh

I feel like in a strongly typed language we shouldn't need the variable name to have a type.

Miguel-Bento-Github avatar Feb 18 '21 15:02 Miguel-Bento-Github

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 itemsInView it indicates it's a list (array).
  • In the case of visibleItemCount it's clear that's an amount (number).
  • Having isVisible indicates that it's a state (boolean).

None of them have the "type" in the name (array, number, boolean), but are still super clear.

ThaNarie avatar Feb 18 '21 15:02 ThaNarie

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'.

mmnathan avatar Feb 22 '21 09:02 mmnathan