mediamonks / frontend-coding-standards

Media.Monks - Frontend Coding Standards
60 stars 23 forks source link

How should we name numeric properties? #60

Open ThijsTyZ opened 3 years ago

ThijsTyZ commented 3 years ago

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.

pigeonfresh commented 3 years ago

I tend to use itemsInView

Miguel-Bento-Github commented 3 years ago

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

ThijsTyZ commented 3 years ago

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

ReneDrie commented 3 years ago

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.

qiyu1987 commented 3 years ago

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

pigeonfresh commented 3 years ago

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

Miguel-Bento-Github commented 3 years ago

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

ThaNarie commented 3 years ago

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.

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

mmnathan commented 3 years ago

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