Variable name percent from 0 to 1
Asked Answered
Z

5

21

So this seems like I should have been wondering about this when I first started programming, but I suppose back then I wasn't as concerned with 'perfect' variable naming.

So I have the variables

float lifeTime; float age;

Where lifeTime is the full lifeTime of my object, age the current lifeTime of the object. The object will die when age reaches the full lifeTime.

I was just creating a variable, ranging from 0 to 1, defining the progress of age compared to the full lifeTime. agePercent, if you will. Of course agePercent isn't correct though, as this ranges from 0 to 1.

After reading some other posts about ageFraction, ageGradient, ageNormalized, I felt like none of these fit the purpose.

My solution is agePerunum, simply using latin (I believe it's correct, but I dropped out in highschool). So my question is... is that cool? Does that make sense to you as a programmer? Any thoughts or maybe better ideas?

http://mymemory.translated.net/en/Latin/English/per-unum

Zane answered 21/2, 2017 at 5:15 Comment(3)
Thanks! The variable is directly used in the calculation and what is displayed is definitely a percentage value, not a perunumage(?) value. I see your point thoughZane
I like fraction one, as it clearly says that it is not more than the whole.Heiser
@Heiser I love this, I was so confused about this "insignificant" question, now I'm gon use thisDao
H
5

One idea is to make agePercent equal to an actual percentage, so 37 instead of 0.37. But that might make other calculations in your code require an unnecessary conversion step.

Proportion and portion are nouns you could suffix age with. Proportion means the relation of one part to another or to the whole with respect to magnitude, quantity, or degree. Portion means an often limited part of a whole. So ageProportion or agePortion? First one sounds better.

Handwoven answered 21/2, 2017 at 5:53 Comment(0)
Z
5

Actually I really liked proportion but eventually went with ageRatio - somehow that reads better ;) thanks all!

Zane answered 24/2, 2017 at 16:46 Comment(0)
D
1

I have come up with this convention that I follow in my code:

  • If the variable/parameter is in range [0 , 1] use Percent: eg. OwnershipPercent
  • If the variable/parameter is in range [0 , 100] use Percent100: eg. OwnershipPercent100
Dermatology answered 26/4, 2020 at 5:29 Comment(4)
Percent or per cent means per hundred, so it should be from 0 to 100.Owings
@AlexChe a lot of APIs present percentage in the float/double data type in the [0-1] range so we can't really avoid it unless we have a data type specifically for percentages.Dermatology
The term 'percentage' has very specific definition in math and it denotes the number from 0 to 100. And when I encounter something called SmthPercent, I definitely expect a number between 0-100. And I probably won't get the error immediately, because 0-1 is a part of a valid percentage range. Probably the main requirement for naming something in software development is unambiguity and clarity. The fact that there is some badly named API somewhere, may hardly be a reason to expand this bad naming to our code. At least it's already general usage. And in this case it's not.Owings
@AlexChe consider this assignment: count = total * tenPercent, intuitively you would expect count to be 10% of total rather than 10 times total. i.e. tenPercent = 0.10 instead of 10. That is my basic reasoning for this naming convention.Dermatology
C
0

Interesting question, although it is likely there is no correct answer and the answers will be opinion-based.

I understand why you feel using 'percent' might not be correct, but I'd have to say that 'per unum' is going to be harder to parse for someone reading the code, at least on the first reading. And even then, it is likely to end up being an attempt at being fancy, rather than being accurate or clear.

I'd say that 'percent' is actually fine, if you can draw a distinction between how the number is presented vs. how it is stored and calculated. And to this, I would look at Excel - it presents a value like 45% as 45% (or whatever formatting is applied), but internally it still stores the value as a decimal number ranging from 0 - 1, so 45% would be .45.

The decision will also be influenced by how you intend to use the progress of age. Are you displaying it as a percentage value? If so, naming the variable percentage is fine. Or, are you using it as a biasing value or ratio for some other calculation?

In any case, a documentation comment alongside the variable will help clarify things, and modern editors display documentation comments via tooltips.

Chloroprene answered 21/2, 2017 at 5:32 Comment(1)
Thanks! Yea it's used 'internally' but of course other programmers might / will see it at some point. I guess it's true that I'm secretly hoping this naming takes off and the other programmers will adjust to it - but I never wanna be that guy... :PZane
D
0

In the same way as bool or boolean can be used to represent numbers that are either zero or one, we can use Zadeh-numbers or fuzzy-values to represent numbers in the continuous interval [0-1].

Discontent answered 23/3 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.